Skip to content

seed-aware BASH_SOURCE bottom pop for interactive source - #3135

Merged
caomengxuan666 merged 9 commits into
masterfrom
fix/3125-interactive-source-bash-source-frame
Nov 10, 2026
Merged

caomengxuan666 merged 9 commits into
masterfrom
fix/3125-interactive-source-bash-source-frame

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Fixes #3125 — 交互 source 不推 BASH_SOURCE 帧(Refs #3125,修法本身只解决本单)

现象

交互 shell(PTY REPL)里 source ./sourced_outer.sh 后,sourced 文件顶层读 ${BASH_SOURCE[0]} 为空、${#BASH_SOURCE[@]} 为 0;嵌套 source 内层深度为 1(GNU 为 2);sourced 文件定义的函数在 REPL 调用时读到空 / N=0;set -u 下 sourced 文件内 "$BASH_SOURCE" 报 unbound variable。-c 与脚本文件形态全部正确,只有交互驱动路径偏离。实害:bash-completion pytest 套件(#2905 链)在交互 fixture 里 source bash_completion,其顶层 _comp__init_collect_startup_configs "$BASH_SOURCE" 触发 unbound variable,2248 例卡在 fixture setup。

根因

rubash 的 bash_source_stack 底部由 set_env(__RUBASH_SCRIPT_NAME) 的两个播种臂(src/executor/public_accessors.rs:1687 IS_C 臂、:1695 普通臂)装入一个 $0 种子——本地实现装置,GNU 没有。三个 BASH_SOURCE 视图臂用“没有脚本主帧 ⇒ 栈底即种子 ⇒ 弹出”来补偿(src/executor/dynamic_arrays.rs:1516、dynamic_arrays.rs:1710、src/executor/arrays/executor.rs:68)。

交互会话从不装种子(run_interactive_stdin 不设 __RUBASH_SCRIPT_NAME,栈从空开始),于是 source 推入的真实帧成了栈底唯一元素,视图的 pop() 把它当种子吃掉。

GNU 锚点(bash-5.3/):builtins/evalfile.c:253 source_file() 无条件 push_source;variables.c:5721 push_source 就是 array_push,栈从空开始、无 $0 种子;execute_cmd.c:5313 函数调用推定义文件;shell.c:1640 主帧只对脚本文件推。GNU 的 bash_source_a 只含真实帧。

修法:种子感知的弹出(seed-aware pop)

播种只发生在 set_env(__RUBASH_SCRIPT_NAME) 以整栈替换装入单元素时;真实帧只从索引 0 插入;弹出只从索引 0——种子若存在,恒在栈底。新增 bash_source_seed: Option<String> 记录播种值,视图臂只在栈底确实等于已记录种子时弹出:

  • src/shell/state.rs:bash_source_stack 旁新增 bash_source_seed: Option<String>(手动 Clone 同步加项)
  • src/executor/init.rs:初始化为 None
  • src/executor/public_accessors.rs:两播种臂同步置种子(__RUBASH_IN_SOURCE 臂不动栈、种子不动)
  • src/executor/dynamic_arrays.rs:新增 bash_source_bottom_is_seed() 判定,两 BASH_SOURCE 臂弹出条件改为“栈底是种子”(其余门原样保留)
  • src/executor/arrays/executor.rs:caller 臂同改
  • src/executor/external_finish.rs:fresh-shell 子进程前后保存/恢复加种子字段

不改:store_indexed_array_state 存储格、current_bash_source() 回退链、push_bash_source/pop_bash_source、函数入口/出口、has_script_main_frame()。#2732(REPL 定义函数的 BASH_SOURCE[0] 应为 main)不在本单修法范围,两单正交。

验证

  • 钉子 tests/issue3125_interactive_source_bash_source.rs(4 格 PTY 形态,期望值逐字节取自 GNU oracle):fail-first 在真基底 cd41459b 上 0 passed / 4 failed(红法与 §1 逐字一致);头树 4 passed / 0 failed。
  • GNU 探针 byte-SAME(probes/drive_oracle.sh + -c/-s/脚本对照):头树二进制与 GNU 逐字节一致——交互 OUTER-pre BS0=[./sourced_outer.sh] N=1、INNER N=2、FN BS0=[./sourced_outer.sh] N=1、nounset N=1 / NS-RC=0;-c/-s/脚本对照全部一致。基底二进制同探针复现全部 4 处偏离。
  • RUSTFLAGS="-D warnings" 下 cargo check --locked --lib --bins:通过——Finished dev profile in 40.56s,H2_EXIT=0,零警告/零错误(全日志 warning/error[ 命中均为墙死段的写失败,非编译警告)。
  • lib 双边(头树 vs 真基底 cd41459b,cargo test --locked --lib):头树 1206 passed; 0 failed; 4 ignored(29.07s,LIB_HEAD_EXIT=0)、真基底 1206 passed; 0 failed; 4 ignored(20.78s,LIB_BASE_EXIT=0),完全一致,修法对 lib 套件零影响。
  • 邻茎(头树):9 茎中 8 茎通过——issue1587 2/2、issue1016 1/1、issue2302 7/7、issue2314 12/12、issue2317 12/12、issue519 5/5、issue1563 13/13、issue2023 2/2;cli_tests function_call_stack_* 3/3 通过(472 filtered out)。issue2142 为基底本有失败(测试 bug),非本修法回归:头树 4/1 与基底树 4/1 完全一致(仅 nested 格失败,断言逐字相同);caller 在嵌套 -c 帧上报 argv[0] 逐字值,测试以绝对路径调用却期望裸名 bash,GNU 在同一调用形下亦印绝对路径。处置待定(另立单修测试 / 随本 PR 修)。
  • cargo fmt --check 全树通过;python3 scripts/check-marker-literals.py OK(117 registry consts,265 allowlisted sites)。
  • CI:本头 11 项全绿(Rust tests、macOS、Compatibility CLI tests、Shell functionality tests、GNU regression goldens and canaries、Cross-target check darwin/linux-android、Windows tests compile check、API ledger tripwire、niubash builds against this tree;Bash upstream test progress skipping)。

残余

REPL 定义函数的 BASH_SOURCE[0]:GNU [main] N=1;基底 [] N=0;本修法后 [environment] N=1——N 已恢复正确,值 [environment] 是 REPL 定义函数的记录定义位置,即 #2732 待修的面,本单不动。

本 PR 删除了什么

无删除。纯新增字段(bash_source_seed)+ 三处弹出条件收窄(stack.pop() 改为“栈底是种子时才 pop”)。净 +60/−3 行(6 文件)+ 钉子 224 行。

@caomengxuan666
caomengxuan666 force-pushed the fix/3125-interactive-source-bash-source-frame branch from 3b10d8f to 618e37a Compare November 8, 2026 13:22
…3125)

GNU variables.c:5721 push_source holds only real frames; rubash's
BASH_SOURCE views popped the stack bottom whenever there was no
script main frame, assuming it was the $0 seed installed by
set_env(__RUBASH_SCRIPT_NAME). An interactive session never installs
that seed, so the pop ate the real sourced-file frame. Record the
seed value at seeding time (bash_source_seed) and pop the bottom
entry only when it really is the seed.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 10, 2026 15:43
@caomengxuan666 caomengxuan666 changed the title test: pin interactive source BASH_SOURCE frame (Refs #3125) seed-aware BASH_SOURCE bottom pop for interactive source Nov 10, 2026
@caomengxuan666
caomengxuan666 merged commit c9e3d63 into master Nov 10, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 11, 2026
…#2731 #2732 #2733 #2734) (#3245)

W-I1 interactive routes: histexpand seeding respects explicit opt-out; interactive definitions record `main`; monitor + diag faces pinned (head 7075bed)

Fixes #2733
Fixes #2732
Refs #2731
Refs #2734

## Per-issue account (captain review, true merge-base 9ec0355 + top-up pins)

| Issue | Stem | Base | This head | Head merged with current master |
|---|---|---|---|---|
| #2733 | issue2733_interactive_histexpand_routes (5) | 4 passed / 1 failed (sole red `histexpand_optout_controls`) | 5 / 0 | 5 / 0 |
| #2732 | issue2732_interactive_bash_source_main (5) | 2 / 3 | 2 / 3 (record value now `main`; remaining reds are the view-layer pop covered on master by PR #3135) | 5 / 0 |
| #2731 | issue2731_interactive_monitor_default (6) | 6 / 0 | 6 / 0 (regression lock; host-side follow-up stays with the niu chain) | 6 / 0 |
| #2734 | issue2734_interactive_diag_line_segment (7) | 5 / 2 | 5 / 2 (pins and fail-first evidence only; product code for this face is a separate lane) | 7 / 0 (the separate repair lane reached master first as PR #3332; the pin file is byte-identical on both sides) |

## Changes

- #2733 seeding: GNU shell.c:1839 seeds the interactive histexpand default from the invocation-time state, so an explicit opt-out is the user's word. Both unconditional seeding sites (`apply_init_interactive_defaults` in src/main.rs, `initialize_interactive_history` in src/script_driver.rs) now seed set-if-unset on the `__RUBASH_SETOPT_histexpand` carrier: a command-line `+H` and a startup-file `set +H` both survive.
- #2732 record face: GNU make_cmd.c:746-762 records `main` for a function defined at the interactive top level. New `definition_source` in src/executor/function_calls.rs records `main` when the BASH_SOURCE stack is empty and the invocation is interactive; every other route keeps the existing `current_bash_source` value, and that function itself is unchanged. The remaining view-layer half of the issue landed on master with PR #3135; the two halves together take the stem to 5/0, as measured on the merged tree during review.
- Pins for all four stems are carried in from PR #3175 unchanged.

## Verification (captain review, landed logs)

- Base red name sets match the registration exactly (2733 sole red `histexpand_optout_controls`; 2732 the three interactive-definition cells; 2734 the piped and `-i -c` cells).
- Neighbour stems on the head: issue1252 3/0, issue1668 6/0, issue2072 4/0, issue2948 6/0; issue2142 4/1 with the same single red name on the base tree (pre-existing, outside this change).
- Lib bilateral (both trees built `--bin rubash` first): base 1243 passed / 0 failed / 4 ignored, head 1243 / 0 / 4, failure name-set difference empty.
- `RUSTFLAGS="-D warnings"` check (lib + bins) rc=0 with the flag proven loaded; `cargo fmt --check` rc=0; whole-tree UTF-8 boundary hygiene rc=0; marker-literal check rc=0; `cargo check --tests` rc=0.
- Head CI (final head): 10 success + 1 skipped + 0 failures. On the review head the macOS library step failed once on the `issue2859_jobs_face_reaped_job_does_not_count` count race, a known load-flake family outside this change's files; that step is green on the final head.

## What this PR deletes

- The two unconditional "set histexpand on" seeding writes (behaviour deletion: an explicit `+H` / `set +H` is no longer overwritten by the interactive default).
- No files, no public API surface, and no tests are deleted. The four pin files arrive unchanged from PR #3175.
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] interactive source does not push a BASH_SOURCE frame (REPL-sourced file sees empty stack; blocks bash-completion suite fixture)

1 participant