diff --git a/pkgs/process/CHANGELOG.md b/pkgs/process/CHANGELOG.md index 6160302f67..05b16c6509 100644 --- a/pkgs/process/CHANGELOG.md +++ b/pkgs/process/CHANGELOG.md @@ -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. diff --git a/pkgs/process/lib/src/interface/common.dart b/pkgs/process/lib/src/interface/common.dart index 80801abf01..4008c1295a 100644 --- a/pkgs/process/lib/src/interface/common.dart +++ b/pkgs/process/lib/src/interface/common.dart @@ -20,6 +20,32 @@ const Map _osToPathStyle = { '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]}', + ); +} + /// 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. diff --git a/pkgs/process/lib/src/interface/local_process_manager.dart b/pkgs/process/lib/src/interface/local_process_manager.dart index 6c7734a689..b286ba2b44 100644 --- a/pkgs/process/lib/src/interface/local_process_manager.dart +++ b/pkgs/process/lib/src/interface/local_process_manager.dart @@ -131,7 +131,7 @@ String _getExecutable( ) { final String commandName = command.first.toString(); if (runInShell) { - return commandName; + return escapeShellExecutable(commandName); } return getExecutablePath( commandName, diff --git a/pkgs/process/test/src/interface/common_test.dart b/pkgs/process/test/src/interface/common_test.dart index 353486c214..0303196775 100644 --- a/pkgs/process/test/src/interface/common_test.dart +++ b/pkgs/process/test/src/interface/common_test.dart @@ -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; @@ -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');