Skip to content

fix(builtins): #844/#848 popd,pushd readonly bindpwd verdict + real out-of-range operand word - #855

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/844-848
Oct 19, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/844-848

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #844. Closes #848.

What

#844 — readonly OLDPWD/PWD now fail popd/pushd's inner cd like cd's own

GNU spec (vendored bash bash.git @b4608166, 5.3 patch 15; WSL GNU bash 5.3.0(1)-release probes 2026-10-19):

  • cd.def:136-175 bindpwd: cd_builtin's post-chdir OLDPWD/PWD refresh fails with err_readonly's `VAR: readonly variable` (error.c:453-456 via bind_variable, variables.c:3131-3136); the chdir stands, the failure returns as cd_builtin's EXECUTION_FAILURE.
  • pushd.def routes every cd through cd_builtin — cd_to_string (pushd.def:562-574), change_to_temp (:577-586), the Dir branch (:301):
    • popd top (:400-403): returns BEFORE the free → rc 1, both stack entries survive, the display's cwd slot still moves to the chdir target, nothing prints.
    • pushd swap (:203-215) / rotate (:256-286): the swap/rotation lands in the list BEFORE change_to_temp, so it survives the failure; the display and rc 0 stay behind its success gate.
    • pushd dir (:296-316): cd_builtin fails before add_dirstack_element — no push, no display.
    • -n forms: NOCD never cds → rc 0, untouched.

New bindpwd_write mirrors cd.rs's existing readonly bind verbatim (one diagnostic per variable, OLDPWD first); success paths are byte-identical to before.

#848 — the range diagnostic names the actual out-of-range word

popd's walk records which_word (pushd.def:374-377) and hands it to pushd_error (:382/:414); pushd checks its operand inline (:235-244). The parse now carries the raw word: popd +5 -n reports "+5", pushd +9 -n reports "+9" — never a trailing option. pushd_error (:540-546) also says "directory stack empty" while the offset is 0 for pushd itself (pushd +9/pushd -9 fresh) — the old pushd arm never had that variant; popd already did (#830). part_025's pushd +9 expectation is re-pinned to that oracle verdict (probe: `bash: line 1: pushd: directory stack empty`, rc 1).

GNU oracle A/B (WSL bash 5.3.0(1), script file via stdin, byte-normalized)

  • 12 readonly forms (OLDPWD/PWD × popd / pushd-dir / pushd-swap / pushd+1 / pushd-n / popd-n) + 8 message forms (popd +5 -n, popd -n +5, pushd +9, pushd +9 -n, fresh-stack variants, both-vars).
  • stderr: byte-identical after brand normalization (every readonly diagnostic and every range message).
  • stdout: byte-identical except 8 lines that diverge identically on master and are outside these tickets: 4 pwd-witness lines on the readonly-PWD forms (the pwd builtin's $PWD fast path — executor/pwd_loop_builtins.rs — prints the stuck variable where GNU cd.def:478-483 prints the tracked cwd; reproducible on master with plain `readonly PWD; cd ../b`, so popd/pushd are now family-consistent with cd.rs) and 4 pwd -P lines on the -n controls (master's pushd never moves the process cwd — popd default pop changes the stack and PWD but does not chdir: 'pushd -n ./sub; popd' leaves the process cwd behind (GNU cds into ./sub) #830 deliberately scoped that fix to popd).

Verification

  1. tests/issue844_848_popd_pushd_readonly_range_regressions.rs (new, next to the popd default pop changes the stack and PWD but does not chdir: 'pushd -n ./sub; popd' leaves the process cwd behind (GNU cds into ./sub) #830 file): 12/12 green — readonly forms + message forms incl. the empty-stack variants.
  2. fix(builtins): #830 popd default pop chdirs to the new stack top — process cwd follows the stack #837's 9 regressions (issue830 file): green. part_025: 13/13 green (one expectation re-pinned to the GNU bytes). issue509: green.
  3. Full suite `--test-threads=1 --no-fail-fast`: failure set identical to a pristine b79e678 worktree run on the same machine (36 stable environment-sensitive failures — checkout-path-dependent chaining tests, fd_redirects c_external cluster, sigint/sigterm delivery, cli_tests which CI runs continue-on-error — plus the same load-wobblers: windows_listing_cache / profiler fixture_text_tree / wait_n flake symmetrically on BOTH trees and pass in isolation). +12 passed vs base.
  4. cargo fmt --check clean; clippy: zero findings in touched files (tree-wide pre-existing findings unchanged).

Notes for review

…dict and name the real out-of-range word

#844 — GNU cd.def:136-175 bindpwd: cd_builtin's post-chdir OLDPWD/PWD
refresh fails with err_readonly's "VAR: readonly variable" (error.c:453-456
via bind_variable, variables.c:3131-3136) when either variable is readonly,
and the failure surfaces as cd_builtin's EXECUTION_FAILURE with the chdir
itself standing. pushd.def routes every cd through cd_builtin — cd_to_string
(pushd.def:562-574), change_to_temp (pushd.def:577-586) and the Dir branch's
direct call (pushd.def:301) — so popd/pushd inherit the verdict:
- popd top: pushd.def:400-403 returns BEFORE the free — rc 1, the stack
  keeps both entries, the display's cwd slot still moves to the chdir
  target, and nothing prints (WSL GNU 5.3.0(1): `readonly OLDPWD; popd`
  -> rc 1, dirs "<new top> <new top>").
- pushd swap/rotate: the swap (pushd.def:203-215) and rotation
  (pushd.def:256-286) land in the list before change_to_temp, so they
  survive the failure while the display and rc 0 stay behind its
  EXECUTION_SUCCESS gate.
- pushd dir: cd_builtin fails before add_dirstack_element
  (pushd.def:296-316) — the element never joins, nothing prints.
- the -n forms never cd and stay rc 0.
bindpwd_write mirrors cd.rs's existing readonly bind (one diagnostic per
variable, OLDPWD first); success paths are byte-identical to before.

