Skip to content

Commit 81d01bc

Browse files
cjihrigMylesBorins
authored andcommittedOct 24, 2017
child_process: add windowsHide option
This commit exposes the UV_PROCESS_WINDOWS_HIDE flag in Node as a windowsHide option to the child_process methods. The option is only applicable to Windows, and is ignored elsewhere. Backport-PR-URL: #16425 Fixes: #15217 PR-URL: #15380 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ff25ca7 commit 81d01bc

8 files changed

+118
-0
lines changed
 

‎doc/api/child_process.md

+32
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ exec('"my script.cmd" a b', (err, stdout, stderr) => {
127127
### child_process.exec(command[, options][, callback])
128128
<!-- YAML
129129
added: v0.1.90
130+
changes:
131+
- version: REPLACEME
132+
pr-url: https://github.com/nodejs/node/pull/15380
133+
description: The `windowsHide` option is supported now.
130134
-->
131135

132136
* `command` {string} The command to run, with space-separated arguments.
@@ -144,6 +148,8 @@ added: v0.1.90
144148
* `killSignal` {string|integer} **Default:** `'SIGTERM'`
145149
* `uid` {number} Sets the user identity of the process (see setuid(2)).
146150
* `gid` {number} Sets the group identity of the process (see setgid(2)).
151+
* `windowsHide` {boolean} Hide the subprocess console window that would
152+
normally be created on Windows systems. **Default:** `false`.
147153
* `callback` {Function} called with the output when process terminates.
148154
* `error` {Error}
149155
* `stdout` {string|Buffer}
@@ -237,6 +243,10 @@ lsExample();
237243
### child_process.execFile(file[, args][, options][, callback])
238244
<!-- YAML
239245
added: v0.1.91
246+
changes:
247+
- version: REPLACEME
248+
pr-url: https://github.com/nodejs/node/pull/15380
249+
description: The `windowsHide` option is supported now.
240250
-->
241251

242252
* `file` {string} The name or path of the executable file to run.
@@ -252,6 +262,8 @@ added: v0.1.91
252262
* `killSignal` {string|integer} **Default:** `'SIGTERM'`
253263
* `uid` {number} Sets the user identity of the process (see setuid(2)).
254264
* `gid` {number} Sets the group identity of the process (see setgid(2)).
265+
* `windowsHide` {boolean} Hide the subprocess console window that would
266+
normally be created on Windows systems. **Default:** `false`.
255267
* `callback` {Function} Called with the output when process terminates.
256268
* `error` {Error}
257269
* `stdout` {string|Buffer}
@@ -363,6 +375,9 @@ supported by `child_process.fork()` and will be ignored if set.
363375
<!-- YAML
364376
added: v0.1.90
365377
changes:
378+
- version: REPLACEME
379+
pr-url: https://github.com/nodejs/node/pull/15380
380+
description: The `windowsHide` option is supported now.
366381
- version: v6.4.0
367382
pr-url: https://github.com/nodejs/node/pull/7696
368383
description: The `argv0` option is supported now.
@@ -389,6 +404,8 @@ changes:
389404
`'/bin/sh'` on UNIX, and `process.env.ComSpec` on Windows. A different
390405
shell can be specified as a string. See [Shell Requirements][] and
391406
[Default Windows Shell][]. **Default:** `false` (no shell).
407+
* `windowsHide` {boolean} Hide the subprocess console window that would
408+
normally be created on Windows systems. **Default:** `false`.
392409
* Returns: {ChildProcess}
393410

394411
The `child_process.spawn()` method spawns a new process using the given
@@ -648,6 +665,9 @@ configuration at startup.
648665
<!-- YAML
649666
added: v0.11.12
650667
changes:
668+
- version: REPLACEME
669+
pr-url: https://github.com/nodejs/node/pull/15380
670+
description: The `windowsHide` option is supported now.
651671
- version: v8.0.0
652672
pr-url: https://github.com/nodejs/node/pull/10653
653673
description: The `input` option can now be a `Uint8Array`.
@@ -677,6 +697,8 @@ changes:
677697
stderr. **Default:** `200*1024` If exceeded, the child process is terminated.
678698
See caveat at [`maxBuffer` and Unicode][].
679699
* `encoding` {string} The encoding used for all stdio inputs and outputs. **Default:** `'buffer'`
700+
* `windowsHide` {boolean} Hide the subprocess console window that would
701+
normally be created on Windows systems. **Default:** `false`.
680702
* Returns: {Buffer|string} The stdout from the command.
681703

682704
The `child_process.execFileSync()` method is generally identical to
@@ -697,6 +719,9 @@ throw an [`Error`][] that will include the full result of the underlying
697719
<!-- YAML
698720
added: v0.11.12
699721
changes:
722+
- version: REPLACEME
723+
pr-url: https://github.com/nodejs/node/pull/15380
724+
description: The `windowsHide` option is supported now.
700725
- version: v8.0.0
701726
pr-url: https://github.com/nodejs/node/pull/10653
702727
description: The `input` option can now be a `Uint8Array`.
@@ -726,6 +751,8 @@ changes:
726751
See caveat at [`maxBuffer` and Unicode][].
727752
* `encoding` {string} The encoding used for all stdio inputs and outputs.
728753
**Default:** `'buffer'`
754+
* `windowsHide` {boolean} Hide the subprocess console window that would
755+
normally be created on Windows systems. **Default:** `false`.
729756
* Returns: {Buffer|string} The stdout from the command.
730757

731758
The `child_process.execSync()` method is generally identical to
@@ -748,6 +775,9 @@ execution.
748775
<!-- YAML
749776
added: v0.11.12
750777
changes:
778+
- version: REPLACEME
779+
pr-url: https://github.com/nodejs/node/pull/15380
780+
description: The `windowsHide` option is supported now.
751781
- version: v8.0.0
752782
pr-url: https://github.com/nodejs/node/pull/10653
753783
description: The `input` option can now be a `Uint8Array`.
@@ -783,6 +813,8 @@ changes:
783813
`'/bin/sh'` on UNIX, and `process.env.ComSpec` on Windows. A different
784814
shell can be specified as a string. See [Shell Requirements][] and
785815
[Default Windows Shell][]. **Default:** `false` (no shell).
816+
* `windowsHide` {boolean} Hide the subprocess console window that would
817+
normally be created on Windows systems. **Default:** `false`.
786818
* Returns: {Object}
787819
* `pid` {number} Pid of the child process.
788820
* `output` {Array} Array of results from stdio output.

‎lib/child_process.js

+8
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
212212
gid: options.gid,
213213
uid: options.uid,
214214
shell: options.shell,
215+
windowsHide: !!options.windowsHide,
215216
windowsVerbatimArguments: !!options.windowsVerbatimArguments
216217
});
217218

