Repository navigation
[process] Escape cmd.exe special characters in shell command names - #2884
Shriraj1901 wants to merge 1 commit into
Conversation
On Windows, running an executable with `runInShell: true` failed when its path contained a parenthesis (or `&`, `^`, `,`, `;`, `=`) but no space: `C:\theP()ath\tool.exe` was cut off at the `(` and cmd.exe reported that `C:\theP` is not recognized as a command. dart:io only quotes a command name that contains a space, so names without one reached cmd.exe unescaped. Caret-escape those characters in the executable when running in a shell on Windows. Names that contain whitespace or a quote are left alone, since dart:io already quotes them or the caller already did. This lets the previously skipped "with parenthesis in the command name" test run on Windows again. Fixes dart-lang#2139
There was a problem hiding this comment.
Code Review
This pull request resolves an issue on Windows where running an executable with runInShell: true fails if its path contains special characters (such as parentheses) but no spaces. It introduces the escapeShellExecutable utility to escape these characters for cmd.exe, integrates it into LocalProcessManager, and adds corresponding unit tests. The feedback suggests optimizing performance by extracting the inline regular expression for whitespace and quotes into a top-level private variable to avoid re-compiling it on every function call.
| /// Characters that are legal in a Windows file name but that `cmd.exe` treats | ||
| /// as syntax when they appear in an unquoted command name. | ||
| final RegExp _cmdSpecialCharacters = RegExp(r'[&^(),;=]'); | ||
|
|
||
| /// Escapes [executable] so that `cmd.exe` runs it as a single command when it | ||
| /// is launched with `runInShell: true` on Windows. | ||
| /// | ||
| /// Without this, a path such as `C:\theP()ath\tool.exe` is cut off at the `(` | ||
| /// and `cmd.exe` reports that `C:\theP` is not recognized as a command. | ||
| /// | ||
| /// Names that contain whitespace are returned unchanged, because `dart:io` | ||
| /// already wraps those in quotes. The same goes for names that are already | ||
| /// quoted, and for every platform other than Windows. | ||
| String escapeShellExecutable( | ||
| String executable, { | ||
| Platform platform = const LocalPlatform(), | ||
| }) { | ||
| if (!platform.isWindows || executable.contains(RegExp(r'[\s"]'))) { | ||
| return executable; | ||
| } | ||
| return executable.replaceAllMapped( | ||
| _cmdSpecialCharacters, | ||
| (match) => '^${match[0]}', | ||
| ); | ||
| } |
There was a problem hiding this comment.
To avoid re-compiling and re-instantiating the regular expression RegExp(r'[\s"]') on every call to escapeShellExecutable, extract it into a top-level private final RegExp variable, similar to _cmdSpecialCharacters.
/// Characters that are legal in a Windows file name but that `cmd.exe` treats
/// as syntax when they appear in an unquoted command name.
final RegExp _cmdSpecialCharacters = RegExp(r'[&^(),;=]');
final RegExp _whitespaceOrQuote = RegExp(r'[\s"]');
/// Escapes [executable] so that `cmd.exe` runs it as a single command when it
/// is launched with `runInShell: true` on Windows.
///
/// Without this, a path such as `C:\theP()ath\tool.exe` is cut off at the `(`
/// and `cmd.exe` reports that `C:\theP` is not recognized as a command.
///
/// Names that contain whitespace are returned unchanged, because `dart:io`
/// already wraps those in quotes. The same goes for names that are already
/// quoted, and for every platform other than Windows.
String escapeShellExecutable(
String executable, {
Platform platform = const LocalPlatform(),
}) {
if (!platform.isWindows || executable.contains(_whitespaceOrQuote)) {
return executable;
}
return executable.replaceAllMapped(
_cmdSpecialCharacters,
(match) => '^${match[0]}',
);
}
On Windows, running an executable with
runInShell: truefailed when its path contained a parenthesis (or&,^,,,;,=) but no space:C:\theP()ath\tool.exewas cut off at the(and cmd.exe reported thatC:\thePis not recognized as a command. dart:io only quotes a command name that contains a space, so names without one reached cmd.exe unescaped.Caret-escape those characters in the executable when running in a shell on Windows. Names that contain whitespace or a quote are left alone, since dart:io already quotes them or the caller already did.
This lets the previously skipped "with parenthesis in the command name" test run on Windows again.
Fixes #2139
Contribution guidelines:
dart format.Many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.
Note: The Dart team is trialing Gemini Code Assist. Don't take its comments as final Dart team feedback. Use the suggestions if they're helpful; otherwise, wait for a human reviewer.