GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — child-process: support any shells on Windows by tkamenoko · Pull Request #21943 · nodejs/node

child-process: support any shells on Windows #21943

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.

Sign up for GitHub

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
tkamenoko wants to merge 16 commits into nodejs:master from tkamenoko:child-process-for-pwsh

Uh oh!

There was an error while loading. Please reload this page.

Closed

child-process: support any shells on Windows #21943

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
b42be28
child-process: fix switches for alternative shells on Windows
tkamenoko Jul 21, 2018
e9863e6
child-process: fix lint errors
tkamenoko Jul 21, 2018
6f3bccc
test: add exec-any-shells test
tkamenoko Jul 21, 2018
6b9b090
test: fix exec-any-shell test for powershell
tkamenoko Jul 23, 2018
1dcea34
child-process: enable to accept full path that contains spaces
tkamenoko Jul 23, 2018
c7163f8
child-process: undo unrelated style changes
tkamenoko Jul 24, 2018
a314340
test: remove hard coding path
tkamenoko Jul 26, 2018
ce7999a
test: fix spawnsync-shell to check correct options on Windows
tkamenoko Jul 26, 2018
097c905
child-process: set windowsVerbatimArguments true for any shell
tkamenoko Aug 2, 2018
a4122b7
child-process: use case-insensitive comparison to check shell name
tkamenoko Aug 2, 2018
27ba48f
test: add testcases to ensure uppercase shell name and complex commands
tkamenoko Aug 2, 2018
f93c796
test: fix to accept multiple lines output of `where bash`
tkamenoko Aug 2, 2018
ade01d9
test: revert windowsVerbatim flag, fix `isCmd` check in spawnsync-shell
tkamenoko Aug 2, 2018
5a2c130
doc: update shell requirements in child_process
tkamenoko Aug 7, 2018
e2192a3
fixup: windowsVerbatimArguments
joaocgreis Aug 14, 2018
7a10600
fixup: don't exhaust exec buffer
joaocgreis Sep 1, 2018
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

9 changes: 5 additions & 4 deletions 9 doc/api/child_process.md
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ changes:
[Default Windows Shell][]. **Default:** `false` (no shell).
* `windowsVerbatimArguments` {boolean} No quoting or escaping of arguments is
done on Windows. Ignored on Unix. This is set to `true` automatically
when `shell` is specified. **Default:** `false`.
when `shell` is specified and is CMD. **Default:** `false`.
* `windowsHide` {boolean} Hide the subprocess console window that would
normally be created on Windows systems. **Default:** `true`.
* Returns: {ChildProcess}
Expand Down Expand Up @@ -867,7 +867,7 @@ changes:
[Default Windows Shell][]. **Default:** `false` (no shell).
* `windowsVerbatimArguments` {boolean} No quoting or escaping of arguments is
done on Windows. Ignored on Unix. This is set to `true` automatically
when `shell` is specified. **Default:** `false`.
when `shell` is specified and is CMD. **Default:** `false`.
* `windowsHide` {boolean} Hide the subprocess console window that would
normally be created on Windows systems. **Default:** `true`.
* Returns: {Object}
Expand Down Expand Up @@ -1432,8 +1432,9 @@ to `stdout` although there are only 4 characters.

## Shell Requirements

The shell should understand the `-c` switch on UNIX or `/d /s /c` on Windows.
On Windows, command line parsing should be compatible with `'cmd.exe'`.
The shell should understand the `-c` switch. If the shell is `'cmd.exe'`, it
should understand the `/d /s /c` switches and command line parsing should be
compatible.

## Default Windows Shell

