Skip to content

lib: allow custom names for spawned processes #7696

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

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 10 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ added: v0.1.90
* `options` {Object}
* `cwd` {String} Current working directory of the child process
* `env` {Object} Environment key-value pairs
* `argv0` {String} Explicitly set the value of `argv[0]` sent to the child
process. This will be set to `command` if not specified.
* `stdio` {Array|String} Child's stdio configuration. (See
[`options.stdio`][`stdio`])
* `detached` {Boolean} Prepare child to run independently of its parent
Expand Down Expand Up @@ -395,6 +397,14 @@ child.on('error', (err) => {
});
```

*Note: Certain platforms (OS X, Linux) will use the value of `argv[0]` for the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/_Note:/_Note*:

For this one and the next. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every other "Note" in the file currently italicizes the whole note ( https://github.com/nodejs/node/blame/master/doc/api/child_process.md#L26, https://github.com/nodejs/node/blame/master/doc/api/child_process.md#L39, https://github.com/nodejs/node/blame/master/doc/api/child_process.md#L188 etc). I'm happy to change this one instance if you want, but should it really be inconsistent with the rest of the file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, no worries. I'll go through and fix those in a separate PR :-)

process title while others (Windows, SunOS) will use `command`.*

*Note: Node.js currently overwrites `argv[0]` with `process.execPath` on
startup, so `process.argv[0]` in a Node.js child process will not match the
`argv0` parameter passed to `spawn` from the parent, retrieve it with the
`process.argv0` property instead.*

#### options.detached
<!-- YAML
added: v0.7.10
Expand Down
23 changes: 20 additions & 3 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,10 @@ added: v0.1.27

The `process.argv` property returns an array containing the command line
arguments passed when the Node.js process was launched. The first element will
be [`process.execPath`]. The second element will be the path to the
JavaScript file being executed. The remaining elements will be any additional
command line arguments.
be [`process.execPath`]. See `process.argv0` if access to the original value of
`argv[0]` is needed. The second element will be the path to the JavaScript
file being executed. The remaining elements will be any additional command line
arguments.

For example, assuming the following script for `process-args.js`:

Expand All @@ -481,6 +482,22 @@ Would generate the output:
4: four
```

## process.argv0
<!-- YAML
added: REPLACEME
-->

The `process.argv0` property stores a read-only copy of the original value of
`argv[0]` passed when Node.js starts.

```js
$ bash -c 'exec -a customArgv0 ./node'
> process.argv[0]
'/Volumes/code/external/node/out/Release/node'
> process.argv0
'customArgv0'
```

## process.chdir(directory)
<!-- YAML
added: v0.1.17
Expand Down
6 changes: 5 additions & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@ function normalizeSpawnArguments(file /*, args, options*/) {
}
}

args.unshift(file);
if (typeof options.argv0 === 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that this is read-only now, perhaps simply doing an existence check is enough? e.g.

args.unshift(options.argv0 ? options.argv0 : file);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell options.argv0 comes from the caller so it can be anything, unlike process.argv0 :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha! sorry... totally misread that :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argv0 here isn't necessarily from process.argv0, it's a parameter passed by the user. I think the check is still necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I had mistakenly read that code... nevermind me ;-)

args.unshift(options.argv0);
} else {
args.unshift(file);
}

var env = options.env || process.env;
var envPairs = [];
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@

_process.setupRawDebug();

Object.defineProperty(process, 'argv0', {
enumerable: true,
configurable: false,
value: process.argv[0]
});
process.argv[0] = process.execPath;

// There are various modes that Node can run in. The most common two
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-child-process-spawn-argv0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
require('../common');
const assert = require('assert');
const cp = require('child_process');

// This test spawns itself with an argument to indicate when it is a child to
// easily and portably print the value of argv[0]
if (process.argv[2] === 'child') {
console.log(process.argv0);
return;
}

const noArgv0 = cp.spawnSync(process.execPath, [__filename, 'child']);
assert.strictEqual(noArgv0.stdout.toString().trim(), process.execPath);

const withArgv0 = cp.spawnSync(process.execPath, [__filename, 'child'],
{argv0: 'withArgv0'});
assert.strictEqual(withArgv0.stdout.toString().trim(), 'withArgv0');