Improve spawning errors (#847)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 1315486 · Browse files · 6 files·+96 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+96 added−24 removed
- tests:+34 added−10 removed
- other:+62 added−14 removed
Provenance
- Commit
381dee2- Tree
8584d99- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
1315486
6 files changed+96 added−24 removed
modified:lib/pipe/setup.js+6 added
21 unmodified lines2223242526272829303121 unmodified lines } finally { maxListenersController.abort(); }};
export const dummyPipeToProcess = (source, destination) => { const promise = Promise.all([source, destination]); promise.pipe = dummyPipeToProcess.bind(undefined, promise); return promise;};modified:lib/pipe/validate.js+1 added-1 removed
10 unmodified lines11121314151516171819121 unmodified linesExpand all10 unmodified lines
const getDestinationStream = destination => { try { if (!isExecaChildProcess(destination)) { throw new TypeError('The first argument must be an Execa child process.'); throw new TypeError(`The first argument must be an Execa child process: ${destination}`); }
const destinationStream = destination.stdin; if (destinationStream === null) {121 unmodified linesExpand alladded:lib/return/early-error.js+47 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import {ChildProcess} from 'node:child_process';import {PassThrough} from 'node:stream';import {mergePromise} from '../promise.js';import {dummyPipeToProcess} from '../pipe/setup.js';import {cleanupStdioStreams} from '../stdio/async.js';import {makeAllStream} from '../stream/all.js';import {makeEarlyError} from './error.js';
// When the child process fails to spawn.// We ensure the returned error is always both a promise and a child process.export const handleEarlyError = ({error, command, escapedCommand, stdioStreamsGroups, options}) => { cleanupStdioStreams(stdioStreamsGroups); const spawned = createDummyProcess(options); const errorInstance = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options}); mergePromise(spawned, handleDummyPromise(errorInstance, options)); return spawned;};
const createDummyProcess = options => { const spawned = new ChildProcess(); createDummyStreams(spawned); spawned.all = makeAllStream(spawned, options); spawned.pipe = dummyPipeToProcess.bind(undefined, spawned); return spawned;};
const createDummyStreams = spawned => { const stdin = createDummyStream(); const stdout = createDummyStream(); const stderr = createDummyStream(); const stdio = [stdin, stdout, stderr]; Object.assign(spawned, {stdin, stdout, stderr, stdio});};
const createDummyStream = () => { const stream = new PassThrough(); stream.end(); return stream;};
const handleDummyPromise = async (error, {reject}) => { if (reject) { throw error; }
return error;};modified:lib/async.js+6 added-11 removed
1 import {setMaxListeners} from 'node:events';2-import childProcess from 'node:child_process';2+import {spawn} from 'node:child_process';3 import {normalizeArguments, handleArguments} from './arguments/options.js';4-import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';4+import {makeError, makeSuccessResult} from './return/error.js';5 import {handleOutput} from './return/output.js';6+import {handleEarlyError} from './return/early-error.js';6-import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './stdio/async.js';7+import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';8 import {spawnedKill} from './exit/kill.js';9 import {cleanupOnExit} from './exit/cleanup.js';10 import {pipeToProcess} from './pipe/setup.js';11 import {makeAllStream} from './stream/all.js';5 unmodified lines17 const stdioStreamsGroups = handleInputAsync(options);18 19 let spawned;20 try {20- spawned = childProcess.spawn(file, args, options);21+ spawned = spawn(file, args, options);22 } catch (error) {22- cleanupStdioStreams(stdioStreamsGroups);23- // Ensure the returned error is always both a promise and a child process24- const dummySpawned = new childProcess.ChildProcess();25- const errorInstance = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});23+ return handleEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});26- const errorPromise = options.reject ? Promise.reject(errorInstance) : Promise.resolve(errorInstance);27- mergePromise(dummySpawned, errorPromise);28- return dummySpawned;24 }25 26 const controller = new AbortController();27 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);62 unmodified linesmodified:lib/sync.js+2 added-2 removed
1-import childProcess from 'node:child_process';1+import {spawnSync} from 'node:child_process';2 import {normalizeArguments, handleArguments} from './arguments/options.js';3 import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';4 import {handleOutput} from './return/output.js';5 import {handleInputSync, pipeOutputSync} from './stdio/sync.js';4 unmodified lines10 const stdioStreamsGroups = handleInputSync(options);11 12 let result;13 try {14- result = childProcess.spawnSync(file, args, options);14+ result = spawnSync(file, args, options);15 } catch (error) {16 const errorInstance = makeEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});17 18 if (!options.reject) {49 unmodified linesmodified:test/return/early-error.js+34 added-10 removed
1 import process from 'node:process';2 import test from 'ava';3-import {execa, execaSync} from '../../index.js';3+import {execa, execaSync, $} from '../../index.js';4 import {setFixtureDir} from '../helpers/fixtures-dir.js';5 6 setFixtureDir();7 8 const isWindows = process.platform === 'win32';9 const ENOENT_REGEXP = isWindows ? /failed with exit code 1/ : /spawn.* ENOENT/;10 11+const earlyErrorOptions = {killSignal: false};12+const getEarlyErrorProcess = options => execa('empty.js', {...earlyErrorOptions, ...options});13+const getEarlyErrorProcessSync = options => execaSync('empty.js', {...earlyErrorOptions, ...options});14+const expectedEarlyError = {code: 'ERR_INVALID_ARG_TYPE'};15+16 test('execaSync() throws error if ENOENT', t => {17 t.throws(() => {18 execaSync('foo');19 }, {message: ENOENT_REGEXP});20 });21 22 const testEarlyErrorShape = async (t, reject) => {18- const subprocess = execa('', {reject});23+ const subprocess = getEarlyErrorProcess({reject});24 t.notThrows(() => {25 subprocess.catch(() => {});26 subprocess.unref();27 subprocess.on('error', () => {});3 unmodified lines31 test('child_process.spawn() early errors have correct shape', testEarlyErrorShape, true);32 test('child_process.spawn() early errors have correct shape - reject false', testEarlyErrorShape, false);33 34 test('child_process.spawn() early errors are propagated', async t => {30- const {failed} = await t.throwsAsync(execa(''));31- t.true(failed);35+ await t.throwsAsync(getEarlyErrorProcess(), expectedEarlyError);36 });37 38 test('child_process.spawn() early errors are returned', async t => {35- const {failed} = await execa('', {reject: false});39+ const {failed} = await getEarlyErrorProcess({reject: false});40 t.true(failed);41 });42 43 test('child_process.spawnSync() early errors are propagated with a correct shape', t => {40- const {failed} = t.throws(() => {41- execaSync('');42- });43- t.true(failed);44+ t.throws(getEarlyErrorProcessSync, expectedEarlyError);45 });46 47 test('child_process.spawnSync() early errors are propagated with a correct shape - reject false', t => {47- const {failed} = execaSync('', {reject: false});48+ const {failed} = getEarlyErrorProcessSync({reject: false});49 t.true(failed);50 });51 52 if (!isWindows) {15 unmodified lines68 t.is(error.code, 'EPIPE');69 }70 });71 }72+73+const testEarlyErrorPipe = async (t, getChildProcess) => {74+ await t.throwsAsync(getChildProcess(), expectedEarlyError);75+};76+77+test('child_process.spawn() early errors on source can use .pipe()', testEarlyErrorPipe, () => getEarlyErrorProcess().pipe(execa('empty.js')));78+test('child_process.spawn() early errors on destination can use .pipe()', testEarlyErrorPipe, () => execa('empty.js').pipe(getEarlyErrorProcess()));79+test('child_process.spawn() early errors on source and destination can use .pipe()', testEarlyErrorPipe, () => getEarlyErrorProcess().pipe(getEarlyErrorProcess()));80+test('child_process.spawn() early errors can use .pipe() multiple times', testEarlyErrorPipe, () => getEarlyErrorProcess().pipe(getEarlyErrorProcess()).pipe(getEarlyErrorProcess()));81+test('child_process.spawn() early errors can use .pipe``', testEarlyErrorPipe, () => $(earlyErrorOptions)`empty.js`.pipe(earlyErrorOptions)`empty.js`);82+test('child_process.spawn() early errors can use .pipe`` multiple times', testEarlyErrorPipe, () => $(earlyErrorOptions)`empty.js`.pipe(earlyErrorOptions)`empty.js`.pipe`empty.js`);83+84+const testEarlyErrorStream = async (t, getStreamProperty, all) => {85+ const childProcess = getEarlyErrorProcess({all});86+ getStreamProperty(childProcess).on('end', () => {});87+ await t.throwsAsync(childProcess);88+};89+90+test('child_process.spawn() early errors can use .stdin', testEarlyErrorStream, ({stdin}) => stdin, false);91+test('child_process.spawn() early errors can use .stdout', testEarlyErrorStream, ({stdout}) => stdout, false);92+test('child_process.spawn() early errors can use .stderr', testEarlyErrorStream, ({stderr}) => stderr, false);93+test('child_process.spawn() early errors can use .stdio', testEarlyErrorStream, ({stdio}) => stdio[1], false);94+test('child_process.spawn() early errors can use .all', testEarlyErrorStream, ({all}) => all, true);