Skip to content

fix(executor): #846 - ! no longer flips a compound's own redirect failure - #902

Merged
caomengxuan666 merged 1 commit into
masterfrom
arookieofc/fix-846-bang-redirect-rc
Oct 19, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
arookieofc/fix-846-bang-redirect-rc

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Closes #846.

Top-level bang over a compound whose OWN redirect open fails must not flip: GNU execute_cmd.c:854-883 returns EXECUTION_FAILURE before the :1204-1207 invert step, so ! { :; } > /bad is rc 1. Same for subshell/if/loop/for/select/case/coproc/time/(( ))/[[ ]]; the pipeline level still always flips (:763).

How:

  • Leave-set Cell flag compound_redirect_failed: every compound-own redirect-open mark site sets it on IoError only (never ExpansionFailure/failglob); dispatch flip sites suppress the flip while set; pipeline flip sites always flip and consume; subshell-region completion flips+consumes iff the single body is inverted; function/eval/source/comsub/evalerror-depth-1 boundaries clear it.
  • Follow-on 1: ! [[ -n x ]] > /bad flipped to 0 because [[ ]] with redirects dispatches through �xecute_conditional_command_with_redirects (command_prepare.rs), never reaching the late.rs word arm. Mark added at the live path.
  • Follow-on 2: ! true | cat > /bad lost ALL later stdout - the stage redirect preflights propagated Err past an active thread-local stdout capture without restoring it. Restore before propagating (3 sites).

Verification:

  • 10-case WSL GNU Bash 5.3.0 script-file battery, stdout+stderr byte-identical.
  • New tests/issue846_bang_redirect_regressions.rs (9 tests).
  • cargo test --test regression 78/78 incl. upstream snapshots.
  • Full cargo test --no-fail-fast: failure set identical to clean HEAD (remaining failures are sandbox-environmental: missing external coreutils, signal/console limits, timing flakes).

Known follow-up (pre-existing, out of scope): a MIDDLE stage output-redirect failure aborts later stages (echo A | cat > bad | cat is rc 1, GNU rc 0).

GNU execute_cmd.c:854-883: a compound do_redirections failure returns
EXECUTION_FAILURE before the :1204-1207 invert step, so `! { :; } > /bad`
exits 1 (no flip). Same for `(( ))`, `[[ ]]`, subshell, if/loop/for/
select/case/coproc/time; the pipeline level still always flips (:763).

Port as a leave-set Cell flag `compound_redirect_failed` (mod.rs/init.rs,
fork-copied in command_substitution.rs): every compound-own redirect-open
mark site sets it on IoError only (never ExpansionFailure/failglob);
dispatch flip sites (inverted wrapper, bool path, time sites) suppress the
flip while set; pipeline flip sites always flip and consume; subshell-region
completion flips+consumes iff the single body is inverted; function/eval/
source/comsub/evalerror-depth-1 boundaries clear it.

Two follow-on fixes in this commit:
- `! [[ -n x ]] > /bad` flipped to 0: `[[ ]]` with redirects dispatches
  through execute_conditional_command_with_redirects (command_prepare.rs),
  never reaching the late.rs word arm that carried the only mark. Mark
  added at the live path.
- `! true | cat > /bad` printed the diagnostic then lost ALL later stdout:
  the stage redirect preflights propagated Err past an active thread-local
  stdout capture without restoring it. Restore before propagating in
  execute_pipeline_stage_inner, the builtin stage path, and the
  compound-stage failglob early return.

Verified: 10-case WSL GNU Bash 5.3.0 script-file battery (brace/subsh/
if/for/while/cond/arith/pipe/lastelem/and/or/coproc/async/comsub/time
forms - stdout+stderr byte-identical), tests/issue846 (9 tests),
cargo test --test regression 78/78 incl. upstream snapshots. Full
cargo test --no-fail-fast matches the clean-HEAD failure set exactly
(all remaining failures are sandbox-environmental: missing external
coreutils, signal/console limits, timing flakes).

Known follow-up (pre-existing, out of scope): a MIDDLE stage
output-redirect failure aborts later stages (`echo A | cat > bad | cat`
is rc 1, GNU rc 0) - the preflight Err propagation predates this work.
@github-actions

Copy link
Copy Markdown
Contributor

欢迎!这是你的第一个 PR 🎉 请确认已跑过本地测试套件;CI 通过后维护者会 review。

@caomengxuan666

Copy link
Copy Markdown
Member

