Skip to content

fix(executor): :? and := honor their GNU class outside the command-word pipeline (Refs #2701) - #3690

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2701-question-assign-contexts
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2701-question-assign-contexts

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

tests/issue2701_question_assign_outside_word_pipeline.rs was 5/12 on master: every context outside the main command-word expansion pipeline dropped the GNU :?/:= semantics — :? degraded to :- (no diagnostic, no fatal) and := never bound.

GNU runs all of these through the same word-expansion machinery:

  • redirect words: redir.c:298 redirection_expand → expand_words_no_vars — a :? jump out of redirection_expand must land before the file is opened.
  • case arms: execute_cmd.c:3712 re-expands each arm per match attempt (expand_word_leave_quoted), arms consulted in order.
  • select list: execute_cmd.c:3525+ → expand_words → subst.c:13219 expand_word_list_internal.
  • arithmetic text / subscripts: :? inside $((...)) is the same parameter_brace_expand_error → expand_wdesc_fatal → FORCE_EOF (subst.c:10347 case '?', 10414/10418).
  • ${var/pat/repl}: parameter_brace_patsub (subst.c:9408+) expands the replacement whenever the variable is set — even when the value is empty or the pattern does not match (verified vs Bash 5.3.20).

The fix runs the existing compensating pre-passes (parameter_expansion_error_in_word_isolated, apply_parameter_assignment_expansions_in_word, raise_word_expansion_error) at each non-command-word expansion boundary instead of adding per-context symptom handling. Case arms use a verdict latch inside the .any so GNU's first-match ordering is preserved (an unreached arm neither binds nor errors). The $(( arm latches the verdict onto arithmetic_nounset_error, which carries the identical FORCE_EOF→ExitCode class.

Legs

  • cargo test --test issue2701_question_assign_outside_word_pipeline: 12/12 (was 5/12 on master).
  • GNU 5.3.20 script-file probes: byte-for-byte match on redirect-?, redirect-=, patsub-?, case-?, arith-? (diagnostic text + rc 1 + script death).
  • Adjacent regressions: case_command_head_bash_command_regressions 4/4; issue1025_...redirect_fd_regressions 12/16 — the 4 reds are /dev/tcp network tests failing identically on a tree without this change (environment-bound, zero connections observed — not a regression).
  • cargo fmt hits the known Windows os error 206 on this box; per-file rustfmt --edition 2021 applied to all touched files.

Generated with Devin

…rd pipeline (Refs #2701)

GNU runs every word through the same expansion machinery, so a `${x:?}`
in a redirect word, arithmetic body, case arm, select list, subscript,
or pattern-substitution replacement is the same fatal
parameter-expansion error as in a command word
(subst.c:10347 case '?', 10414/10418 parameter_brace_expand_error ->
expand_wdesc_fatal -> FORCE_EOF), and `${x:=}` binds at the point of
evaluation. Rubash's compensating pre-passes
(parameter_expansion_error_in_word / apply_parameter_assignment_)
only ran inside the simple-command word scan, so in every other
context `:?` degraded to `:-` and `:=` was silently dropped.

- command_execute: pre-pass each redirect target before dispatch
  (redir.c:298 redirection_expand -> expand_words_no_vars); a verdict
  must land before the file is opened, matching GNU's jump out of
  redirection_expand.
- compound_exec: case arms are re-expanded per match attempt in order
  (execute_cmd.c:3712 expand_word_leave_quoted); a verdict latch
  inside the .any preserves GNU's first-match ordering so an
  unreached arm neither binds nor errors.
- select_exec: the word list goes through the same pipeline
  (execute_cmd.c:3525+ -> subst.c:13219 expand_word_list_internal).
- expand_word/arithmetic: the word pre-scan skips `$((`/`$[` units as
  arith-owned (rubash#533), so the arith text is scanned at its own
  expansion boundary; the nounset latch carries the identical
  FORCE_EOF/ExitCode class.
- parameter_errors: nested_operator_word_error now descends into
  `${var/pat/repl}` replacement text whenever the variable is set —
  GNU parameter_brace_patsub (subst.c:9408+) expands the replacement
  through parameter_brace_expand_word even when the value is empty
  or the pattern does not match (verified against Bash 5.3.20);
  split_parameter_separator promoted to crate::executor scope.

tests/issue2701_question_assign_outside_word_pipeline.rs: 12/12
(was 5/12). GNU 5.3.20 script-file probes match byte-for-byte on
redirect/case/arith/patsub/assign cells.

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

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 merged commit c01ebe4 into master Nov 15, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
…after unset HISTTIMEFORMAT stay armed

Refs #3535

## 根因

GNU 的 `history_comment_char` 武装是粘性的:`variables.c:6168-6177 sv_histtimefmt` 在 HISTTIMEFORMAT 存在时把 `history_comment_char` 置 `'#'`(仅当其为 0),unset 时只清 `history_write_timestamps`,从不复位注释符。rubash 把武装建模为「HISTTIMEFORMAT 当前存在」(各 history 读取面采样 `get_env("HISTTIMEFORMAT").is_some()` 同时充当 write-timestamps 与 comment-char 武装),于是置过又撤掉之后,`history -r` 把 `#<epoch>` 行当普通条目读入、条目带空戳,列表渲染为 `??` 三行,与 GNU 的两行带戳不符。

已合入的 #3537(#3393)只建模了同样粘性的 `history_multiline_entries`,未动注释符武装——本 PR 补这一面。

## 修法(机制,非个案)

- `ShellState` 新增粘性闩 `history_comment_char_armed`(readline `history_comment_char` 进程全局的移植,随 Clone fork 边界传递);`Executor::new` 以进程环境导入的 HISTTIMEFORMAT 播种(GNU 对环境导入变量同样跑 sv_histtimefmt)。
- 赋值面闩定:`execute_command` 每命令边界在 HISTTIMEFORMAT 存在时闩定;临时(前缀)绑定在 `apply_temporary_assignments` 内闩定(其恢复先于命令边界,GNU 探针证实 `HISTTIMEFORMAT='[%Y] ' true` 同样粘性武装)。
- `SessionHistory` 读取面把两个 GNU 状态拆开传参:`comment_char_armed`(粘性,管 `#<digit>` 识别与读时戳,histfile.c:375-383 + history.c hist_inittime)与 `timestamps_armed`(现值 = history_write_timestamps,仅继续管 multiline 重组门,histfile.c:384——粘性不放宽该门,钉 multigate 格守此界)。

## GNU 锚点

- `variables.c:6168-6177` sv_histtimefmt(武装粘性本体)
- `lib/readline/histfile.c:375-383` + `:466`(文件首行戳仅借用本读、读毕复位——不闩定)
- `lib/readline/history.c:322-333` hist_inittime(条目戳首字 = 当前 comment char)
- `lib/readline/histfile.c:384`(multiline 门 = history_write_timestamps 现值)

期望值全部以 GNU bash 5.3.20 脚本文件实测(repro / 从未武装对照 / 前缀绑定 / 空值绑定 / `history -n` 面 / multiline 门,共 6 形)。

## 回归钉

`tests/issue3535_history_comment_char_sticky.rs`(6 格):武装后撤除的 `-r` 形、从未武装对照组、前缀绑定、空值绑定、`-n` 面、multiline 门不放宽。

## 本 PR 删除了什么

无删除。纯增性改动:新增一个 ShellState 布尔闩与其播种/闩定点、读取面签名增加一个参数(仓内 5 个调用点同步更新)、新增钉文件。`src/script_driver.rs`、`src/executor/init.rs` 的改动仅为上述闩的播种与调用点传参。

## Captain verification (merge round R5, head 84a58c5, base 78060e4)

- Pin `issue3535_history_comment_char_sticky`: 6/6 green on the merged shape (fail-first banked at delivery: 4 red / 2 green on base).
- Neighbors: issue3393 8/8, issue1669 2/2, both aligned with base.
- `RUSTFLAGS="-D warnings" cargo check --locked --lib --bins`: rc=0, zero warnings. `rustfmt --check` on all 9 changed files: rc=0.
- Full lib bilateral (head vs base 78060e4, bin-first): 1249/0/4 both sides, failure name-set diff empty.
- CI on the R5 head (run 43729056059, head == 84a58c5): 3 failing jobs (Rust tests, Rust tests (macOS), GNU regression goldens and canaries), each failing ONLY on the issue2875 baseline cell (`api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell`); niu-canary success. PASS comment 6894417642 stands.
- Intersection (merge-base eaa357f..78060e4 vs the PR's 9 files): exactly 2 files, both released by the captain after personally reading both sides' hunks — `src/executor/command_execute.rs` (tip #3686 @@ -468 and #3690 @@ -600, both in `execute_command_with_procsub_scope`, vs PR @@ -30 in `execute_command`; ~438 / ~570 lines apart, different functions, no text overlap) and `src/executor/temporary_assignments.rs` (tip #3669 @@ -1482 in `apply_shell_assignment` vs PR @@ -186 in `apply_temporary_assignments`; ~1296 lines apart, different functions, no text overlap). Rebase onto 78060e4 was clean, zero conflicts; the rebased diff is byte-identical to the R4 shape except the two base-blob index lines. Tip unmoved after all legs.

Refs #3535 (the issue stays open until the captain hand-closes it on this merge).
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.

1 participant