Skip to content

fix(trap): do not fire the ERR trap for word/assignment expansion errors - #467

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/err-trap-indirect
Oct 9, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/err-trap-indirect

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Fixes #439: the ERR trap no longer fires for a word/assignment expansion error.

GNU fires the ERR trap only for a simple command that executed and failed (run_error_trap callers, execute_cmd.c:773/874/1000/1166, sit below expansion in execute_simple_command). A word/assignment expansion error abandons the command via exp_jump_to_top_level(DISCARD) (subst.c expand_wdesc_error) before it executes, so the trap never fires:

trap 'echo ERR-FIRED' ERR
x=${!bad}
echo after

GNU prints bad: invalid indirect expansion then after — no ERR-FIRED. Rubash used to print an extra ERR-FIRED.

Change

  • A command_expansion_failed mark is set by the word/assignment expansion error sites (the parameter_expansion_error pre-scan including invalid indirect expansion, bad substitution, failglob, heredoc and arithmetic expansion-error conversions), reset per execute_ast loop iteration, and consulted by maybe_run_error_trap.
  • The ( ) subshell wrapper clears and restores the mark: a child's expansion mark dies with the fork, and the parent judges a subshell by its exit status — GNU still fires ERR for (x=${!bad}).

Failure classes that must keep firing are pinned by the new tests/issue439_err_trap_indirect_expansion.rs (11 tests): plain command failure, set -E errtrace function inheritance, command-substitution failure in an assignment (x=$(false)), subshell expansion death, arithmetic failure ((( 1/0 ))), EXIT trap independence, and per-command mark scoping (x=${!bad}; false on separate lines still fires).

Oracle

WSL unavailable; oracle was Git for Windows GNU Bash 5.2.37 (msys). All ERR-trap trigger semantics fixed here are stable 5.x behavior, so 5.2 is a valid comparison.

Known 5.2 divergences left untouched (pre-existing, not part of #439's trap semantics):

  • GNU 5.2 abandons the remainder of a single-line semicolon-joined bash -c list after an expansion error (x=${!bad}; echo after prints no after, rc 1), while rubash keeps reading the next line. In the multi-line repro form from the issue both agree (after prints, rc 0).
  • The redirection-expansion diagnostic text differs (cat < ${!bad}: GNU bad: invalid indirect expansion, rubash ${!bad}: ambiguous redirect); the trap behavior matches (ERR fires).

Test plan

  • cargo test --test issue439_err_trap_indirect_expansion (11 new tests) — green
  • issue343_345_trap_regressions, issue70_subshell_trap_regressions, host_internal_trap_suppression — green
  • Expansion/assignment regression suites (issue154, issue334, issue337_342, issue428, parameter_transform_tests, o1_parse_expand_exec_regressions, assignment regressions) — green except 4 parameter_transform_tests + 1 pwd lib failure that reproduce identically on unmodified origin/master (environment-dependent, unrelated)
  • difftest suite: 29/29 PASS; cargo fmt --check clean

closes #439

…ors (fixes #439)

GNU fires the ERR trap only for a simple command that executed and
failed (run_error_trap callers, execute_cmd.c:773/874/1000/1166, sit
below expansion in execute_simple_command). A word/assignment expansion
error abandons the command via exp_jump_to_top_level(DISCARD) before it
executes, so `trap 'echo ERR-FIRED' ERR; x=${!bad}` must print only the
diagnostic. Rubash classified the expansion failure as a command failure
and fired the trap.

A command_expansion_failed mark is now set by the word/assignment
expansion error sites (parameter_expansion_error pre-scan, bad
substitution, failglob, heredoc and arithmetic expansion error
conversions), reset per execute_ast loop iteration, and consulted by
maybe_run_error_trap. The subshell wrapper clears and restores it: a
child's expansion mark dies with the fork, and the parent judges a
subshell by its exit status (GNU: (x=${!bad}) still fires ERR).

Failure classes that must keep firing are pinned by
tests/issue439_err_trap_indirect_expansion.rs: plain command failure,
set -E errtrace inheritance, command substitution failure in an
assignment, subshell expansion death, arithmetic failure, EXIT trap
independence, and per-command mark scoping.

Oracle: Git for Windows GNU Bash 5.2.37 (msys); WSL unavailable.
Known 5.2 divergences left untouched (pre-existing, noted in #439
discussion scope): GNU 5.2 abandons the remainder of a single-line
semicolon-joined -c list after an expansion error while rubash keeps
reading the next line, and the redirection-expansion diagnostic text
(ambiguous redirect vs invalid indirect expansion).
@caomengxuan666
caomengxuan666 merged commit d3f1bc5 into master Oct 9, 2026
7 of 8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/err-trap-indirect branch October 9, 2026 14:25
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.

[bug] ERR trap 在间接展开错误时被错误触发

1 participant