#848 — the range diagnostics echo the OFFENDING ±N WORD instead of
args.last(): popd's walk records which_word (pushd.def:374-377) and hands
it to pushd_error (:382/:414), pushd checks its operand inline (:235-244),
so `popd +5 -n` reports "+5" and `pushd +9 -n` reports "+9" — never a
trailing option. pushd_error (pushd.def:540-546) also says "directory
stack empty" while the offset is 0 for pushd itself (`pushd +9` fresh,
WSL GNU 5.3.0(1) probe 2026-10-19), which the old pushd arm never had;
popd already did (#830). part_025's `pushd +9` expectation is re-pinned
to that oracle verdict.

tests/issue844_848_popd_pushd_readonly_range_regressions.rs: 12 spawn-level
regressions (readonly OLDPWD/PWD x popd/pushd-dir/swap/rotate + the -n
controls; the three ticket message forms plus the empty-stack variants)
next to the #830 file.
@caomengxuan666

Copy link
Copy Markdown
Member Author

独立代码评审 — PR #855 (fix/844-848, 05d9849)

结论:APPROVE

单提交 05d9849,父 = master 头 8e9c7c0(#852 合入后),无夹带。以下全部为本评审代理独立实做(自建 worktree D:/repo/rubash-cr-855,评审后已删)。

GNU 语义核对(WSL GNU bash 5.3.0(1) oracle 自跑,双侧 stderr 逐字节归一比对)

  • 17 形矩阵(readonly OLDPWD/PWD × popd / pushd dir / swap / rotate + 双 -n 控制 + 8 个越界文案形):17/17 一致。关键证词逐条复核:readonly OLDPWD; popd → rc 1、dirs 双条目同值(cwd 槽已动、栈未弹、无显示);swap/rotate 失败后 dirs 显示保留后的序(swap→[sub,sub2],rotate→[sub,sub2]),rc 1、无显示;pushd dir 失败元素不入栈、pwd -P 已动。
  • 双变量同 readonly → 两条诊断、OLDPWD 先 PWD 后,与 cd.rs 既有 bindpwd 镜像同序同机制(__RUBASH_READONLY_VARS + DATA_DOLLAR),一致。
  • 时机核对:popd = chdir 先 bindpwd 后;swap/rotate = 列表先变 change_to_temp 后;dir = 存在性探测先、cd(bindpwd) 先、add_dirstack_element 后(失败不入栈)——与 pushd.def 对应,且 readonly PWD; pushd ./does-not-exist 双侧均只报 No such file or directory(无 readonly 诊断),时机吻合。
  • 补充边角 9 形(both-readonly、popd +0、fresh pushd +9/-9、fresh popd +5 -n、pushd +0 fresh 成功、popd -- +5):8/9 一致,1 形差异见非阻塞备注 1。

#848 文案

  • popd +5 -n / popd -n +5 / popd +5 / popd -5 / pushd +9 -n / pushd -n +9 / pushd +9(活栈)→ 双侧均报原词(含负号形式 -5)。popd -- +5 双侧均等价于裸 popd(GNU 的 -- 截断后续词,rubash 的 parse 早短路为 Top——行为一致)。
  • part_025 重钉正确:fresh pushd +9 在 WSL 5.3.0(1) 实测为 pushd: directory stack empty rc 1(-9、+0 同族,+0 在 1 条目栈上反而成功打印 cwd——rubash resolved_index 行为一致),旧期望 "+9: … out of range" 是钉在 rubash 旧输出上的错钉。

栈状态审计

失败后 dirs 探针(swap/rotate/popd 三形)内部持久化栈与 GNU 显示逐条一致;save_stack 在失败返回前落盘,显示与内部状态无脱节。

回归

  • issue830(fix(builtins): #830 popd default pop chdirs to the new stack top — process cwd follows the stack #837 9 例)9/9、issue509 3/3、part_025 13/13(含重钉的 test_pushd_redirects_stderr)、新 issue844_848 12/12。
  • 全量 --no-fail-fast -- --test-threads=1 双向(PR vs 8e9c7c0 独立 base worktree):失败集恒等(35 failed tests × 9 targets,comm 双向空差;cli_tests 23 + executor_tests 6 + sigquit 2 + 5 个 1 例目标)。一次 --lib 单例失败复跑 689/689 通过,判定为与 base 构建并行的资源竞争抖动,非本 PR。
  • 成功路径字节级不变:master 二进制 vs PR 二进制 8 形(dir/swap/rotate/3-entry rotate/-n push/popd top/+1/-n)stdout/stderr/rc 全同。
  • cargo fmt --check 干净;RUSTFLAGS=-D warnings 在 master 上同样失败(issue544 等 test 目标的既有 warning 债),PR 自身 5 文件零新增 warning。

夹带扫描

5 文件全部在题内(pushd.rs / pushd/parse.rs / pushd/stack.rs / part_025 / 新回归文件);无 Cargo.toml/docs/CHANGELOG 搭车;新测试无进程击杀代码;pushd.def/cd.def 仅以语义坐标(path:line)与注释引用出现,无 GPL 源码移植。

非阻塞备注(不挡合并)

  1. pushd -n(无操作数):GNU 5.3.0 实测是静默 no-op rc 0(栈/cwd/显示全不动);rubash 的 parse 把它归入 Swap(无 -n 变体),本 PR 使 readonly 诊断在该形上会发出(readonly PWD; pushd -n → rc 1 + 诊断,GNU 无声 rc 0)。该归类似是 [builtins] dirstack 族数字参数验证退出码错:dirs/popd/pushd 非数字 → rc=1,dirs -l +q 静默 rc=0(GNU 统一 2) #509 之前就有的形态,非本 PR 引入,但既然 bindpwd 现在会在这条路径上发声,建议开 follow-up issue 给 pushd 无操作数 + -n 形补 no-op 语义。
  2. fresh popd -N(空列表):GNU 静默 rc 1,rubash(master 与本 PR 相同)报 "popd: directory stack empty" rc 1。既有差异,本 PR 未触碰该行为,可并入 popd 越界文案取 args.last():popd +5 -n 报 "-n:" 而 GNU 报 "+5:"——#837 CR N2 #848 后续。
  3. pushd.rs Dir 失败分支里的 if stack.is_empty() 重查是死代码(上方已保证非空),防御性无害。

不合并不 squash,交由维护者处置。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant