Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/core/src/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ describe('exec', () => {
'quoted "value"',
]);
});

it('falls back to the last stdout line in the failure message when stderr is empty', async () => {
await expect(exec('sh', ['-c', 'echo useful-diagnostic-on-stdout; exit 3'], {
log: () => {},
throwOnNonZero: true,
})).rejects.toThrow('failed (exit 3): useful-diagnostic-on-stdout');
});

it('prefers the last non-empty stderr line in the failure message', async () => {
await expect(exec('sh', ['-c', 'echo noisy >&2; echo real-error >&2; exit 2'], {
log: () => {},
throwOnNonZero: true,
})).rejects.toThrow('failed (exit 2): real-error');
});
});

describe('ensureCli', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ export async function exec(cmd: string, args: string[], opts: ExecOptions): Prom
child.on('close', (exitCode) => {
const result: ExecResult = { exitCode: exitCode ?? -1, stdout, stderr };
if (throwOnNonZero && result.exitCode !== 0) {
const tail = stderr.trim().split('\n').pop() ?? stdout.trim().split('\n').pop() ?? '';
// Prefer the last non-empty stderr line; fall back to stdout. Note
// `''.trim().split('\n').pop()` returns '' (not undefined), so a plain
// ??-chain never reaches the stdout fallback when stderr is empty.
const lastLine = (s: string) => s.trim().split('\n').filter(Boolean).pop() ?? '';
const tail = lastLine(stderr) || lastLine(stdout);
reject(new Error(`${cmd} ${args.join(' ')} failed (exit ${result.exitCode}): ${tail}`));
} else {
resolve(result);
Expand Down
Loading