mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
child_process: allow options.cwd receive a URL
PR-URL: https://github.com/nodejs/node/pull/38862 Fixes: https://github.com/nodejs/node/issues/38861 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
@@ -146,6 +146,10 @@ exec('"my script.cmd" a b', (err, stdout, stderr) => {
|
||||
<!-- YAML
|
||||
added: v0.1.90
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/38862
|
||||
description: The `cwd` option can be a WHATWG `URL` object using
|
||||
`file:` protocol.
|
||||
- version: v15.4.0
|
||||
pr-url: https://github.com/nodejs/node/pull/36308
|
||||
description: AbortSignal support was added.
|
||||
@@ -156,7 +160,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'`
|
||||
@@ -271,6 +275,10 @@ controller.abort();
|
||||
<!-- YAML
|
||||
added: v0.1.91
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/38862
|
||||
description: The `cwd` option can be a WHATWG `URL` object using
|
||||
`file:` protocol.
|
||||
- version:
|
||||
- v15.4.0
|
||||
- v14.17.0
|
||||
@@ -284,7 +292,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`
|
||||
@@ -376,6 +384,10 @@ controller.abort();
|
||||
<!-- YAML
|
||||
added: v0.5.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/38862
|
||||
description: The `cwd` option can be a WHATWG `URL` object using
|
||||
`file:` protocol.
|
||||
- version: v15.13.0
|
||||
pr-url: https://github.com/nodejs/node/pull/37256
|
||||
description: timeout was added.
|
||||
@@ -403,7 +415,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`][]).
|
||||
@@ -487,6 +499,10 @@ if (process.argv[2] === 'child') {
|
||||
<!-- YAML
|
||||
added: v0.1.90
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/38862
|
||||
description: The `cwd` option can be a WHATWG `URL` object using
|
||||
`file:` protocol.
|
||||
- version: v15.13.0
|
||||
pr-url: https://github.com/nodejs/node/pull/37256
|
||||
description: timeout was added.
|
||||
@@ -517,7 +533,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.
|
||||
@@ -845,6 +861,10 @@ configuration at startup.
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/38862
|
||||
description: The `cwd` option can be a WHATWG `URL` object using
|
||||
`file:` protocol.
|
||||
- version: v10.10.0
|
||||
pr-url: https://github.com/nodejs/node/pull/22409
|
||||
description: The `input` option can now be any `TypedArray` or a
|
||||
@@ -865,7 +885,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]`.
|
||||
@@ -914,6 +934,10 @@ arbitrary command execution.**
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/38862
|
||||
description: The `cwd` option can be a WHATWG `URL` object using
|
||||
`file:` protocol.
|
||||
- version: v10.10.0
|
||||
pr-url: https://github.com/nodejs/node/pull/22409
|
||||
description: The `input` option can now be any `TypedArray` or a
|
||||
@@ -928,7 +952,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]`.
|
||||
@@ -974,6 +998,10 @@ metacharacters may be used to trigger arbitrary command execution.**
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/38862
|
||||
description: The `cwd` option can be a WHATWG `URL` object using
|
||||
`file:` protocol.
|
||||
- version: v10.10.0
|
||||
pr-url: https://github.com/nodejs/node/pull/22409
|
||||
description: The `input` option can now be any `TypedArray` or a
|
||||
@@ -997,7 +1025,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]`.
|
||||
|
||||
@@ -71,6 +71,7 @@ const {
|
||||
ERR_OUT_OF_RANGE,
|
||||
} = errorCodes;
|
||||
const { clearTimeout, setTimeout } = require('timers');
|
||||
const { getValidatedPath } = require('internal/fs/utils');
|
||||
const {
|
||||
isInt32,
|
||||
validateAbortSignal,
|
||||
@@ -450,9 +451,11 @@ function normalizeSpawnArguments(file, args, options) {
|
||||
else
|
||||
validateObject(options, 'options');
|
||||
|
||||
let cwd = options.cwd;
|
||||
|
||||
// Validate the cwd, if present.
|
||||
if (options.cwd != null) {
|
||||
validateString(options.cwd, 'options.cwd');
|
||||
if (cwd != null) {
|
||||
cwd = getValidatedPath(cwd, 'options.cwd');
|
||||
}
|
||||
|
||||
// Validate detached, if present.
|
||||
@@ -577,11 +580,12 @@ function normalizeSpawnArguments(file, args, options) {
|
||||
// Make a shallow copy so we don't clobber the user's options object.
|
||||
...options,
|
||||
args,
|
||||
cwd,
|
||||
detached: !!options.detached,
|
||||
envPairs,
|
||||
file,
|
||||
windowsHide: !!options.windowsHide,
|
||||
windowsVerbatimArguments: !!windowsVerbatimArguments
|
||||
windowsVerbatimArguments: !!windowsVerbatimArguments,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -20,12 +20,14 @@
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
|
||||
const assert = require('assert');
|
||||
const { spawn } = require('child_process');
|
||||
const { pathToFileURL, URL } = require('url');
|
||||
|
||||
// Spawns 'pwd' with given options, then test
|
||||
// - whether the child pid is undefined or number,
|
||||
@@ -66,10 +68,27 @@ function testCwd(options, expectPidType, expectCode = 0, expectData) {
|
||||
}));
|
||||
}
|
||||
|
||||
{
|
||||
assert.throws(() => {
|
||||
testCwd({
|
||||
cwd: new URL('http://example.com/'),
|
||||
}, 'number', 0, tmpdir.path);
|
||||
}, /The URL must be of scheme file/);
|
||||
|
||||
if (process.platform !== 'win32') {
|
||||
assert.throws(() => {
|
||||
testCwd({
|
||||
cwd: new URL('file://host/dev/null'),
|
||||
}, 'number', 0, tmpdir.path);
|
||||
}, /File URL host must be "localhost" or empty on/);
|
||||
}
|
||||
}
|
||||
|
||||
// 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: pathToFileURL(tmpdir.path) }, 'number', 0, tmpdir.path);
|
||||
|
||||
// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
|
||||
testCwd({ cwd: '' }, 'number');
|
||||
|
||||
Reference in New Issue
Block a user