Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 69fa9e1

Browse filesBrowse files
XadillaXtargos
authored andcommitted
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 0706565 commit 69fa9e1
Copy full SHA for 69fa9e1

File tree

Expand file treeCollapse file tree

3 files changed

+61
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+61
-10
lines changed
Open diff view settings
Collapse file

‎doc/api/child_process.md‎

Copy file name to clipboardExpand all lines: doc/api/child_process.md
+35-7Lines changed: 35 additions & 7 deletions
  • Display the source diff
  • Display the rich diff
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: v15.4.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: v15.4.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: v15.13.0
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: v15.13.0
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]`.
Collapse file

‎lib/child_process.js‎

Copy file name to clipboardExpand all lines: lib/child_process.js
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const {
7171
ERR_OUT_OF_RANGE,
7272
} = errorCodes;
7373
const { clearTimeout, setTimeout } = require('timers');
74+
const { getValidatedPath } = require('internal/fs/utils');
7475
const {
7576
isInt32,
7677
validateAbortSignal,
@@ -450,9 +451,11 @@ function normalizeSpawnArguments(file, args, options) {
450451
else
451452
validateObject(options, 'options');
452453

454+
let cwd = options.cwd;
455+
453456
// Validate the cwd, if present.
454-
if (options.cwd != null) {
455-
validateString(options.cwd, 'options.cwd');
457+
if (cwd != null) {
458+
cwd = getValidatedPath(cwd, 'options.cwd');
456459
}
457460

458461
// Validate detached, if present.
@@ -577,11 +580,12 @@ function normalizeSpawnArguments(file, args, options) {
577580
// Make a shallow copy so we don't clobber the user's options object.
578581
...options,
579582
args,
583+
cwd,
580584
detached: !!options.detached,
581585
envPairs,
582586
file,
583587
windowsHide: !!options.windowsHide,
584-
windowsVerbatimArguments: !!windowsVerbatimArguments
588+
windowsVerbatimArguments: !!windowsVerbatimArguments,
585589
};
586590
}
587591

Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-child-process-cwd.js
+19Lines changed: 19 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.