Skip to content

fix(core): show stdout tail in exec() failure message when stderr is empty - #976

Open
Larslllllll wants to merge 1 commit into
profullstack:masterfrom
Larslllllll:fix/exec-error-tail-stdout
Open

fix(core): show stdout tail in exec() failure message when stderr is empty#976
Larslllllll wants to merge 1 commit into
profullstack:masterfrom
Larslllllll:fix/exec-error-tail-stdout

Conversation

@Larslllllll

Copy link
Copy Markdown
Contributor

Problem

exec()'s non-zero-exit error message picks its tail with:

const tail = stderr.trim().split('\n').pop() ?? stdout.trim().split('\n').pop() ?? '';

But ''.trim().split('\n') is [''], so .pop() returns ''not undefined. The ?? fallback to stdout never fires.

Many CLIs write their diagnostics to stdout (and nothing to stderr), so failures looked like:

sh -c echo useful-diagnostic-on-stdout; exit 3 failed (exit 3): 

— the actual diagnostic was dropped from the error the user sees.

Verified before this patch

> exec('sh', ['-c', 'echo useful-diagnostic-on-stdout; exit 3'])
Error: sh -c ... failed (exit 3):         // stdout content missing

Fix

Pick the last non-empty stderr line; fall back to the last non-empty stdout line.

Error: sh -c echo useful-diagnostic-on-stdout; exit 3 failed (exit 3): useful-diagnostic-on-stdout

Tests

  • empty stderr → stdout tail appears in the message
  • multi-line stderr → last non-empty stderr line wins

…empty

The error tail used `stderr.trim().split('\n').pop() ?? stdout…pop()`,
but pop() on the single-element array [''] returns '' — not undefined —
so the ?? fallback never fired and commands that fail while writing
their diagnostics to stdout (very common for CLIs) produced errors like:

    git … failed (exit 128):

Now the last non-empty stderr line is preferred, with the last
non-empty stdout line as the actual fallback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant