Skip to content

Commit 14fc4dd

Browse files
XadillaXtargos
authored andcommittedSep 4, 2021
child_process: allow options.cwd receive a URL
PR-URL: #38862 Fixes: #38861 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 15ba19b commit 14fc4dd

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed
 

‎doc/api/child_process.md

+35-7
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ exec('"my script.cmd" a b', (err, stdout, stderr) => {
146146
<!-- YAML
147147
added: v0.1.90
148148
changes:
149+
- version: REPLACEME
150+
pr-url: https://github.com/nodejs/node/pull/38862
151+
description: The `cwd` option can be a WHATWG `URL` object using
152+
`file:` protocol.
149153
- version: v14.17.0
150154
pr-url: https://github.com/nodejs/node/pull/36308
151155
description: AbortSignal support was added.
@@ -156,7 +160,7 @@ changes:
156160

157161
* `command` {string} The command to run, with space-separated arguments.
158162
* `options` {Object}
159-
* `cwd` {string} Current working directory of the child process.
163+
* `cwd` {string|URL} Current working directory of the child process.
160164
**Default:** `process.cwd()`.
161165
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
162166
* `encoding` {string} **Default:** `'utf8'`
@@ -271,6 +275,10 @@ controller.abort();
271275
<!-- YAML
272276
added: v0.1.91
273277
changes:
278+
- version: REPLACEME
279+
pr-url: https://github.com/nodejs/node/pull/38862
280+
description: The `cwd` option can be a WHATWG `URL` object using
281+
`file:` protocol.
274282
- version: v14.17.0
275283
pr-url: https://github.com/nodejs/node/pull/36308
276284
description: AbortSignal support was added.
@@ -282,7 +290,7 @@ changes:
282290
* `file` {string} The name or path of the executable file to run.
283291
* `args` {string[]} List of string arguments.
284292
* `options` {Object}
285-
* `cwd` {string} Current working directory of the child process.
293+
* `cwd` {string|URL} Current working directory of the child process.
286294
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
287295
* `encoding` {string} **Default:** `'utf8'`
288296
* `timeout` {number} **Default:** `0`
@@ -374,6 +382,10 @@ controller.abort();
374382
<!-- YAML
375383
added: v0.5.0
376384
changes:
385+
- version: REPLACEME
386+
pr-url: https://github.com/nodejs/node/pull/38862
387+
description: The `cwd` option can be a WHATWG `URL` object using
388+
`file:` protocol.
377389
- version: REPLACEME
378390
pr-url: https://github.com/nodejs/node/pull/37256
379391
description: timeout was added.
@@ -399,7 +411,7 @@ changes:
399411
* `modulePath` {string} The module to run in the child.
400412
* `args` {string[]} List of string arguments.
401413
* `options` {Object}
402-
* `cwd` {string} Current working directory of the child process.
414+
* `cwd` {string|URL} Current working directory of the child process.
403415
* `detached` {boolean} Prepare child to run independently of its parent
404416
process. Specific behavior depends on the platform, see
405417
[`options.detached`][]).
@@ -483,6 +495,10 @@ if (process.argv[2] === 'child') {
483495
<!-- YAML
484496
added: v0.1.90
485497
changes:
498+
- version: REPLACEME
499+
pr-url: https://github.com/nodejs/node/pull/38862
500+
description: The `cwd` option can be a WHATWG `URL` object using
501+
`file:` protocol.
486502
- version: REPLACEME
487503
pr-url: https://github.com/nodejs/node/pull/37256
488504
description: timeout was added.
@@ -511,7 +527,7 @@ changes:
511527
* `command` {string} The command to run.
512528
* `args` {string[]} List of string arguments.
513529
* `options` {Object}
514-
* `cwd` {string} Current working directory of the child process.
530+
* `cwd` {string|URL} Current working directory of the child process.
515531
* `env` {Object} Environment key-value pairs. **Default:** `process.env`.
516532
* `argv0` {string} Explicitly set the value of `argv[0]` sent to the child
517533
process. This will be set to `command` if not specified.
@@ -839,6 +855,10 @@ configuration at startup.
839855
<!-- YAML
840856
added: v0.11.12
841857
changes:
858+
- version: REPLACEME
859+
pr-url: https://github.com/nodejs/node/pull/38862
860+
description: The `cwd` option can be a WHATWG `URL` object using
861+
`file:` protocol.
842862
- version: v10.10.0
843863
pr-url: https://github.com/nodejs/node/pull/22409
844864
description: The `input` option can now be any `TypedArray` or a
@@ -859,7 +879,7 @@ changes:
859879
* `file` {string} The name or path of the executable file to run.
860880
* `args` {string[]} List of string arguments.
861881
* `options` {Object}
862-
* `cwd` {string} Current working directory of the child process.
882+
* `cwd` {string|URL} Current working directory of the child process.
863883
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
864884
as stdin to the spawned process. Supplying this value will override
865885
`stdio[0]`.
@@ -908,6 +928,10 @@ arbitrary command execution.**
908928
<!-- YAML
909929
added: v0.11.12
910930
changes:
931+
- version: REPLACEME
932+
pr-url: https://github.com/nodejs/node/pull/38862
933+
description: The `cwd` option can be a WHATWG `URL` object using
934+
`file:` protocol.
911935
- version: v10.10.0
912936
pr-url: https://github.com/nodejs/node/pull/22409
913937
description: The `input` option can now be any `TypedArray` or a
@@ -922,7 +946,7 @@ changes:
922946

923947
* `command` {string} The command to run.
924948
* `options` {Object}
925-
* `cwd` {string} Current working directory of the child process.
949+
* `cwd` {string|URL} Current working directory of the child process.
926950
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
927951
as stdin to the spawned process. Supplying this value will override
928952
`stdio[0]`.
@@ -968,6 +992,10 @@ metacharacters may be used to trigger arbitrary command execution.**
968992
<!-- YAML
969993
added: v0.11.12
970994
changes:
995+
- version: REPLACEME
996+
pr-url: https://github.com/nodejs/node/pull/38862
997+
description: The `cwd` option can be a WHATWG `URL` object using
998+
`file:` protocol.
971999
- version: v10.10.0
9721000
pr-url: https://github.com/nodejs/node/pull/22409
9731001
description: The `input` option can now be any `TypedArray` or a
@@ -991,7 +1019,7 @@ changes:
9911019
* `command` {string} The command to run.
9921020
* `args` {string[]} List of string arguments.
9931021
* `options` {Object}
994-
* `cwd` {string} Current working directory of the child process.
1022+
* `cwd` {string|URL} Current working directory of the child process.
9951023
* `input` {string|Buffer|TypedArray|DataView} The value which will be passed
9961024
as stdin to the spawned process. Supplying this value will override
9971025
`stdio[0]`.

‎lib/child_process.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const {
5858
ERR_OUT_OF_RANGE,
5959
} = errorCodes;
6060
const { clearTimeout, setTimeout } = require('timers');
61+
const { getValidatedPath } = require('internal/fs/utils');
6162
const {
6263
validateString,
6364
isInt32,
@@ -503,9 +504,11 @@ function normalizeSpawnArguments(file, args, options) {
503504
else if (options === null || typeof options !== 'object')
504505
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
505506

507+
let cwd = options.cwd;
508+
506509
// Validate the cwd, if present.
507-
if (options.cwd != null) {
508-
validateString(options.cwd, 'options.cwd');
510+
if (cwd != null) {
511+
cwd = getValidatedPath(cwd, 'options.cwd');
509512
}
510513

511514
// Validate detached, if present.
@@ -602,11 +605,12 @@ function normalizeSpawnArguments(file, args, options) {
602605
// Make a shallow copy so we don't clobber the user's options object.
603606
...options,
604607
args,
608+
cwd,
605609
detached: !!options.detached,
606610
envPairs,
607611
file,
608612
windowsHide: !!options.windowsHide,
609-
windowsVerbatimArguments: !!windowsVerbatimArguments
613+
windowsVerbatimArguments: !!windowsVerbatimArguments,
610614
};
611615
}
612616

‎test/parallel/test-child-process-cwd.js

+19
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23+
2324
const common = require('../common');
2425
const tmpdir = require('../common/tmpdir');
2526
tmpdir.refresh();
2627

2728
const assert = require('assert');
2829
const { spawn } = require('child_process');
30+
const { pathToFileURL, URL } = require('url');
2931

3032
// Spawns 'pwd' with given options, then test
3133
// - whether the child pid is undefined or number,
@@ -66,10 +68,27 @@ function testCwd(options, expectPidType, expectCode = 0, expectData) {
6668
}));
6769
}
6870

71+
{
72+
assert.throws(() => {
73+
testCwd({
74+
cwd: new URL('http://example.com/'),
75+
}, 'number', 0, tmpdir.path);
76+
}, /The URL must be of scheme file/);
77+
78+
if (process.platform !== 'win32') {
79+
assert.throws(() => {
80+
testCwd({
81+
cwd: new URL('file://host/dev/null'),
82+
}, 'number', 0, tmpdir.path);
83+
}, /File URL host must be "localhost" or empty on/);
84+
}
85+
}
86+
6987
// Assume these exist, and 'pwd' gives us the right directory back
7088
testCwd({ cwd: tmpdir.path }, 'number', 0, tmpdir.path);
7189
const shouldExistDir = common.isWindows ? process.env.windir : '/dev';
7290
testCwd({ cwd: shouldExistDir }, 'number', 0, shouldExistDir);
91+
testCwd({ cwd: pathToFileURL(tmpdir.path) }, 'number', 0, tmpdir.path);
7392

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

0 commit comments

Comments
 (0)
Please sign in to comment.