Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: child_process: clarify subprocess.pid can be undefined when ENOENT #37014

Merged
merged 2 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,11 @@ does not indicate that the child process has been terminated.
added: v0.1.90
-->

* {integer}
* {integer|undefined}

Returns the process identifier (PID) of the child process.
Returns the process identifier (PID) of the child process. If the child process
fails to spawn due to errors, then the value is `undefined` and `error` is
emitted.

```js
const { spawn } = require('child_process');
Expand Down
17 changes: 10 additions & 7 deletions test/parallel/test-child-process-cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ const assert = require('assert');
const { spawn } = require('child_process');

// Spawns 'pwd' with given options, then test
// - whether the child pid is undefined or number,
// - whether the exit code equals expectCode,
// - optionally whether the trimmed stdout result matches expectData
function testCwd(options, expectCode = 0, expectData) {
function testCwd(options, expectPidType, expectCode = 0, expectData) {
const child = spawn(...common.pwdCommand, options);

assert.strictEqual(typeof child.pid, expectPidType);

child.stdout.setEncoding('utf8');

// No need to assert callback since `data` is asserted.
Expand All @@ -57,18 +60,18 @@ function testCwd(options, expectCode = 0, expectData) {

// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
{
testCwd({ cwd: 'does-not-exist' }, -1)
testCwd({ cwd: 'does-not-exist' }, 'undefined', -1)
.on('error', common.mustCall(function(e) {
assert.strictEqual(e.code, 'ENOENT');
}));
}

// Assume these exist, and 'pwd' gives us the right directory back
testCwd({ cwd: tmpdir.path }, 0, tmpdir.path);
testCwd({ cwd: tmpdir.path }, 'number', 0, tmpdir.path);
const shouldExistDir = common.isWindows ? process.env.windir : '/dev';
testCwd({ cwd: shouldExistDir }, 0, shouldExistDir);
testCwd({ cwd: shouldExistDir }, 'number', 0, shouldExistDir);

// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
testCwd({ cwd: '' });
testCwd({ cwd: undefined });
testCwd({ cwd: null });
testCwd({ cwd: '' }, 'number');
testCwd({ cwd: undefined }, 'number');
testCwd({ cwd: null }, 'number');
14 changes: 9 additions & 5 deletions test/parallel/test-child-process-exec-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');

function test(fn, code) {
fn('does-not-exist', common.mustCall(function(err) {
function test(fn, code, expectPidType = 'number') {
const child = fn('does-not-exist', common.mustCall(function(err) {
assert.strictEqual(err.code, code);
assert(err.cmd.includes('does-not-exist'));
}));

assert.strictEqual(typeof child.pid, expectPidType);
}

// With `shell: true`, expect pid (of the shell)
if (common.isWindows) {
test(child_process.exec, 1); // Exit code of cmd.exe
test(child_process.exec, 1, 'number'); // Exit code of cmd.exe
} else {
test(child_process.exec, 127); // Exit code of /bin/sh
test(child_process.exec, 127, 'number'); // Exit code of /bin/sh
}

test(child_process.execFile, 'ENOENT');
// With `shell: false`, expect no pid
test(child_process.execFile, 'ENOENT', 'undefined');
3 changes: 3 additions & 0 deletions test/parallel/test-child-process-spawn-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ assert.strictEqual(enoentChild.stdio[0], enoentChild.stdin);
assert.strictEqual(enoentChild.stdio[1], enoentChild.stdout);
assert.strictEqual(enoentChild.stdio[2], enoentChild.stderr);

// Verify pid is not assigned.
assert.strictEqual(enoentChild.pid, undefined);

enoentChild.on('spawn', common.mustNotCall());

enoentChild.on('error', common.mustCall(function(err) {
Expand Down