Repository navigation
fix(trap): do not fire the ERR trap for word/assignment expansion errors - #467
Merged
Merged
Conversation
…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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inexecute_simple_command). A word/assignment expansion error abandons the command viaexp_jump_to_top_level(DISCARD)(subst.cexpand_wdesc_error) before it executes, so the trap never fires:GNU prints
bad: invalid indirect expansionthenafter— noERR-FIRED. Rubash used to print an extraERR-FIRED.Change
command_expansion_failedmark is set by the word/assignment expansion error sites (theparameter_expansion_errorpre-scan includinginvalid indirect expansion, bad substitution, failglob, heredoc and arithmetic expansion-error conversions), reset perexecute_astloop iteration, and consulted bymaybe_run_error_trap.( )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 -Eerrtrace 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}; falseon 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):
bash -clist after an expansion error (x=${!bad}; echo afterprints noafter, rc 1), while rubash keeps reading the next line. In the multi-line repro form from the issue both agree (afterprints, rc 0).cat < ${!bad}: GNUbad: 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) — greenissue343_345_trap_regressions,issue70_subshell_trap_regressions,host_internal_trap_suppression— greenissue154,issue334,issue337_342,issue428,parameter_transform_tests,o1_parse_expand_exec_regressions, assignment regressions) — green except 4parameter_transform_tests+ 1pwdlib failure that reproduce identically on unmodifiedorigin/master(environment-dependent, unrelated)cargo fmt --checkcleancloses #439