Repository navigation
fix(executor): comsub reap publishes its status to $? before the enclosing command runs (rubash#485) - #494
Merged
Conversation
…osing command runs (rubash#485)
GNU command_substitute (subst.c:7143) reaps the substitution child in the
parent and the child's exit status becomes last_command_exit_value at that
moment - BEFORE the enclosing command runs - so every later expansion on
the same command line sees it, and a SUCCESSFUL substitution clobbers the
previous status just the same. Probes, WSL GNU bash 5.3.0
(/usr/local/bin/bash, 2026-10-10):
echo "$(cd nope && pwd)" $? -> " 1" (rubash: " 0" - the issue repro)
echo "$(exit 3)" $? -> " 3"
false; echo "$(true) $?" -> " 0" (unconditional reap contract)
true; echo $? "$(exit 3)" -> "0 " (left-to-right: $? BEFORE keeps old)
echo "$(exit 3)$?" -> "3" (same-word trailing fragment)
echo a`exit 3`b $? -> "ab 3" (embedded backtick word)
v="$(exit 3)$?" -> v=3, rc=3 (assignment keeps last comsub)
false; x=$(true) y=$? -> y=0, rc=0
f(){ local v="$(exit 3)$?"; } -> v=3, rc=0 (declaration builtin masks)
rubash recorded the substitution's status only in the
last_command_substitution_status cell and promoted it at the two
consumption sites (vanished-words null command, assignment RHS tail), so
the word-expansion path never published it: every mid-line $? read the
stale pre-expansion value.
Fix - publish the reaped status at each parent-side completion point:
- embedded_mutations.rs run_ast_command_substitution_with_context and
run_function_command_substitution tails: exit_code = status at the fork
boundary instead of restoring the pre-comsub value (the in-place
executor IS the parent side of GNU's waitpid; saved_exit_code dropped
exactly the status GNU publishes)
- embedded_mutations.rs expand_command_substitution_mut_typed_with_context:
the three fallbacks that run through the &self
expand_command_substitution_with_context wrapper (specialized cd/pwd
path incl. the #484 fast path, specialized builtins, final fallback)
promote the wrapper's status cell - the wrapper is &self and cannot
write the field
- command_prepare.rs whole-word quoted "$(...)" fast path: same
promotion after the wrapper call
- parameter_core.rs expand_word_mut_with_context: embedded-backtick words
route to the mutable embedded walker (the production path for every
$( )-bearing word) instead of the legacy &self expand_word fallback,
whose completions cannot publish status
Unchanged on purpose (GNU-verified): the enclosing command's own status
still overwrites when it runs (echo $(false); $? = 0); set -e aborts on a
failed comsub assignment with its rc (D case, 1 / 4) and does NOT abort
on a word-attached comsub failure; local/declare mask the assignment
status (rc 0); nested comsub reports the outer body's own last status to
the parent.
Known adjacent gap, NOT touched (pre-existing, separate subsystem): in a
pipeline, stage words are expanded in a pre-pass, so `true; echo $?
"$(exit 3)" | tr -d ' '` prints 3 where GNU prints 0 - the later word's
comsub runs before the earlier word's $? expands. The pre-pass predates
this fix (which only made its status observable); filing separately.
Also pre-existing: the cd/pwd comsub fast path stays silent on stderr
where GNU prints the cd diagnostic (status and stdout match).
Verification: tests/issue485_comsub_status_propagation.rs 13/13 (oracle
probes above baked as assertions; set -e D case included). Adjacent
suites green: issue70, issue343_345, issue440 (#455 lane), issue71,
issue154, issue305, issue319, issue335, issue354, issue362, issue368,
issue376, issue379, issue384, issue416_417, posixexp2, comsub_nonascii,
conformance runner 22/23 (03-cond-unclosed-bracket pre-existing, GNU
aborts the same shape with rc 2). Full cargo test --no-fail-fast: failure set byte-identical to the
pre-fix tree (68 host-env failures compared name-by-name with the three
fixed files reverted: pwd/path drive rendering, tilde-user fixture,
bashdb fixtures, prompt/cli env classes; empty both-way diff); lib
636 passed / 2 failed = the master host-env pair.
Closes #485
…, not per admission probe The #491 merge added the cd diagnostic to command_substitution_cd_pwd_output - but the mut_typed word path consults that helper TWICE: first as the admission probe (command_substitution_uses_specialized_path calls it with .is_some() to admit the specialized path), then for the real expansion inside the &self wrapper's inner walk. Each failure printed the diagnostic, so `x="$(cd nope && pwd)"` and `local y="$(cd nope && pwd)"` reported `cd: nope: No such file or directory` twice where GNU bash 5.3.0 prints it once (probe vs script-file oracle, /usr/local/bin/bash, byte-compare on both shapes). Split the helper: the single-arg form (real expansion) keeps the emission; the probe goes through command_substitution_cd_pwd_output_quiet_probe, which suppresses only the diagnostic - status cell, output, and admission result are unchanged. Verification: both #485 suites green (tests/issue485_comsub_status_ propagation.rs 13/13, tests/issue485_comsub_status_word_propagation.rs 13/13); 17-form oracle matrix byte-identical to GNU 5.3.0 including the cd shapes now that the diagnostic prints once; full cargo test --no-fail-fast failure set identical to the pre-merge baseline (68 host-env failures, empty both-way diff).
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.
Closes #485
问题
echo "$(cd nope && pwd)" $?—— GNU bash 在父壳回收命令替换子进程的瞬间就把其退出状态写入last_command_exit_value(subst.ccommand_substitute),同一命令行内后置的$?随即可见;rubash 只把状态记进last_command_substitution_status单元、仅由空词/赋值两个收尾点消费,词展开路径上$?读到的仍是展开前的旧值(GNU 1 / rubash 0)。修复(父侧回收点发布状态)
embedded_mutations.rsrun_ast_command_substitution_with_context/run_function_command_substitution尾部:fork 边界处exit_code = status,不再回退saved_exit_code(原地执行器就是 GNU waitpid 的父侧)。expand_command_substitution_mut_typed_with_context三个经过&self包装器的回落点(specialized cd/pwd 含 perf(executor): single-handle cd/pwd comsub path resolution (rubash#375) #484 快路径、specialized builtins、最终回落)从状态单元提升——包装器是&self写不了字段。command_prepare.rs整词引号"$( )"快路径同一提升。parameter_core.rs嵌入反引号词改道可变嵌入 walker($()词的生产路径),脱离无法发布状态的遗留&self展开器。语义边界(oracle: WSL GNU 5.3.0, /usr/local/bin/bash)
保持/对齐均以现场探针为准:
echo "$(cd nope && pwd)" $?11false; echo "$(true) $?"0(成功的替换同样覆盖)0true; echo $? "$(exit 3)"0(左到右,前置$?保留旧值)0echo "$(exit 3)$?"33echo a\exit 3`b $?`ab 3ab 3v="$(exit 3)$?"false; x=$(true) y=$?f(){ local v="$(exit 3)$?"; }set -e; x=$(exit 4)set -e; echo "$(exit 9) x"已知相邻缺口(本 PR 不动,现状与修复前一致)
true; echo $? "$(exit 3)" | tr -d ' 'rubash 得 3、GNU 得 0——后一词的替换先于前一词的$?执行。该 pre-pass 早于本修复存在(修复只是让它的状态变得可观测),属流水线展开时序子系统,建议另开 issue。bash: line N: cd: nope: ...(status/stdout 已一致)。验证
tests/issue485_comsub_status_propagation.rs:13/13(上表形态 + set -e 两用例 + 嵌套替换,全部为 5.3.0 现场实测字节)。cargo test --no-fail-fast:失败集与修复前树逐名比对完全一致(68 个 host-env 失败:pwd/path 盘符渲染、tilde-user fixture、bashdb fixtures、prompt/cli 环境类;双向空差),lib 636 passed / 2 failed = master 既有环境对。