Skip to content

fix(#553): compound's own failed redirection fires ERR again - unblock redir snapshot - #557

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/redir-golden-553-followup
Oct 12, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/redir-golden-553-followup

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Root cause

PR #553's #507 fix gates BOTH post-execute consumers of a command's status
(the ERR trap fire and the errexit promotion) on
command_status_produced_by_body() — a node-kind test that exempts
if/while/until/for/case/select compounds because their nonzero
aggregate is usually inherited from an exempt inner failure (a
&&/|| short-circuit arm or a ! pipeline) that was already adjudicated
in the nested walk.

The gate cannot see one flavor: the compound's own failed redirection.
That failure surfaces as ExecuteError::IoError from the redirect wrapper,
the walker's IoError arm falls through to the same gated ERR site, and the
kind-based skip silenced the trap. Concretely, redir12.sub (upstream
redirection suite, vendored under third_party/bash):

trap 'echo got error ERR' ERR
while [ -z "x" ]; do x=4; done </dev/null > unwritable-file
echo after ERR trap: $?

stopped printing got error ERR — upstream_suite_slice_snapshots redir
snapshot lost exactly that one byte, master went red
(CI run 38195839266), all PRs blocked. This is also the case #553's own
commit message promised would keep adjudicating ("a compound's own failed
redirection — the wrapper's failure"); the promise held only for node kinds
outside the gate list.

GNU semantics (anchor + behavior)

GNU anchors the ERR trap and the errexit jump for a compound's own
redirection failure at the compound-command redirection setup in
execute_cmd.c (the "Handle WHILE FOR CASE etc. with redirections" path,
execute_cmd.c:849-884 in the vendored bash-5.3-16-gb4608166 mirror): the
trap state is latched before dispatch, the failure status is set, the ERR
trap runs at the compound level, then errexit exits. The upstream
redir.right expectation pins the stdout: got error ERR + after ERR trap: 1.

That path is orthogonal to what #507 fixed: an exempt inner failure reaches
the compound as a body-inherited aggregate with no wrapper failure, and
GNU's execute_if_command family hands that status up with no ERR/errexit
look of its own. One node-kind gate cannot distinguish the two flavors, so
the fix re-enables adjudication for the wrapper-failure flavor only.

WSL GNU 5.3.0 behavioral cross-check (stdout byte-equal with this branch
after the fix):

case GNU rubash fixed
trap 'echo got error ERR' ERR; while … done </dev/null > unwritable got error ERR / after ERR trap: 1 identical
set -e; if true; then false && true; fi; echo R (#507 guard) R, rc 0 identical
set -e; trap 'echo E' ERR; false E, rc 1 identical

Fix

One hunk in src/executor/ast_exec.rs (+16): the walker's
ExecuteError::IoError arm now fires maybe_run_error_trap when the failed
node is a pass-through compound kind. maybe_run_error_trap's own gates
(inverted, &&/|| context, errexit-suppression, errtrace depth,
recursion guard) keep every #507 exclusion, and the gated post-site stays
skipped for these kinds, so nothing double-fires. No golden or snapshot
bytes touched — redir.snap passes byte-identical to the recorded
baseline.

Test evidence

  • cargo test --test regression upstream_suite_slice_snapshots: green
    (was the master red); all 13 suites pass, redir.snap unchanged.
  • Full cargo test --no-fail-fast, run serially on this branch and on
    pristine master (f33c5e3) in identical conditions; failure sets compared
    name-by-name bidirectionally: the only delta is
    upstream_suite_slice_snapshots (red on master, green here) — no other
    test flips in either direction (68 pre-existing failures, identical sets).
  • fix: four P1-high semantic divergences - errexit body adjudication, stdin parse-error status, printf write-failure status, dirstack EX_USAGE #553's 20 new tests stay green: issue507 x7, issue508 x5, issue509 x3,
    issue510 x5; the part_025 dirs companions (test_dirs_redirects_stderr,
    test_dirs_appends_stderr) stay green.
  • cargo fmt --check clean.

Fix A without breaking B: the #507 guard suite and the redir snapshot are
green simultaneously.

…k redir snapshot

PR #553's #507 pass-through gate (command_status_produced_by_body) exempted
if/loop/for/case/select compounds from the post-execute ERR adjudication,
and the exemption leaked one flavor it must not cover: the compound's OWN
failed redirection. redir12.sub's
`trap 'echo got error ERR' ERR; while ...; done </dev/null > unwritable-file`
stopped printing "got error ERR", so upstream_suite_slice_snapshots went
red on master (CI run 38195839266) and every PR was blocked. The wrapper's
open() failure surfaces as ExecuteError::IoError; the walker's IoError arm
falls through to the same gated ERR site the #507 commit added its skip at,
so the trap went silent for exactly the kinds whose redirects the wrapper
owns.

Semantics (GNU oracle, vendored bash-5.3-16-gb4608166): GNU runs the ERR
trap and the errexit jump at the compound level when the compound's own
redirection fails (execute_cmd.c:849-884 - the "WHILE FOR CASE etc. with
redirections" path latches was_error_trap before dispatch, sets the failure
status, runs the trap, then exits under errexit; the upstream redir.right
expectation pins the behavior as "got error ERR" + "after ERR trap: 1").
That path is ORTHOGONAL to the one #507 fixed: an exempt inner failure (a
`&&`/`||` short-circuit arm or a `!` pipeline) reaches the compound as a
body-inherited AGGREGATE with no wrapper failure, and GNU's
execute_if_command family still hands that status up with no ERR/errexit
look of its own. One gate keyed on node kind cannot see the difference -
this restores the adjudication for the wrapper-failure flavor only.

Fix: in the walker's ExecuteError::IoError arm, fire maybe_run_error_trap
when the failed node is a pass-through compound kind. maybe_run_error_trap's
own gates (inverted, &&/|| context, suppress, errtrace depth, recursion)
keep every #507 exclusion, and the gated post-site stays skipped for these
kinds, so nothing double-fires.

GNU cross-check (WSL 5.3.0, stdout byte-equal after the fix): the repro
prints `got error ERR` + `after ERR trap: 1`; #507's guard
`set -e; if true; then false && true; fi; echo R` still prints R with
rc 0; `set -e; trap 'echo E' ERR; false` still prints E and exits 1.

No golden or snapshot bytes touched: redir.snap passes unchanged against
the recorded baseline.

Tests: cargo test --test regression upstream_suite_slice_snapshots green;
full serial cargo test --no-fail-fast vs pristine master (f33c5e3),
failure sets compared name-by-name bidirectionally - the only delta is
upstream_suite_slice_snapshots (red on master, green here), no other test
flips either way; #553's 20 new tests (issue507 x7, issue508 x5, issue509
x3, issue510 x5) and the part_025 dirs companions stay green; cargo fmt
clean.
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