Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/process/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 5.0.7-wip

* Fix running an executable with `runInShell: true` on Windows when its path
contains parentheses (or another character that `cmd.exe` treats specially)
but no spaces.

## 5.0.6

* Add an example app.
Expand Down
26 changes: 26 additions & 0 deletions pkgs/process/lib/src/interface/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ const Map<String, String> _osToPathStyle = <String, String>{
'windows': 'windows',
};

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

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


/// Searches the `PATH` for the executable that [executable] is supposed to launch.
///
/// This first builds a list of candidate paths where the executable may reside.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ String _getExecutable(
) {
final String commandName = command.first.toString();
if (runInShell) {
return commandName;
return escapeShellExecutable(commandName);
}
return getExecutablePath(
commandName,
Expand Down
57 changes: 44 additions & 13 deletions pkgs/process/test/src/interface/common_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,43 @@ import 'package:process/src/interface/common.dart';
import 'package:test/test.dart';

void main() {
group('escapeShellExecutable', () {
final windows = FakePlatform(operatingSystem: 'windows');
final linux = FakePlatform(operatingSystem: 'linux');

String escape(String executable, Platform platform) =>
escapeShellExecutable(executable, platform: platform);

test('escapes cmd.exe special characters on Windows', () {
expect(escape(r'C:\theP()ath\tool.exe', windows),
r'C:\theP^(^)ath\tool.exe');
expect(escape(r'C:\a&b\tool.exe', windows), r'C:\a^&b\tool.exe');
expect(escape(r'C:\a^b\tool.exe', windows), r'C:\a^^b\tool.exe');
expect(
escape(r'C:\a,b;c=d\tool.exe', windows), r'C:\a^,b^;c^=d\tool.exe');
});

test('leaves ordinary names alone on Windows', () {
expect(escape('tool', windows), 'tool');
expect(escape(r'C:\Program-Files\tool.exe', windows),
r'C:\Program-Files\tool.exe');
});

test('leaves names with whitespace alone, dart:io quotes them', () {
expect(escape(r'C:\the P()ath\tool.exe', windows),
r'C:\the P()ath\tool.exe');
});

test('leaves already quoted names alone', () {
expect(escape(r'"C:\theP()ath\tool.exe"', windows),
r'"C:\theP()ath\tool.exe"');
});

test('does nothing on other platforms', () {
expect(escape('/theP()ath/tool', linux), '/theP()ath/tool');
});
});

group('getExecutablePath', () {
late FileSystem fs;
late Directory workingDir, dir1, dir2, dir3;
Expand Down Expand Up @@ -678,22 +715,16 @@ void main() {
testCompileAndExecute(main);
});

test(
'with parenthesis in the command name',
() async {
final dir = tmpDir.childDirectory('theP()ath');
final main = dir.childFile('main.dart')
..createSync(recursive: true)
..writeAsStringSync('''
test('with parenthesis in the command name', () async {
final dir = tmpDir.childDirectory('theP()ath');
final main = dir.childFile('main.dart')
..createSync(recursive: true)
..writeAsStringSync('''
void main() {
print('hello');
}''');
testCompileAndExecute(main);
},
skip: io.Platform.isWindows
? 'https://github.com/dart-lang/tools/issues/2139'
: null,
);
testCompileAndExecute(main);
});

test('with spaces and parenthesis in the command name', () async {
final dir = tmpDir.childDirectory('the P()ath');
Expand Down
Loading