Skip to content

Commit e561895

Browse files
ppannutocjihrig
authored andcommitted
child_process: control argv0 for spawned processes
In some cases it useful to control the value of `argv[0]`, c.f. - https://github.com/andrewffff/child_process_with_argv0 - https://github.com/andrep/argv0 This patch adds explicit support for setting the value of `argv[0]` when spawning a process. PR-URL: #7696 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2f32191 commit e561895

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

doc/api/child_process.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ added: v0.1.90
295295
* `options` {Object}
296296
* `cwd` {String} Current working directory of the child process
297297
* `env` {Object} Environment key-value pairs
298+
* `argv0` {String} Explicitly set the value of `argv[0]` sent to the child
299+
process. This will be set to `command` if not specified.
298300
* `stdio` {Array|String} Child's stdio configuration. (See
299301
[`options.stdio`][`stdio`])
300302
* `detached` {Boolean} Prepare child to run independently of its parent
@@ -397,6 +399,14 @@ child.on('error', (err) => {
397399
});
398400
```
399401

402+
*Note: Certain platforms (OS X, Linux) will use the value of `argv[0]` for the
403+
process title while others (Windows, SunOS) will use `command`.*
404+
405+
*Note: Node.js currently overwrites `argv[0]` with `process.execPath` on
406+
startup, so `process.argv[0]` in a Node.js child process will not match the
407+
`argv0` parameter passed to `spawn` from the parent, retrieve it with the
408+
`process.argv0` property instead.*
409+
400410
#### options.detached
401411
<!-- YAML
402412
added: v0.7.10

lib/child_process.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,11 @@ function normalizeSpawnArguments(file /*, args, options*/) {
346346
}
347347
}
348348

349-
args.unshift(file);
349+
if (typeof options.argv0 === 'string') {
350+
args.unshift(options.argv0);
351+
} else {
352+
args.unshift(file);
353+
}
350354

351355
var env = options.env || process.env;
352356
var envPairs = [];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const cp = require('child_process');
5+
6+
// This test spawns itself with an argument to indicate when it is a child to
7+
// easily and portably print the value of argv[0]
8+
if (process.argv[2] === 'child') {
9+
console.log(process.argv0);
10+
return;
11+
}
12+
13+
const noArgv0 = cp.spawnSync(process.execPath, [__filename, 'child']);
14+
assert.strictEqual(noArgv0.stdout.toString().trim(), process.execPath);
15+
16+
const withArgv0 = cp.spawnSync(process.execPath, [__filename, 'child'],
17+
{argv0: 'withArgv0'});
18+
assert.strictEqual(withArgv0.stdout.toString().trim(), 'withArgv0');

0 commit comments

Comments
 (0)