Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
child_process: allow options.cwd receive a URL
Ref: #38861
  • Loading branch information
XadillaX committed May 31, 2021
commit 759e5b813d65c869d1b928003209ef3e965f7c81
14 changes: 7 additions & 7 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ changes:

* `command` {string} The command to run, with space-separated arguments.
* `options` {Object}
* `cwd` {string} Current working directory of the child process.
* `cwd` {string|URL} Current working directory of the child process.
**Default:** `process.cwd()`.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
* `encoding` {string} **Default:** `'utf8'`
Expand Down Expand Up @@ -284,7 +284,7 @@ changes:
* `file` {string} The name or path of the executable file to run.
* `args` {string[]} List of string arguments.
* `options` {Object}
* `cwd` {string} Current working directory of the child process.
* `cwd` {string|URL} Current working directory of the child process.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
* `encoding` {string} **Default:** `'utf8'`
* `timeout` {number} **Default:** `0`
Expand Down Expand Up @@ -403,7 +403,7 @@ changes:
* `modulePath` {string} The module to run in the child.
* `args` {string[]} List of string arguments.
* `options` {Object}
* `cwd` {string} Current working directory of the child process.
* `cwd` {string|URL} Current working directory of the child process.
* `detached` {boolean} Prepare child to run independently of its parent
process. Specific behavior depends on the platform, see
[`options.detached`][]).
Expand Down Expand Up @@ -517,7 +517,7 @@ changes:
* `command` {string} The command to run.
* `args` {string[]} List of string arguments.
* `options` {Object}
* `cwd` {string} Current working directory of the child process.
* `cwd` {string|URL} Current working directory of the child process.
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
* `argv0` {string} Explicitly set the value of `argv[0]` sent to the child
process. This will be set to `command` if not specified.
Expand Down Expand Up @@ -865,7 +865,7 @@ changes:
* `file` {string} The name or path of the executable file to run.
* `args` {string[]} List of string arguments.
* `options` {Object}
* `cwd` {string} Current working directory of the child process.
* `cwd` {string|URL} Current working directory of the child process.
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
as stdin to the spawned process. Supplying this value will override
`stdio[0]`.
Expand Down Expand Up @@ -928,7 +928,7 @@ changes:

* `command` {string} The command to run.
* `options` {Object}
* `cwd` {string} Current working directory of the child process.
* `cwd` {string|URL} Current working directory of the child process.
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
as stdin to the spawned process. Supplying this value will override
`stdio[0]`.
Expand Down Expand Up @@ -997,7 +997,7 @@ changes:
* `command` {string} The command to run.
* `args` {string[]} List of string arguments.
* `options` {Object}
* `cwd` {string} Current working directory of the child process.
* `cwd` {string|URL} Current working directory of the child process.
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
as stdin to the spawned process. Supplying this value will override
`stdio[0]`.
Expand Down
19 changes: 16 additions & 3 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const {
validateString,
} = require('internal/validators');
const child_process = require('internal/child_process');
const { URL } = require('internal/url');
const {
getValidStdio,
setupChannel,
Expand Down Expand Up @@ -450,9 +451,20 @@ function normalizeSpawnArguments(file, args, options) {
else
validateObject(options, 'options');

let cwd = null;
if (!(options.cwd instanceof URL)) {
cwd = options.cwd;
} else if (options.cwd.protocol === 'file:' && !options.cwd.host) {
cwd = options.cwd.pathname;
} else {
throw new ERR_INVALID_ARG_VALUE('options.cwd',
options.cwd,
'contains invalid protocol or host');
}

// Validate the cwd, if present.
if (options.cwd != null) {
validateString(options.cwd, 'options.cwd');
if (cwd != null) {
validateString(cwd, 'options.cwd');
}

// Validate detached, if present.
Expand Down Expand Up @@ -581,7 +593,8 @@ function normalizeSpawnArguments(file, args, options) {
envPairs,
file,
windowsHide: !!options.windowsHide,
windowsVerbatimArguments: !!windowsVerbatimArguments
windowsVerbatimArguments: !!windowsVerbatimArguments,
cwd,
Comment thread
aduh95 marked this conversation as resolved.
Outdated
};
}

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-child-process-cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';

const { URL } = require('url');
const common = require('../common');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
Expand Down Expand Up @@ -66,10 +68,25 @@ function testCwd(options, expectPidType, expectCode = 0, expectData) {
}));
}

{
assert.throws(() => {
testCwd({
cwd: new url("https://github.com/nodejs/node/pull/38862/commits/%60http://$%7Btmpdir.path%7D%60"),
Comment thread
XadillaX marked this conversation as resolved.
Outdated
}, 'number', 0, tmpdir.path);
}, /The property 'options\.cwd' contains invalid protocol or host/);

assert.throws(() => {
testCwd({
cwd: new url("https://github.com/nodejs/node/pull/38862/commits/%60file://host$%7Btmpdir.path%7D%60"),
}, 'number', 0, tmpdir.path);
}, /The property 'options\.cwd' contains invalid protocol or host/);
}

// Assume these exist, and 'pwd' gives us the right directory back
testCwd({ cwd: tmpdir.path }, 'number', 0, tmpdir.path);
const shouldExistDir = common.isWindows ? process.env.windir : '/dev';
testCwd({ cwd: shouldExistDir }, 'number', 0, shouldExistDir);
testCwd({ cwd: new url("https://github.com/nodejs/node/pull/38862/commits/%60file://$%7Btmpdir.path%7D%60") }, 'number', 0, tmpdir.path);
Comment thread
aduh95 marked this conversation as resolved.
Outdated

// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
testCwd({ cwd: '' }, 'number');
Expand Down