Fix `execaSync()` output transforms
Before adoption: this commit came in with the history the project adopted, so no landing records it.
With a binary encoding, no line is split, so transforms wrongly ran on the empty output when the subprocess produced none. That empty chunk is now not passed to them. A transform failing on one file descriptor also prevented the other ones from being redirected to their target. Those errors are now collected per file descriptor, so each one keeps writing its own files.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 004f3b3 · Browse files · 3 files·+58 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+58 added−17 removed
- tests:+39 added−1 removed
- other:+19 added−16 removed
Provenance
- Commit
e0b5dbc- Tree
08046c4- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
004f3b3
3 files changed+58 added−17 removed
modified:lib/io/output-sync.js+19 added-16 removed
11 unmodified lines1213141516161718192021222323242526272728293031313233343536373839404141424344454647484950515251535455545657585960616263646566676468697071726970737475767778797680817882838081848586878815 unmodified lines1041051061071051081091101111126 unmodified lines11912012112212012312412512612726 unmodified lines11 unmodified lines if (output === null) { return {output: Array.from({length: 3})}; }
const state = {}; 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) => transformOutputResultSync({ result, fileDescriptors, fdNumber, state, errors, outputFiles, verboseInfo, }, options)); return {output: transformedOutput, ...state}; return {output: transformedOutput, error: errors.find(error => error !== undefined)};};
const transformOutputResultSync = ( {result, fileDescriptors, fdNumber, state, outputFiles, verboseInfo}, {result, fileDescriptors, fdNumber, errors, outputFiles, verboseInfo}, {buffer, encoding, lines, stripFinalNewline, maxBuffer},) => { if (result === null) { return; }
const truncatedResult = truncateMaxBufferSync(result, fdNumber, {maxBuffer, buffer}); const uint8ArrayResult = bufferToUint8Array(truncatedResult); const {stdioItems, objectMode} = fileDescriptors[fdNumber]; const chunks = runOutputGeneratorsSync([uint8ArrayResult], stdioItems, encoding, state); // Empty output must not be passed as a chunk: with a binary encoding, no line is split, so transforms would wrongly run on it const {chunks, error: transformError} = runOutputGeneratorsSync(uint8ArrayResult.length === 0 ? [] : [uint8ArrayResult], stdioItems, encoding); const {serializedResult, finalResult = serializedResult} = serializeChunks({ chunks, objectMode, encoding, lines, stripFinalNewline, fdNumber, });
logOutputSync({ const logError = logOutputSync({ serializedResult, fdNumber, state, verboseInfo, encoding, stdioItems, objectMode, });
const error = transformError ?? logError; errors.push(error);
const returnedResult = buffer[fdNumber] ? finalResult : undefined;
try { if (state.error === undefined) { if (error === undefined) { writeToFiles(serializedResult, stdioItems, outputFiles); }
return returnedResult; } catch (error) { state.error = error; } catch (writeError) { errors.push(writeError); return returnedResult; }};
// Applies transform generators to `stdout`/`stderr`const runOutputGeneratorsSync = (chunks, stdioItems, encoding, state) => {const runOutputGeneratorsSync = (chunks, stdioItems, encoding) => { try { return runGeneratorsSync(chunks, stdioItems, encoding, false); return {chunks: runGeneratorsSync(chunks, stdioItems, encoding, false)}; } catch (error) { state.error = error; return chunks; return {chunks, error}; }};
// The contents is converted to three stages:15 unmodified lines
return {serializedResult};};
const logOutputSync = ({serializedResult, fdNumber, state, verboseInfo, encoding, stdioItems, objectMode}) => {const logOutputSync = ({serializedResult, fdNumber, verboseInfo, encoding, stdioItems, objectMode}) => { if (!shouldLogOutput({ stdioItems, encoding, verboseInfo,6 unmodified lines
try { logLinesSync(linesArray, fdNumber, verboseInfo); } catch (error) { state.error ??= error; return error; }};
// When the `std*` target is a file path/URL or a file descriptor26 unmodified linesmodified:test/transform/generator-error.js+22 added-1 removed
1 import {once} from 'node:events';2+import {readFile, rm} from 'node:fs/promises';3 import test from 'ava';4+import tempfile from 'tempfile';5 import {execa, execaSync} from '../../index.js';6 import {foobarString} from '../helpers/input.js';5-import {noopGenerator, infiniteGenerator, convertTransformToFinal} from '../helpers/generator.js';7+import {noopGenerator, infiniteGenerator, convertTransformToFinal, throwingGenerator} from '../helpers/generator.js';8 import {generatorsMap} from '../helpers/map.js';9 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';10 import {getEarlyErrorSubprocess, expectedEarlyError} from '../helpers/early-error.js';11 78 unmodified lines90 test('Generators are destroyed on early subprocess exit', async t => {91 const error = await t.throwsAsync(getEarlyErrorSubprocess({stdout: infiniteGenerator()}));92 t.like(error, expectedEarlyError);93 });94+95+// A transform failing on one file descriptor must not prevent the other ones from being redirected to their target96+const testTransformErrorOtherFd = async (t, execaMethod) => {97+ const filePath = tempfile();98+ const cause = new Error(foobarString);99+ const runSubprocess = () => execaMethod('noop-both.js', [foobarString], {100+ stdout: throwingGenerator(cause)(),101+ stderr: {file: filePath},102+ });103+104+ await (execaMethod === execa105+ ? t.throwsAsync(runSubprocess())106+ : t.throws(runSubprocess));107+ t.is(await readFile(filePath, 'utf8'), `${foobarString}\n`);108+ await rm(filePath);109+};110+111+test('Transform errors on stdout do not skip the stderr file', testTransformErrorOtherFd, execa);112+test('Transform errors on stdout do not skip the stderr file, sync', testTransformErrorOtherFd, execaSync);modified:test/transform/split-binary.js+17 added
90 unmodified lines91 test('Does not split lines when "binary" is undefined, encoding "hex", preserveNewlines, sync', testBinaryOption, undefined, simpleChunks, simpleChunksUint8Array, simpleFullHex, false, false, 'hex', execaSync);92 test('Does not split lines when "binary" is false, encoding "hex", preserveNewlines, sync', testBinaryOption, false, simpleChunks, simpleChunksUint8Array, simpleFullHex, false, false, 'hex', execaSync);93 test('Splits lines when "binary" is false, objectMode, preserveNewlines, sync', testBinaryOption, false, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false, 'utf8', execaSync);94 test('Splits lines when "binary" is undefined, objectMode, preserveNewlines, sync', testBinaryOption, undefined, simpleChunks, noNewlinesChunks, noNewlinesChunks, true, false, 'utf8', execaSync);95+96+// With a binary encoding, no line is split, so transforms must not run when the subprocess produced no output at all97+const bracketsGenerator = function * () {98+ yield '[]';99+};100+101+const testNoOutputBinary = async (t, execaMethod) => {102+ const {stdout} = await execaMethod('empty.js', {103+ stdout: [bracketsGenerator],104+ encoding: 'hex',105+ stripFinalNewline: false,106+ });107+ t.is(stdout, '');108+};109+110+test('Does not run transforms on empty output with a binary encoding', testNoOutputBinary, execa);111+test('Does not run transforms on empty output with a binary encoding, sync', testNoOutputBinary, execaSync);