Fix `verbose` discarding IPC messages when its function throws
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 59de065 · Browse files · 6 files·+60 added−10 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+60 added−10 removed
- tests:+40 added−0 removed
- other:+20 added−10 removed
Provenance
- Commit
b548d8b- Tree
6a6543b- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
59de065
6 files changed+60 added−10 removed
modified:docs/debugging.md+2 added
114 unmodified lines115 It is called once per log line. The first argument is the default log line string. The second argument is the same information but as an object instead (documented [here](api.md#verbose-object)).116 117 If a string is returned, it is printed on `stderr`. If `undefined` is returned, nothing is printed.118 119+If the function throws, the subprocess fails with that error as [`error.cause`](api.md#errorcause). If it threw while logging the output of [`stdout`](api.md#resultstdout), [`stderr`](api.md#resultstderr) or another file descriptor, that file descriptor's output is then not kept.120+121 ### Filter logs122 123 ```js124 import {execa as execa_} from 'execa';80 unmodified linesmodified:lib/io/contents.js+4 added-3 removed
109 unmodified linesExpand all110111112113114114115116115116117109 unmodified linesExpand all }};
// Ensure we are returning Uint8Arrays when using `encoding: 'buffer'`// The error might be any value thrown by the `verbose` function, including `null` or `undefined`const handleBufferedData = ({bufferedData}) => isArrayBuffer(bufferedData) ? new Uint8Array(bufferedData) : bufferedData;const handleBufferedData = error => isArrayBuffer(error?.bufferedData) ? new Uint8Array(error.bufferedData) : error?.bufferedData;modified:lib/ipc/buffer-messages.js+11 added-1 removed
17 unmodified lines181920212223242526277 unmodified lines3536373839374041424344454647484950515253543 unmodified lines17 unmodified lines
const isVerbose = shouldLogIpc(verboseInfo); const buffer = getFdSpecificValue(bufferArray, 'ipc'); const maxBuffer = getFdSpecificValue(maxBufferArray, 'ipc'); // A throwing `verbose` function must not prevent the following messages from being buffered const logErrors = [];
for await (const message of loopOnMessages({ anyProcess: subprocess, channel: subprocess.channel,7 unmodified lines ipcOutput.push(message); }
if (isVerbose) { try { logIpcOutput(message, verboseInfo); logIpcOutput(message, verboseInfo); } catch (error) { logErrors.push(error); } } }
if (logErrors.length > 0) { throw logErrors[0]; }
return ipcOutput;};
export const getBufferedIpcOutput = async (ipcOutputPromise, ipcOutput) => {3 unmodified linesmodified:lib/verbose/log.js+3 added-6 removed
41 unmodified lines42 const verboseLine = defaultVerboseFunction(verboseObject);43 return {verboseLine, verboseObject};44 };45 46-// Serialize any type to a line string, for logging46+// Serialize any type to a line string, for logging.47+// Tabs are escaped by `escapeLines()`, like any other control character.48 export const serializeVerboseMessage = message => {49 const messageString = typeof message === 'string' ? message : inspect(message);49- const escapedMessage = escapeLines(messageString);50- return escapedMessage.replaceAll('\t', () => ' '.repeat(TAB_SIZE));50+ return escapeLines(messageString);51 };52-53-// Same as `util.inspect()`54-const TAB_SIZE = 2;modified:test/verbose/custom-throw.js+35 added
1 import test from 'ava';2+import {execa} from '../../index.js';3 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';4 import {foobarString} from '../helpers/input.js';5 import {runVerboseSubprocess} from '../helpers/verbose.js';6 import {earlyErrorOptions, earlyErrorOptionsSync} from '../helpers/early-error.js';58 unmodified lines65 66 test('Propagate wrapped exception in "verbose" function, "output", spawn error', testCommandThrowWrap, 'output', earlyErrorOptions, false);67 test('Propagate wrapped exception in "verbose" function, "ipc", spawn error', testCommandThrowWrap, 'ipc', earlyErrorOptions, false);68 test('Propagate wrapped exception in "verbose" function, "output", spawn error, sync', testCommandThrowWrap, 'output', earlyErrorOptionsSync, true);69+70+// A throwing `verbose` function is a logging error: it must not discard IPC messages which have already been buffered71+const getThrowingVerbose = (type, thrownValue = new Error(foobarString)) => ({72+ verbose: (line, verboseObject) => {73+ if (verboseObject.type === type) {74+ throw thrownValue;75+ }76+ },77+});78+79+test('IPC output is kept when the "verbose" function throws, "ipc"', async t => {80+ const {cause, ipcOutput} = await t.throwsAsync(execa('ipc-send-twice.js', {ipc: true, ...getThrowingVerbose('ipc')}));81+ t.is(cause.message, foobarString);82+ t.deepEqual(ipcOutput, ['foo', 'bar']);83+});84+85+// A `verbose` function might throw a non-Error, or a falsy value. Neither must be mistaken for no error at all86+test('A non-Error thrown by the "verbose" function is propagated, "output"', async t => {87+ const {cause, stdout} = await t.throwsAsync(execa('noop.js', [foobarString], getThrowingVerbose('output', foobarString)));88+ t.is(cause, foobarString);89+ t.is(stdout, undefined);90+});91+92+test('A null value thrown by the "verbose" function is propagated, "output"', async t => {93+ const {cause, stdout} = await t.throwsAsync(execa('noop.js', [foobarString], getThrowingVerbose('output', null)));94+ t.is(cause, null);95+ t.is(stdout, undefined);96+});97+98+test('IPC output is kept when the "verbose" function throws a falsy value, "ipc"', async t => {99+ const {cause, ipcOutput} = await t.throwsAsync(execa('ipc-send-twice.js', {ipc: true, ...getThrowingVerbose('ipc', 0)}));100+ t.is(cause, 0);101+ t.deepEqual(ipcOutput, ['foo', 'bar']);102+});modified:test/verbose/output-enable.js+5 added
112 unmodified lines113 const {stderr} = await nestedSubprocess('noop.js', ['\u{1}'], {verbose: 'full'});114 t.is(getOutputLine(stderr), `${testTimestamp} [0] \\u0001`);115 });116 117+test('Escapes tabs from stdout', async t => {118+ const {stderr} = await nestedSubprocess('noop.js', [`${foobarString}\t${foobarString}`], {verbose: 'full'});119+ t.is(getOutputLine(stderr), `${testTimestamp} [0] ${foobarString}\\t${foobarString}`);120+});121+122 const testStdioSame = async (t, fdNumber) => {123 const {nestedResult: {stdio}} = await nestedSubprocess('noop-fd.js', [`${fdNumber}`, foobarString], {verbose: 'full'});124 t.is(stdio[fdNumber], foobarString);125 };24 unmodified lines