Fix `execaSync()` returning `undefined` output when the subprocess fails to spawn
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #1263
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 93bddc4 · Browse files · 2 files·+27 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+27 added−6 removed
- tests:+19 added−0 removed
- other:+8 added−6 removed
Provenance
- Commit
bfe4633- Tree
ebb8cc4- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
93bddc4
2 files changed+27 added−6 removed
modified:lib/io/output-sync.js+8 added-6 removed
6 unmodified lines7891011121314151112131415167 unmodified lines24252627282930313233343543 unmodified lines798081828382848586878870 unmodified lines6 unmodified linesimport {getFileTarget} from '../stdio/duplicate.js';import {truncateMaxBufferSync} from './max-buffer.js';
// Apply `stdout`/`stderr` options, after spawning, in sync modeexport const transformOutputSync = ({fileDescriptors, syncResult: {output}, options, verboseInfo}) => { if (output === null) { return {output: Array.from({length: fileDescriptors.length}), error: undefined}; }
export const transformOutputSync = ({fileDescriptors, syncResult, options, verboseInfo}) => { const output = syncResult.output ?? getSpawnFailureOutput(options); const outputFiles = new Set(); // Transforms, `verbose` and file writes apply to a single file descriptor each, so their errors must not affect the other ones const errors = []; const transformedOutput = output.map((result, fdNumber) =>7 unmodified lines }, options)); return {output: transformedOutput, error: errors.find(error => error !== undefined)};};
// When the subprocess fails to spawn, `spawnSync()` returns no output. Like with asynchronous methods, each piped output is empty instead.// This is the output `spawnSync()` returns when the subprocess succeeds without printing anything: `stdin` and non-piped file descriptors are `null`. Input pipes on other file descriptors cannot be used with synchronous methods.const getSpawnFailureOutput = ({stdio}) => stdio.map((stdioOption, fdNumber) => fdNumber !== 0 && stdioOption === 'pipe' ? new Uint8Array() : null);
const transformOutputResultSync = ( {result, fileDescriptors, fdNumber, errors, outputFiles, verboseInfo}, {buffer, encoding, lines, stripFinalNewline, maxBuffer},) => {43 unmodified lines
// Applies transform generators to `stdout`/`stderr`const runOutputGeneratorsSync = (chunks, stdioItems, encoding) => { try { // `error` is set, so that a polluted `Object.prototype.error` is not read when destructuring return {chunks: runGeneratorsSync(chunks, stdioItems, encoding, false)}; return {chunks: runGeneratorsSync(chunks, stdioItems, encoding, false), error: undefined}; } catch (error) { return {chunks, error}; }};70 unmodified linesmodified:test/return/early-error.js+19 added
13 unmodified lines14 } from '../../index.js';15 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';16 import {foobarString, foobarUint8Array} from '../helpers/input.js';17 import {fullStdio} from '../helpers/stdio.js';18+import {getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';19 import {20 earlyErrorOptions,21 getEarlyErrorSubprocess,22 getEarlyErrorSubprocessSync,20 unmodified lines43 execaSync('foo');44 }, {message: ENOENT_REGEXP});45 });46 47+// The subprocess never started, so its output is empty, but it must have the same shape as with the asynchronous methods48+const testSpawnFailureOutput = async options => {49+ const {stdio, all} = await execa('nonexistent-command-for-execa', {...options, reject: false});50+ const {stdio: stdioSync, all: allSync} = execaSync('nonexistent-command-for-execa', {...options, reject: false});51+ assert.deepEqual(stdioSync, stdio);52+ assert.deepEqual(allSync, all);53+};54+55+test('execaSync() output is empty when the command does not exist', () => testSpawnFailureOutput({}));56+test('execaSync() output is empty when the command does not exist, with encoding: buffer', () => testSpawnFailureOutput({encoding: 'buffer'}));57+test('execaSync() output is empty when the command does not exist, with lines: true', () => testSpawnFailureOutput({lines: true}));58+test('execaSync() output is empty when the command does not exist, with all: true', () => testSpawnFailureOutput({all: true}));59+test('execaSync() output is empty when the command does not exist, with an additional file descriptor', () => testSpawnFailureOutput(fullStdio));60+test('execaSync() output is undefined when the command does not exist, with stdout: ignore', () => testSpawnFailureOutput({stdout: 'ignore'}));61+test('execaSync() output is undefined when the command does not exist, with buffer: false', () => testSpawnFailureOutput({buffer: false}));62+test('execaSync() output is empty when the cwd does not exist', () => testSpawnFailureOutput({cwd: '/nonexistent-directory-for-execa'}));63+test('execaSync() output runs the final of transforms when the command does not exist', () => testSpawnFailureOutput({stdout: convertTransformToFinal(getOutputGenerator(foobarString)(), true)}));64+65 const testEarlyErrorShape = async reject => {66 const subprocess = getEarlyErrorSubprocess({reject});67 assert.doesNotThrow(() => {68 subprocess.catch(() => {});211 unmodified lines