@@ -428,6 +429,12 @@ function normalizeSpawnArguments(file, args, options) {
428429
throw new TypeError('"argv0" must be a string');
429430
}
430431

432+
// Validate windowsHide, if present.
433+
if (options.windowsHide != null &&
434+
typeof options.windowsHide !== 'boolean') {
435+
throw new TypeError('"windowsHide" must be a boolean');
436+
}
437+
431438
// Validate windowsVerbatimArguments, if present.
432439
if (options.windowsVerbatimArguments != null &&
433440
typeof options.windowsVerbatimArguments !== 'boolean') {
@@ -493,6 +500,7 @@ var spawn = exports.spawn = function(/*file, args, options*/) {
493500
file: opts.file,
494501
args: opts.args,
495502
cwd: options.cwd,
503+
windowsHide: !!options.windowsHide,
496504
windowsVerbatimArguments: !!options.windowsVerbatimArguments,
497505
detached: !!options.detached,
498506
envPairs: opts.envPairs,

‎src/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ class ModuleWrap;
287287
V(verify_error_string, "verifyError") \
288288
V(version_string, "version") \
289289
V(weight_string, "weight") \
290+
V(windows_hide_string, "windowsHide") \
290291
V(windows_verbatim_arguments_string, "windowsVerbatimArguments") \
291292
V(wrap_string, "wrap") \
292293
V(writable_string, "writable") \

‎src/process_wrap.cc

+7
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ class ProcessWrap : public HandleWrap {
211211
// options.stdio
212212
ParseStdioOptions(env, js_options, &options);
213213

214+
// options.windowsHide
215+
Local<String> windows_hide_key = env->windows_hide_string();
216+
217+
if (js_options->Get(windows_hide_key)->IsTrue()) {
218+
options.flags |= UV_PROCESS_WINDOWS_HIDE;
219+
}
220+
214221
// options.windows_verbatim_arguments
215222
Local<String> windows_verbatim_arguments_key =
216223
env->windows_verbatim_arguments_string();

‎src/spawn_sync.cc

+5
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,11 @@ int SyncProcessRunner::ParseOptions(Local<Value> js_value) {
777777
if (js_options->Get(env()->detached_string())->BooleanValue())
778778
uv_process_options_.flags |= UV_PROCESS_DETACHED;
779779

780+
Local<String> win_hide = env()->windows_hide_string();
781+
782+
if (js_options->Get(win_hide)->BooleanValue())
783+
uv_process_options_.flags |= UV_PROCESS_WINDOWS_HIDE;
784+
780785
Local<String> wba = env()->windows_verbatim_arguments_string();
781786

782787
if (js_options->Get(wba)->BooleanValue())

‎test/parallel/test-child-process-spawnsync-shell.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ assert.strictEqual(env.stdout.toString().trim(), 'buzz');
6060
assert.strictEqual(result.options.shell, shell);
6161
assert.strictEqual(result.options.file, result.file);
6262
assert.deepStrictEqual(result.options.args, result.args);
63+
assert.strictEqual(result.options.windowsHide, undefined);
6364
assert.strictEqual(result.options.windowsVerbatimArguments,
6465
windowsVerbatim);
6566
}

‎test/parallel/test-child-process-spawnsync-validation-errors.js

+16
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ if (!common.isWindows) {
124124
fail('argv0', common.mustNotCall(), err);
125125
}
126126

127+
{
128+
// Validate the windowsHide option
129+
const err = /^TypeError: "windowsHide" must be a boolean$/;
130+
131+
pass('windowsHide', undefined);
132+
pass('windowsHide', null);
133+
pass('windowsHide', true);
134+
pass('windowsHide', false);
135+
fail('windowsHide', 0, err);
136+
fail('windowsHide', 1, err);
137+
fail('windowsHide', __dirname, err);
138+
fail('windowsHide', [], err);
139+
fail('windowsHide', {}, err);
140+
fail('windowsHide', common.mustNotCall(), err);
141+
}
142+
127143
{
128144
// Validate the windowsVerbatimArguments option
129145
const err = /^TypeError: "windowsVerbatimArguments" must be a boolean$/;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Flags: --expose_internals
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const cp = require('child_process');
6+
const internalCp = require('internal/child_process');
7+
const cmd = process.execPath;
8+
const args = ['-p', '42'];
9+
const options = { windowsHide: true };
10+
11+
// Since windowsHide isn't really observable, monkey patch spawn() to verify
12+
// the flag is being passed through correctly. spawnSync() is kind enough to
13+
// give all of the parsed inputs back in the result.
14+
const originalSpawn = internalCp.ChildProcess.prototype.spawn;
15+
16+
internalCp.ChildProcess.prototype.spawn = common.mustCall(function(options) {
17+
assert.strictEqual(options.windowsHide, true);
18+
return originalSpawn.apply(this, arguments);
19+
}, 2);
20+
21+
{
22+
const child = cp.spawnSync(cmd, args, options);
23+
24+
assert.strictEqual(child.status, 0);
25+
assert.strictEqual(child.signal, null);
26+
assert.strictEqual(child.options.windowsHide, true);
27+
assert.strictEqual(child.stdout.toString().trim(), '42');
28+
assert.strictEqual(child.stderr.toString().trim(), '');
29+
}
30+
31+
{
32+
const child = cp.spawn(cmd, args, options);
33+
34+
child.on('exit', common.mustCall((code, signal) => {
35+
assert.strictEqual(code, 0);
36+
assert.strictEqual(signal, null);
37+
}));
38+
}
39+
40+
{
41+
const callback = common.mustCall((err, stdout, stderr) => {
42+
assert.ifError(err);
43+
assert.strictEqual(stdout.trim(), '42');
44+
assert.strictEqual(stderr.trim(), '');
45+
});
46+
47+
cp.execFile(cmd, args, options, callback);
48+
}

0 commit comments

Comments
 (0)
Please sign in to comment.