Skip to content
Myr

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

6 files+96 added−24 removed

modified:lib/pipe/setup.js+6 added
modified:lib/pipe/validate.js+1 added-1 removed
added:lib/return/early-error.js+47 added
modified:lib/async.js+6 added-11 removed
1import {setMaxListeners} from 'node:events';2import childProcess from 'node:child_process';2import {spawn} from 'node:child_process';3import {normalizeArguments, handleArguments} from './arguments/options.js';4import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';4import {makeError, makeSuccessResult} from './return/error.js';5import {handleOutput} from './return/output.js';6import {handleEarlyError} from './return/early-error.js';6import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './stdio/async.js';7import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';8import {spawnedKill} from './exit/kill.js';9import {cleanupOnExit} from './exit/cleanup.js';10import {pipeToProcess} from './pipe/setup.js';11import {makeAllStream} from './stream/all.js';
5 unmodified lines
17 const stdioStreamsGroups = handleInputAsync(options);1819 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 }2526 const controller = new AbortController();27 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);
62 unmodified lines
modified:lib/sync.js+2 added-2 removed
1import childProcess from 'node:child_process';1import {spawnSync} from 'node:child_process';2import {normalizeArguments, handleArguments} from './arguments/options.js';3import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';4import {handleOutput} from './return/output.js';5import {handleInputSync, pipeOutputSync} from './stdio/sync.js';
4 unmodified lines
10 const stdioStreamsGroups = handleInputSync(options);1112 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});1718 if (!options.reject) {
49 unmodified lines
modified:test/return/early-error.js+34 added-10 removed
1import process from 'node:process';2import test from 'ava';3import {execa, execaSync} from '../../index.js';3import {execa, execaSync, $} from '../../index.js';4import {setFixtureDir} from '../helpers/fixtures-dir.js';56setFixtureDir();78const isWindows = process.platform === 'win32';9const ENOENT_REGEXP = isWindows ? /failed with exit code 1/ : /spawn.* ENOENT/;1011const earlyErrorOptions = {killSignal: false};12const getEarlyErrorProcess = options => execa('empty.js', {...earlyErrorOptions, ...options});13const getEarlyErrorProcessSync = options => execaSync('empty.js', {...earlyErrorOptions, ...options});14const expectedEarlyError = {code: 'ERR_INVALID_ARG_TYPE'};1516test('execaSync() throws error if ENOENT', t => {17 t.throws(() => {18 execaSync('foo');19 }, {message: ENOENT_REGEXP});20});2122const 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 lines
31test('child_process.spawn() early errors have correct shape', testEarlyErrorShape, true);32test('child_process.spawn() early errors have correct shape - reject false', testEarlyErrorShape, false);3334test('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});3738test('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});4243test('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});4647test('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});5152if (!isWindows) {
15 unmodified lines
68 t.is(error.code, 'EPIPE');69 }70 });71}7273const testEarlyErrorPipe = async (t, getChildProcess) => {74 await t.throwsAsync(getChildProcess(), expectedEarlyError);75};7677test('child_process.spawn() early errors on source can use .pipe()', testEarlyErrorPipe, () => getEarlyErrorProcess().pipe(execa('empty.js')));78test('child_process.spawn() early errors on destination can use .pipe()', testEarlyErrorPipe, () => execa('empty.js').pipe(getEarlyErrorProcess()));79test('child_process.spawn() early errors on source and destination can use .pipe()', testEarlyErrorPipe, () => getEarlyErrorProcess().pipe(getEarlyErrorProcess()));80test('child_process.spawn() early errors can use .pipe() multiple times', testEarlyErrorPipe, () => getEarlyErrorProcess().pipe(getEarlyErrorProcess()).pipe(getEarlyErrorProcess()));81test('child_process.spawn() early errors can use .pipe``', testEarlyErrorPipe, () => $(earlyErrorOptions)`empty.js`.pipe(earlyErrorOptions)`empty.js`);82test('child_process.spawn() early errors can use .pipe`` multiple times', testEarlyErrorPipe, () => $(earlyErrorOptions)`empty.js`.pipe(earlyErrorOptions)`empty.js`.pipe`empty.js`);8384const testEarlyErrorStream = async (t, getStreamProperty, all) => {85 const childProcess = getEarlyErrorProcess({all});86 getStreamProperty(childProcess).on('end', () => {});87 await t.throwsAsync(childProcess);88};8990test('child_process.spawn() early errors can use .stdin', testEarlyErrorStream, ({stdin}) => stdin, false);91test('child_process.spawn() early errors can use .stdout', testEarlyErrorStream, ({stdout}) => stdout, false);92test('child_process.spawn() early errors can use .stderr', testEarlyErrorStream, ({stderr}) => stderr, false);93test('child_process.spawn() early errors can use .stdio', testEarlyErrorStream, ({stdio}) => stdio[1], false);94test('child_process.spawn() early errors can use .all', testEarlyErrorStream, ({all}) => all, true);