独立评审裁决(#897 vs #902 同票双 PR):建议合入本 PR

本 PR 与 #897 同为 closes #846。按双分支自建 worktree 实测裁决:合入 #902,#897 以 superseded 关闭。方法:master / #897 / #902 三树自建构建(6e600e5a / bbf06a0,debug),oracle 为 WSL GNU bash 5.3.0(1),60 形矩阵(52 计划形 + 8 边界探针形)rc+stdout+stderr 逐字节比对(程序名前缀与 time 计时行归一化)。对侧裁决评论见 #897。

本 PR 实测结果

60 形中除两处双方共有且预存的面外全部与 GNU 5.3.0 一致:

  1. 中段 stage 输出重定向失败中止后续阶段(echo A | cat > BAD | cat:GNU rc 0,三树 rc 1)——PR body 已如实披露为预存 follow-up,与 顶层 ! { :; } > /bad rc 分歧:GNU rc=1 vs rubash rc=0(子壳形一致)——#842 CR N2 #846 无关,建议单独开票跟踪;
  2. ! time true > BAD 时 time 未打印 real/user/sys 计时(预存表层差异,rc 一致)。

对照 #897 的实测分歧面(仅列差异形,BAD = 必然 ENOENT 路径):

形 GNU 5.3.0 master #897 #902
! coproc true > BAD 1 0 0 ✗ 未覆盖 1 ✓
{ :; } > BAD; ! false 0 0 1 ✗ 回退 0 ✓
( ! { :; } > BAD ) 0 0 1 ✗ 回退 0 ✓
! ( { :; } > BAD ) 0 0 1 ✗ 回退 0 ✓
f() { { :; } > BAD; }; ! f 0 0 1 ✗ 回退 0 ✓
set -e; ( ! { :; } > BAD ); echo cont=$? cont=0, rc 0 同 GNU rc 1 ✗ 回退 同 GNU ✓
eval '{ :; } > BAD'; ! { false; } 0 0 1 ✗ 回退 0 ✓
( { :; } > BAD ); ! { false; } 0 0 1 ✗ 回退 0 ✓
f() { { :; } > BAD; }; f; ! { false; } 0 0 1 ✗ 回退 0 ✓
echo pre; ! true | cat > BAD; echo post pre+post 仅 pre 仅 pre pre+post ✓(capture 恢复修复)

判定依据

  • 机制与 GNU 结构逐点对应:mark 只在复合自身 do_redirections 失败裁定点置位(IoError only);flip 点查而不清(! ! C 双层抑制,实测 GNU 1 = 三树 1);pipeline wait-side(:763)恒翻转并消费(! true \| cat > BAD = 0 实测一致);subshell 区域单体 inverted 提升翻转(:1831,( ! C > BAD ) = 0);function/eval/source/comsub 边界清除;每命令回扫清除兜底——fix(executor): #846 — '!' must not invert a compound's own redirect failure #897 无边界生命周期,标志跨命令滞留造成上表 8 个回退(5 个为 master 原本正确的形态)。
  • 附带修复实测确认:master 上 echo pre; ! true | cat > BAD; echo post 丢失全部后续 stdout,本 PR 恢复(3 处 restore_stdout_capture)。
  • errexit/ERR trap 交互面(set -e 下 ! C > BAD 继续执行、invert 门控 ERR trap)与 ! 管道翻转让步面(! true > BAD = 0、! false | true = 1)实测与 GNU 一致;与 fix(executor): mark_quoted_ifs_from_raw recognizes markers.rs carrier bytes (#868) #874(fix/868,command_prepare.rs 重改)无冲突——该合并已在两分支共同基底之内。
  • 单提交基于当前 master、CI 7 项绿、本地新测试 9/9 绿(worktree 实测)。

非阻塞建议

  1. tests/issue846_bang_redirect_regressions.rs 择机补上表 8 个序列/边界回归面(今日行为正确,测试锁住以免后续重构回退);fix(executor): #846 — '!' must not invert a compound's own redirect failure #897 的 errexit 直面用例亦可并入。
  2. 中段 stage follow-up 开独立票(勿与 顶层 ! { :; } > /bad rc 分歧:GNU rc=1 vs rubash rc=0(子壳形一致)——#842 CR N2 #846 混闭)。

两 PR 在 command_prepare.rs / compound_exec.rs 等九个文件文本重叠:本 PR 合入后 #897 无法干净合入,应关闭而非重排。

@caomengxuan666
caomengxuan666 merged commit 463f414 into master Oct 19, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the arookieofc/fix-846-bang-redirect-rc branch October 21, 2026 07:58
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.

顶层 ! { :; } > /bad rc 分歧:GNU rc=1 vs rubash rc=0(子壳形一致)——#842 CR N2

2 participants