Skip to content

fix(executor,lexer): #940 - redirect-failure diagnostics through ordered fd table; !+{ inside groups parses - #976

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/940
Oct 21, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/940

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Fixes #940 — the residual faces of the #826/#846 redirect-failure family:

  • R1 — redirect-failure diagnostic bypasses the ordered fd table on ( )/simple faces: ( :; ) 2>cap.txt >bad never performed the earlier 2> open and reported to real stderr; true 2>cap.txt >bad opened 2> but still reported to real stderr. Root cause: apply_command_output_redirects_inner ran the four stdio arms in FIELD order (out before err) instead of GNU do_redirection_internal's parse-order walk (redir.c:767-955), and the open-failure reports bypassed the ordered fd-2 routing (redirection_error, redir.c:135/149-176). Fix: arms now run in command.redirects parse order (same contract as apply_brace_group_redirects); a new failing_open_redirect_index extends write_redirect_diagnostic_routed's {target}: head-match to output-side opens so every compound/dispatch IoError path reports through the fd-2 binding in force at the failing entry (incl. exec 2>file persistent bindings and !/time/& wrapper unwrapping).
  • R2 — ! + compound + redirect failure inside a group aborts the script: { ! { :; } >bad; echo inner=$?; } died rc 1. Root cause: skip_brace's prev_accepts_close tracker consumed ! as non-word content, so a { after ! was never a standalone opener and the inner } closed the outer group — the fold produced an unbalanced collapsed token → EOF_COMPOUND parse error → >bad bound to the outer node. GNU: ! is BANG (parse.y:2378 alist) and BANG is in reserved_word_acceptable (parse.y:5913), so the boundary stays open. Fixed by treating a standalone ! at a command boundary as a boundary token.
  • Bonus fix on the same path: execute_subshell_command_with_redirects early-returned on apply failure without restoring the saved child state — leaked subshell_depth and the reset_for_subshell_child env mutation into the parent.

Verification

  • scripts/audit/nested-context-probes.py 56 probes: divergences 9 → 0 (oracle leg GNU_CMD=D:/Git/bin/bash.exe MSYS2 GNU 5.2.37 — the documented non-WSL fallback; WSL unavailable to this runner).
  • Ticket surface byte-identical to GNU: brace=1 d1-nonempty, paren=1 d2-nonempty, true=0 d3-nonempty, group-cont d4-nonempty, diagnostic on real stderr, nested=1 survived=0, rc 0.
  • { ! { :; } >bad; echo inner; } and the multi-line { ! { :;\n}; echo; } now print inner=1 (previously rc 1 abort / rc 2 syntax error).
  • cargo test --test issue846_bang_redirect_regressions 9/9, cargo test --lib lexer:: 94/94, cargo test --test parser_tests 316/316.
  • issue-846 catalogue case verified byte-identical to GNU manually; manifest row promoted to must-match.

Test plan

  • 56-probe nested-context harness: divergences=0
  • Ticket probes byte-for-byte vs GNU
  • issue846 Rust regressions + lexer/parser suites green

Generated with Devin

…red fd table; `!`+`{` inside groups parses

R1: GNU redir.c:767-955 do_redirection_internal applies a compound's
redirect list in parse order and redir.c:135 redirection_error reports an
open failure through the fd-2 binding in force at the failing entry.
apply_command_output_redirects_inner ran its four stdio arms in field
order (out before err), so `( :; ) 2>cap.txt >bad` never performed the
earlier `2>` open (missing side effects, stale cap survived) and every
non-brace apply path reported through real stderr. The arms now run in
command.redirects parse order; failing_open_redirect_index extends
write_redirect_diagnostic_routed's `{target}: ` head-match to output-side
opens, and the ast_exec IoError arms route through it (unwrapping
`!`/`time`/`&` nodes), covering subshell/if/loop/case/select/simple/
function/exec/comsub faces.

R2: `{ ! { :; } >bad; echo inner; }` aborted the whole script: skip_brace
consumed `!` as non-word content so `{` after `!` was never a standalone
opener — the inner `}` closed the outer group, the fold produced an
unbalanced collapsed token and the parse failed EOF_COMPOUND with `>bad`
bound to the outer node. GNU: `!` is BANG (parse.y:2378 alist) and BANG
is in reserved_word_acceptable (parse.y:5913). A standalone `!` at a
command boundary now keeps the boundary open.

Also: execute_subshell_command_with_redirects early-returned on apply
failure without restoring the saved child state — leaked subshell_depth
and the reset_for_subshell_child env mutation.

Verified: nested-context-probes 56/56 divergences=0 (MSYS2 GNU 5.2.37
leg, WSL unavailable); ticket faces byte-identical to GNU;
issue846 tests 9/9, lexer 94/94, parser_tests 316/316.

Fixes #940

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant