Fix `escapedCommand` and `verbose` quoting arguments when using the `shell` option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
With `shell`, the shell receives the file and arguments joined with spaces, so quoting each argument showed a different command than the one that ran. Fixes #1264
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 2141147 · Browse files · 7 files·+42 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+42 added−8 removed
- tests:+28 added−2 removed
- other:+14 added−6 removed
Provenance
- Commit
b2c3a54- Tree
a31b297- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
2141147
7 files changed+42 added−8 removed
modified:docs/api.md+2 added
630 unmodified lines631 _Type:_ `string`632 633 Same as [`command`](#resultcommand) but escaped.634 635+With the [`shell`](#optionsshell) option, the command is not quoted, since the shell interprets it.636+637 [More info.](debugging.md#command)638 639 ### result.cwd640 731 unmodified linesmodified:docs/debugging.md+1 added-1 removed
8 unmodified lines9 ## Command10 11 [`error.command`](api.md#resultcommand) contains the file and [arguments](input.md#command-arguments) that were run. It is intended for logging or debugging.12 13-[`error.escapedCommand`](api.md#resultescapedcommand) is the same, except control characters are escaped. This makes it safe to either print or copy and paste in a terminal, for debugging purposes.13+[`error.escapedCommand`](api.md#resultescapedcommand) is the same, except control characters are escaped. This makes it safe to either print or copy and paste in a terminal, for debugging purposes. With the [`shell`](api.md#optionsshell) option, the command is not quoted, since the shell interprets it.14 15 Since the escaping is fairly basic, neither `error.command` nor `error.escapedCommand` should be executed directly, including using [`execa()`](api.md#execafile-arguments-options) or [`parseCommandString()`](api.md#parsecommandstringcommand).16 17 ```js187 unmodified linesmodified:lib/arguments/command.js+2 added-1 removed
5 unmodified lines6789101011121314157 unmodified lines5 unmodified lines
// Compute `result.command`, `result.escapedCommand` and `verbose`-related informationexport const handleCommand = (filePath, rawArguments, rawOptions) => { const startTime = getStartTime(); // The `node` option ignores `shell` const {command, escapedCommand} = joinCommand(filePath, rawArguments); const {command, escapedCommand} = joinCommand(filePath, rawArguments, rawOptions.shell && !rawOptions.node); const verbose = normalizeFdSpecificOption(rawOptions, 'verbose'); // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values const verboseInfo = getVerboseInfo(verbose, escapedCommand, {__proto__: null, ...rawOptions}); logCommand(escapedCommand, verboseInfo);7 unmodified linesmodified:lib/arguments/escape.js+7 added-4 removed
12345567891089101112131415161774 unmodified linesimport {platform} from 'node:process';import {stripVTControlCharacters} from 'node:util';
// Compute `result.command` and `result.escapedCommand`export const joinCommand = (filePath, rawArguments) => {export const joinCommand = (filePath, rawArguments, shell) => { const fileAndArguments = [filePath, ...rawArguments]; const command = fileAndArguments.join(' '); // With `shell`, the shell interprets the command, so it must not be quoted const escapedCommand = shell ? escapeControlCharacters(command) const escapedCommand = fileAndArguments .map(fileAndArgument => quoteString(escapeControlCharacters(fileAndArgument))) .join(' '); : fileAndArguments .map(fileAndArgument => quoteString(escapeControlCharacters(fileAndArgument))) .join(' '); return {command, escapedCommand};};
// Remove ANSI sequences and escape control characters and newlines74 unmodified linesmodified:test/arguments/escape.js+23 added-2 removed
1 import assert from 'node:assert/strict';2 import {platform} from 'node:process';3 import test from 'node:test';4 import {assertThrows, assertRejects, assertLike} from '../helpers/assert.js';5-import {execa, execaSync} from '../../index.js';6-import {setFixtureDirectory} from '../helpers/fixtures-directory.js';5+import {execa, execaSync, execaNode} from '../../index.js';6+import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';7 8 setFixtureDirectory();9 10 const isWindows = platform === 'win32';123 unmodified lines134 for (const commandArguments of shellRoundTripArguments) {135 test(`result.escapedCommand can be pasted in a shell: ${JSON.stringify(commandArguments)}`, () => testShellRoundTrip(commandArguments));136 }137 }138+139+// With `shell`, the shell interprets the command, so `result.escapedCommand` must not quote it.140+const testShellEscapedCommand = async (execaMethod, commandArguments, expected) => {141+ const {escapedCommand} = await execaMethod('echo', commandArguments, {shell: true});142+ assert.equal(escapedCommand, expected);143+};144+145+test('result.escapedCommand does not quote with "shell"', () => testShellEscapedCommand(execa, ['foo', '|', 'sort'], 'echo foo | sort'));146+test('result.escapedCommand does not quote with "shell", sync', () => testShellEscapedCommand(execaSync, ['foo', '|', 'sort'], 'echo foo | sort'));147+test('result.escapedCommand escapes control characters with "shell"', () => testShellEscapedCommand(execa, ['\'foo\tbar\''], 'echo \'foo\\tbar\''));148+149+// The `node` option ignores `shell`150+test('result.escapedCommand quotes with "shell" and "node"', async () => {151+ const {escapedCommand} = await execaNode('noop.js', ['foo bar'], {shell: true, cwd: FIXTURES_DIRECTORY});152+ assert.equal(escapedCommand, isWindows ? 'noop.js "foo bar"' : 'noop.js \'foo bar\'');153+});154+155+test('result.escapedCommand does not quote with "shell" and template strings', async () => {156+ const {escapedCommand} = await execa({shell: true})`echo 'foo' | sort`;157+ assert.equal(escapedCommand, 'echo \'foo\' | sort');158+});modified:test/verbose/start.js+5 added
145 unmodified lines146 const {stderr} = await nestedSubprocess('noop.js', ['foo bar'], {verbose: 'short'});147 assert.equal(getCommandLine(stderr), `${testTimestamp} [0] $ noop.js ${QUOTE}foo bar${QUOTE}`);148 });149 150+test('Does not quote command with "shell"', async () => {151+ const {stderr} = await nestedSubprocess('echo', ['foo', '|', 'sort'], {verbose: 'short', shell: true});152+ assert.equal(getCommandLine(stderr), `${testTimestamp} [0] $ echo foo | sort`);153+});154+155 test('Quotes special punctuation from command', async () => {156 const {stderr} = await nestedSubprocess('noop.js', ['%'], {verbose: 'short'});157 assert.equal(getCommandLine(stderr), `${testTimestamp} [0] $ noop.js ${QUOTE}%${QUOTE}`);158 });15 unmodified linesmodified:types/return/result.d.ts+2 added
70 unmodified lines71 command: string;72 73 /**74 Same as `command` but escaped.75+76+ With the `shell` option, the command is not quoted, since the shell interprets it.77 */78 escapedCommand: string;79 80 /**127 unmodified lines