Skip to content

Commit 481dc8f

Browse files
Attila Hajdrikremy
Attila Hajdrik
authored andcommitted
fix: executable path handling under windows (#962)
- If the user configured a *nix compatible npm script where the path contained a forward slash, that failed under windows, since cmd.exe treated that as a command line argument. - The quote handling was not 100% correct under windows, the whole path needs to be quoted, not just the segments it is also modified.
1 parent 3c352f2 commit 481dc8f

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lib/monitor/run.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var noop = function () {};
1212
var restart = null;
1313
var psTree = require('ps-tree');
1414
var hasPS = true;
15+
var path = require('path');
1516

1617
// discover if the OS has `ps`, and therefore can use psTree
1718
exec('ps', function (error) {
@@ -52,11 +53,23 @@ function run(options) {
5253

5354
var executable = cmd.executable;
5455

55-
// special logic for windows, as spaces in the paths need the path fragment
56-
// quoted, so it reads: c:\"Program Files"\nodejs\node.exe
57-
if (utils.isWindows && executable.indexOf(' ') !== -1) {
58-
var re = /\\((\w+\s+)+\w+)(?=([\\\.]))(?=([^"]*"[^"]*")*[^"]*$)/g;
59-
executable = executable.replace(re, '\\"$1"');
56+
if (utils.isWindows) {
57+
// under windows if the executable path contains a forward slash, that will
58+
// fail with cmd.exe, so we need to normalize it
59+
if (executable.indexOf('/') !== -1) {
60+
executable = path.normalize(executable);
61+
}
62+
63+
// if the executable path contains a space the whole string must be quoted
64+
// to get windows treat it as 1 argument for cmd.exe
65+
if (executable.indexOf(' ') !== -1 && executable[0] !== '\"'
66+
&& executable[executable.length - 1] !== '\"') {
67+
// remove all quotes from executable (possible backward compat hacks)
68+
executable = executable.replace (/\"/g, '');
69+
70+
// add quotes to beginning and end of executable
71+
executable = '\"' + executable + '\"';
72+
}
6073
}
6174

6275
var args = runCmd ? utils.stringify(executable, cmd.args) : ':';

0 commit comments

Comments
 (0)