Skip to content

[process] Escape cmd.exe special characters in shell command names - #2884

Open
Shriraj1901 wants to merge 1 commit into
dart-lang:mainfrom
Shriraj1901:fix-process-shell-special-chars
Open

Shriraj1901 wants to merge 1 commit into
dart-lang:mainfrom
Shriraj1901:fix-process-shell-special-chars

Conversation

@Shriraj1901

Copy link
Copy Markdown

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 #2139

  • Thanks for your contribution! Please replace this text with a description of what this PR is changing or adding and why, list any relevant issues, and review the contribution guidelines below.

  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

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.

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
@Shriraj1901
Shriraj1901 requested a review from a team as a code owner November 17, 2026 09:36

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +23 to +47
/// 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]}',
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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]}',
  );
}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't launch commands with parenthesis and not spaces in the path

1 participant