Skip to content

fix(executor): propagate command-substitution status to in-word $? (rubash#485) - #491

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/comsub-status-propagation
Oct 10, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/comsub-status-propagation

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Closes #485.

GNU bash records a command substitution's exit status in last_command_exit_value as soon as the substitution completes in the parent (subst.c command_substitute, after wait_for (pid); same rule the nofork funsub form documents at subst.c:7120-7122). Words to the RIGHT of the substitution in the same command's word list therefore observe it:

echo "$(cd nope && pwd)" $?   # GNU: 1 (with the cd diagnostic), rubash was: 0
echo $(true) $(false) $?      # GNU: 1  (left-to-right, last one wins)
echo $(false) $(true) $?      # GNU: 0
false; echo $(true) $?        # GNU: 0  (a succeeding comsub resets the status)

rubash only consumed the substitution status for null-word commands ($(false) alone) and assignment results (x=$(false)), so a mid-word $? read the stale pre-command status.

Change

  • expand_command_substitution_with_context records the substitution status into a new word_expansion_comsub_exit overlay (cell stays set for the assignment paths).
  • All $? readers (unbraced, braced, ${!?} indirect, patsub specials) go through dollar_question_status(), which prefers the overlay and falls back to exit_code.
  • The overlay is cleared at each command-node boundary (execute_ast_inner), so the next command's committed status wins, like GNU.
  • case commits the overlay before the clause walk (the case command sets no status until a body finishes): case $(false) in "") echo $?;; esac prints 1.
  • The echo fast path pins the substitution cell back to 0 — $(echo $(false)) is status 0.
  • The cd/pwd comsub shortcut now emits the cd diagnostic the real builtin prints in the child instead of swallowing it.

Bookkeeping only: the enclosing command still imposes its own exit status, so errexit semantics are unchanged (set -e; echo "$(false)"; echo after keeps running; a bare $(false) null command still exits under errexit; local x="$(false)" stays 0).

Oracle

All forms verified byte-identical against GNU Bash 5.2.37 (msys, D:\Git\usr\bin\bash.exe) — quoted/unquoted, left-to-right ordering, nested $(echo $(false)), set -e interaction, ERR trap $?, case head, for-list, array assignment.

Tests

  • New: tests/issue485_comsub_status_word_propagation.rs (13 tests, all pass).
  • Full cargo test on this host: the failing-test set is byte-identical to a clean-HEAD baseline worktree (pre-existing host-environment failures only; no new failures). cargo fmt applied.

caomengxuan666 and others added 2 commits October 10, 2026 13:40
…ubash#485)

GNU subst.c command_substitute records the substitution child's wait
status in last_command_exit_value as soon as the substitution completes
in the parent (the same rule the nofork funsub form documents at
subst.c:7120-7122, already cited in function_substitute). Words to the
RIGHT of the substitution in the same command therefore observe it:
echo "$(cd nope && pwd)" $? prints 1, each substitution overwrites the
previous left-to-right (echo $(true) $(false) $? is 1,
echo $(false) $(true) $? is 0), and a succeeding substitution resets a
failing previous status ("false; echo $(true) $?" is 0). rubash only
consumed the substitution status for null-word commands and assignment
results, so a mid-word $? saw the stale pre-command status (0).

Rust semantic owners:
- command_substitution.rs expand_command_substitution_with_context:
  record the substitution cell into the new word_expansion_comsub_exit
  overlay after the inner expansion (the cell stays set for the
  assignment paths, expand_assignment_value_result*).
- parameter_core.rs dollar_question_status: $? readers (unbraced, braced,
  ${!?} indirect, patsub specials) prefer the overlay, falling back to
  exit_code outside word expansion.
- ast_exec.rs: clear the overlay at each command-node boundary — the
  next command commits its own status, like GNU.
- compound_exec.rs execute_case_command: commit the overlay before the
  clause walk — the case command itself sets no status until a body
  finishes, so "case $(false) in '') echo $?;; esac" prints 1.
- command_substitution.rs echo fast path: pin the substitution cell back
  to 0 after echo (echo cannot fail there, and an argument's nested
  substitution would otherwise leave a stale status — $(echo $(false))
  is status 0).
- command_substitution.rs cd/pwd shortcut: emit the cd diagnostic the
  real builtin would print in the child ("cd: nope: No such file or
  directory" with the script line prefix) instead of swallowing it.

Bookkeeping only: the enclosing command still imposes its own exit
status, so errexit is unchanged ('set -e; echo "$(false)"; echo after'
keeps running; a bare $(false) null command still errexit-exits), and
'local x="$(false)"' stays 0.

Verified byte-identical against GNU Bash (oracle) on the reproducer
family: quoted/unquoted forms, left-to-right ordering, nested
$(echo $(false)), errexit interaction, ERR trap $?, case head, for-list,
array assignment. Regression file
tests/issue485_comsub_status_word_propagation.rs (13 tests) passes;
full-suite failing-test set is byte-identical to the clean-HEAD
baseline on this host (no new failures).

Closes rubash#485.
@caomengxuan666
caomengxuan666 merged commit d3a07c6 into master Oct 10, 2026
@caomengxuan666
caomengxuan666 deleted the fix/comsub-status-propagation branch October 10, 2026 05:46
caomengxuan666 added a commit that referenced this pull request Oct 10, 2026
…, 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).
caomengxuan666 added a commit that referenced this pull request Oct 10, 2026
…osing command runs (rubash#485) (#494)

* fix(executor): comsub reap publishes its status to $? before the enclosing 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

* fix(executor): cd/pwd comsub shortcut prints the #491 diagnostic once, 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).
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] comsub 失败状态在词内 $? 的传播时序与 GNU 不一致(rubash 0 / GNU 1)

1 participant