Expand Down
11 changes: 8 additions & 3 deletions 11 lib/child_process.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,19 @@ function normalizeSpawnArguments(file, args, options) {

if (options.shell) {
const command = [file].concat(args).join(' ');

// Set the shell, switches, and commands.
if (process.platform === 'win32') {
if (typeof options.shell === 'string')
file = options.shell;
else
file = process.env.comspec || 'cmd.exe';
args = ['/d', '/s', '/c', `"${command}"`];
options.windowsVerbatimArguments = true;
// '/d /s /c' is used only for cmd.exe.
if (/^(?:.*\\)?cmd(?:\.exe)?$/i.test(file)) {
Comment thread

@refack refack Aug 20, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

A late comment, I think that \b might be a simpler test for the prefix:

if (/\bcmd(\.exe)?$/i.test(file)) 

also IMHO no need to ?: the groups since we just .test

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@refack refack Aug 20, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

P.S. I would call my comment a "nit" (that is a non blocking style change)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
args = ['/d', '/s', '/c', `"${command}"`];
options.windowsVerbatimArguments = true;
} else {
args = ['-c', command];
}
} else {
if (typeof options.shell === 'string')
file = options.shell;
Expand Down
68 changes: 68 additions & 0 deletions 68 test/parallel/test-child-process-exec-any-shells-windows.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');

// This test is only relevant on Windows.
if (!common.isWindows)
common.skip('Windows specific test.');

// This test ensures that child_process.exec can work with any shells.

tmpdir.refresh();
const tmpPath = `${tmpdir.path}\\path with spaces`;
fs.mkdirSync(tmpPath);

const test = (shell) => {
cp.exec('echo foo bar', { shell: shell },
common.mustCall((error, stdout, stderror) => {
assert.ok(!error && !stderror);
assert.ok(stdout.includes('foo') && stdout.includes('bar'));
}));
};
const testCopy = (shellName, shellPath) => {
// Copy the executable to a path with spaces, to ensure there are no issues
// related to quoting of argv0
const copyPath = `${tmpPath}\\${shellName}`;
fs.copyFileSync(shellPath, copyPath);
test(copyPath);
};

const system32 = `${process.env.SystemRoot}\\System32`;

// Test CMD
test(true);
test('cmd');
testCopy('cmd.exe', `${system32}\\cmd.exe`);
test('cmd.exe');
test('CMD');

// Test PowerShell
test('powershell');
testCopy('powershell.exe',
`${system32}\\WindowsPowerShell\\v1.0\\powershell.exe`);
fs.writeFile(`${tmpPath}\\test file`, 'Test', common.mustCall((err) => {
assert.ifError(err);
cp.exec(`Get-ChildItem "${tmpPath}" | Select-Object -Property Name`,
{ shell: 'PowerShell' },
common.mustCall((error, stdout, stderror) => {
assert.ok(!error && !stderror);
assert.ok(stdout.includes(
'test file'));
}));
}));

// Test Bash (from WSL and Git), if available
cp.exec('where bash', common.mustCall((error, stdout) => {
if (error) {
return;
}
const lines = stdout.trim().split(/[\r\n]+/g);
for (let i = 0; i < lines.length; ++i) {
const bashPath = lines[i].trim();
test(bashPath);
testCopy(`bash_${i}.exe`, bashPath);
}
}));
9 changes: 5 additions & 4 deletions 9 test/parallel/test-child-process-spawnsync-shell.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ assert.strictEqual(env.stdout.toString().trim(), 'buzz');

function test(testPlatform, shell, shellOutput) {
platform = testPlatform;

const isCmd = /^(?:.*\\)?cmd(?:\.exe)?$/i.test(shellOutput);
const cmd = 'not_a_real_command';
const shellFlags = platform === 'win32' ? ['/d', '/s', '/c'] : ['-c'];
const outputCmd = platform === 'win32' ? `"${cmd}"` : cmd;
const windowsVerbatim = platform === 'win32' ? true : undefined;

const shellFlags = isCmd ? ['/d', '/s', '/c'] : ['-c'];
const outputCmd = isCmd ? `"${cmd}"` : cmd;
const windowsVerbatim = isCmd ? true : undefined;
internalCp.spawnSync = common.mustCall(function(opts) {
assert.strictEqual(opts.file, shellOutput);
assert.deepStrictEqual(opts.args,
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.