Fix an uncaught error when a file target cannot be opened and the subprocess fails to spawn
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 15c46cd · Browse files · 4 files·+41 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+41 added−5 removed
- tests:+33 added−0 removed
- other:+8 added−5 removed
Provenance
- Commit
1cee524- Tree
2e8dd15- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
15c46cd
4 files changed+41 added−5 removed
modified:lib/stdio/handle.js+8 added-5 removed
239 unmodified lines240 // If the subprocess spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.241 // We need to create those streams before subprocess spawning, in case their creation fails, e.g. when passing an invalid generator as argument.242 // Like this, an exception would be thrown, which would prevent spawning a subprocess.243 export const cleanupCustomStreams = fileDescriptors => {244- for (const {stdioItems} of fileDescriptors) {245- for (const {stream} of stdioItems) {246- if (stream !== undefined && !isStandardStream(stream)) {247- stream.destroy();248- }244+ for (const {stream} of fileDescriptors.flatMap(({stdioItems}) => stdioItems)) {245+ if (stream === undefined || isStandardStream(stream)) {246+ continue;247 }248+249+ // The stream can still error after being destroyed, e.g. when its file cannot be opened.250+ // That error would be uncaught and crash the current process, although it is irrelevant since execa already fails with its own error.251+ stream.on('error', () => {});252+ stream.destroy();253 }254 };255 256 // When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `subprocess.std*`.10 unmodified linesadded:test/fixtures/early-error-file.js+8 added
12345678#!/usr/bin/env nodeimport process from 'node:process';import {execa} from '../../index.js';import {earlyErrorOptions} from '../helpers/early-error.js';
// The file cannot be opened, since its directory does not existconst {code} = await execa('empty.js', {...earlyErrorOptions, [process.argv[2]]: {file: '/nonexistent-directory-for-execa/file'}, reject: false});console.log(code);added:test/fixtures/invalid-stdio-file.js+10 added
12345678910#!/usr/bin/env nodeimport {execa} from '../../index.js';
// The file cannot be opened, since its directory does not exist.// Its stream is created before the next item is rejected, which throws before spawning.try { execa('empty.js', {stdout: [{file: '/nonexistent-directory-for-execa/file'}, ['foo']]});} catch (error) { console.log(error.message);}modified:test/return/early-error.js+15 added
96 unmodified lines97 test('child_process.spawn() early errors are propagated', async () => {98 await assertRejects(getEarlyErrorSubprocess(), expectedEarlyError);99 });100 101+// File targets are opened before spawning, then closed since the subprocess did not spawn.102+// Failing to open them must not be an uncaught exception, which would crash the current process.103+const testEarlyErrorFile = async optionName => {104+ const {stdout} = await execa('early-error-file.js', [optionName]);105+ assert.equal(stdout, expectedEarlyError.code);106+};107+108+test('child_process.spawn() early errors do not crash if an output file cannot be opened', () => testEarlyErrorFile('stdout'));109+test('child_process.spawn() early errors do not crash if an input file cannot be opened', () => testEarlyErrorFile('stdin'));110+111+test('Invalid stdio options do not crash if an output file cannot be opened', async () => {112+ const {stdout} = await execa('invalid-stdio-file.js');113+ assert.equal(stdout, 'The `stdout` option cannot be an iterable.');114+});115+116 test('child_process.spawn() early errors are returned', async () => {117 const {failed} = await getEarlyErrorSubprocess({reject: false});118 assert.equal(failed, true);119 });182 unmodified lines