Skip to content

fix(builtins): #830 popd default pop chdirs to the new stack top — process cwd follows the stack - #837

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/830
Oct 19, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/830

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Root cause (one paragraph)

popd's top-of-stack path (then stack.remove(0) + a PWD rewrite in src/builtins/pushd.rs) mutated the directory stack and $PWD but never touched the process working directory, while GNU's popd_builtin cds: builtins/pushd.def:392-403 (top branch) runs cd_to_string(pushd_directory_list[offset-1]) — pushd.def:559-572, a literal cd_builtin(["--", word]) call — before freeing the popped element. So after pushd -n ./sub; popd, rubash's pwd/$PWD/dirs claimed ./sub while pwd -P and every external child stayed in the original directory — the builtin/real split is the ticket's core symptom.

GNU semantics (vendored bash read + WSL GNU bash 5.3.0(1) script-file probes, 2026-10-13)

scenario GNU 5.3.0 rubash before rubash after
pushd -n ./sub; popd rc 0, pwd -P and external pwd = <cwd>/sub stack+PWD claim ./sub, pwd -P/cmd /c cd stay put matches GNU
same, target rmdir'd first rc 1, popd: ./sub: No such file or directory, stack intact (dirs still lists it), no stack print rc 0, popped anyway, printed the stack matches GNU
popd -n +0 (2 entries) pops the pushed word, no cd removed the cwd slot matches GNU
popd -1 (2 entries) -1 == offset → top branch: cds to stack[1] popped cwd slot, no cd matches GNU
popd -1/-2 (3 entries) removes display slot 1 / top branch already agreed; restructured without change matches GNU
popd +0 (1 entry) rc 1, popd: directory stack empty (pushd_error, pushd.def:540-546) rc 0, cleared the stack, wrote PWD=/ matches GNU
popd +9 (1 entry) popd: directory stack empty popd: +9: directory stack index out of range matches GNU (test updated)
pushd sub; popd OLDPWD = pre-popd PWD (cd's bindpwd under cd_to_string) OLDPWD untouched matches GNU
popd -- +8 -- ignores the index; default top pop same (kept) same

Range/verdict wording follows pushd_error exactly: offset 0 → "directory stack empty" (no operand), otherwise +N: directory stack index out of range (sh_erange).

The fix

  • chdir first, pop second (popd_chdir): set_current_dir(shell_path_to_windows(stack[1])). Failure prints cd.def:427's shape under the invoking builtin's prolog (popd: <word>: <strerror>) and returns EXECUTION_FAILURE with the stack, PWD and display untouched — GNU frees the element only after cd_to_string succeeds.
  • success does cd's bindpwd bookkeeping: OLDPWD = pre-popd PWD, PWD = promoted word, and the __RUBASH_PHYSICAL_PWD handling mirrors cd.rs — a real chdir drops the override (so pwd -P follows the real cwd), the Windows-host POSIX bridge pins it (PopdChdir::Bridged, same predicate as cd.rs:437-455 logical_posix_test_dir; round 2).
  • ±N operands resolve through resolved_index once: resolved 0 (popd, popd +0, popd -(len-1)) is exactly GNU's top branch (direction == '-' && which == directory_list_offset, probe-verified on 2- and 3-entry displays); other slots only remove the element (pushd.def:404-421 — no chdir, $PWD untouched).
  • dirs -c audited (CLEARSTAK, pushd.def:481-485): neither side chdirs; rubash's fallback display element tracks $PWD, which now tracks the real cwd after a popd — same working-directory fallback GNU gets from get_working_directory. Regression-covered.

Differential regression

tests/issue830_popd_default_chdir_regressions.rs — 9 cases; every scenario witnesses both the pwd -P builtin and an external child (cmd /c cd, plus the $(cmd /c cd) command-substitution form), because the builtin/real cwd split was the defect.

Two pre-existing executor_command_chaining popd cases follow the corrected semantics:

Full-suite baseline comparison

cargo test --no-fail-fast on the 12 binaries that fail anywhere + the new file, both sides on pristine trees (main worktree carries a parallel session's uncommitted files, so both runs ran in a detached worktree):

No test that passed at baseline fails after the fix.

Out of scope (observed, not changed)

GPL

Behavior derived from reading third_party/bash/builtins/pushd.def and cd.def (specification only, per repo policy); the Rust implementation is original — no GPL code copied.

…reeing it

popd's top-of-stack path rewrote the stack and $PWD but never touched the
process cwd: `pushd -n ./sub; popd` left `pwd -P` and every external child
in the original directory while `pwd`/dirs claimed ./sub. GNU cds —
pushd.def:392-403 (popd_builtin top branch) runs cd_to_string
(pushd.def:559-572, a `cd_builtin(["--", word])` call) BEFORE freeing the
popped element.

- chdir first (shell_path_to_windows + set_current_dir). Failure keeps the
  stack, PWD and display untouched, prints cd.def:427's shape under the
  invoking builtin's prolog (`popd: <word>: <strerror>` via
  posix_errors::message) and returns EXECUTION_FAILURE — GNU 5.3.0 probe:
  rc 1, `dirs` still lists the word, no stack print. An operand that
  stored as "" keeps cd.def:331-336's "null directory" verdict.
- success performs cd's bindpwd bookkeeping: OLDPWD = pre-popd PWD, PWD =
  the promoted word, `__RUBASH_PHYSICAL_PWD` dropped so `pwd -P` follows
  the real process cwd (same contract as cd.rs after a chdir).
- ±N operands resolve through resolved_index once: resolved 0 (`popd`,
  `popd +0`, `popd -(len-1)`) is exactly GNU's top branch
  (`direction == '-' && which == directory_list_offset`; probe-verified on
  two- and three-entry displays); any other slot only removes the element
  (pushd.def:404-421 — no chdir, $PWD untouched, as GNU leaves it).
- range verdicts follow pushd_error (pushd.def:540-546): "directory stack
  empty" while the display shows one entry, else sh_erange's "directory
  stack index out of range". Fixes two same-family gaps: `popd +0` on a
  spent stack used to succeed and write PWD=/, and `popd -n +0` removed
  the cwd slot instead of the top (GNU R3 probe pops the pushed word).
- `dirs -c` audited (CLEARSTAK, pushd.def:481-485): neither side chdirs;
  rubash's fallback display element tracks $PWD, which now tracks the real
  cwd after a popd, so the post `-c` display stays on the working
  directory like GNU's get_working_directory fallback.

GNU differential (WSL GNU bash 5.3.0(1), script-file probes, 2026-10-13):

  pushd -n ./sub; popd -> GNU pwd -P=/tmp/.../sub, PWD absolute, rc 0
                         (rubash before: pwd -P and `cmd /c cd` stayed in
                          the original dir; after: both report .../sub)
  pushd -n ./sub; rmdir sub; popd
                       -> GNU rc 1, "popd: ./sub: No such file or
                          directory", dirs still `<cwd> ./sub`
                         (rubash before: rc 0, popped, printed the stack)
  popd -n +0           -> GNU pops the pushed word, no cd (rubash before:
                          removed the cwd slot)
  (2-entry) popd -1    -> GNU top branch: cds to stack[1], rc 0
  (3-entry) popd -1/-2 -> GNU removes display slot 1 / top branch; rubash
                          resolved_index maps both identically
  popd +0 (1-entry)    -> GNU rc 1 "directory stack empty" (pushd_error
                          offset==0); rubash used to succeed with PWD=/
  pushd sub; popd      -> GNU OLDPWD=<pre-popd PWD>; rubash now matches

Regression: tests/issue830_popd_default_chdir_regressions.rs (9 cases;
each scenario witnesses both the `pwd -P` builtin and an external child
`cmd /c cd` / `$(cmd /c cd)` — the builtin/real split was the ticket's
core symptom). Full suite: failure set identical to the master baseline
modulo the 9 new cases (red at bd4f703, green here); no new failures.

GPL note: behavior derived from reading third_party/bash/builtins/pushd.def
and cd.def (specification only); implementation is original Rust, no GPL
code copied.
…idge verdict; empty-stack message pinned to pushd_error

The two executor_command_chaining popd failures in the first cut:

- test_popd_double_dash_ignores_following_stack_index: popd's chdir ran a
  raw set_current_dir, but cd on this host resolves /bin-class names
  through the Windows-host bridge (cd.rs:437-455 logical_posix_test_dir:
  cfg(windows) && !shell_root_configured && "/"|"/bin"|"/etc"|"/tmp"
  |"/usr") — success by logical-state rewrite, no chdir(2). popd_chdir now
  returns Real|Bridged; the caller mirrors cd.rs's physical-pwd handling
  (Real drops __RUBASH_PHYSICAL_PWD, Bridged pins it to the logical name).
- test_popd_appends_stderr asserted the pre-#830 out-of-range wording for
  `popd +9` on a spent stack; GNU's pushd_error (pushd.def:540-546) names
  "directory stack empty" whenever the offset is 0 (WSL GNU bash 5.3.0
  probe: "popd: directory stack empty", rc 1; with a pushed entry:
  "popd: +9: directory stack index out of range"). Assertion updated.
…at the crate target dir

With the top-pop chdir in place the shell process's cwd moves mid-script
(pushd /tmp; pushd /bin; popd -- +8 lands it on /tmp), so the harness's
relative target/... redirect paths resolved against the moving cwd: the
output scattered and, under >>, accumulated across runs. The files now
anchor at CARGO_MANIFEST_DIR/target, spelled POSIX-side for the script
(/<drive>/... — shell_path_to_windows's canonical input) and native for
the Rust-side fs:: probes; assertions unchanged.
@caomengxuan666

Copy link
Copy Markdown
Member Author

独立评审(PR #837)— 结论:APPROVE

独立复核代理(非实现代理)。自建 worktree cr-837(091bac26)+ cr-837-base(origin/master 992d5c5),全部结论以 vendored 源码 + WSL oracle + 双树实测为准,未采信票面报告。

1. GNU 语义逐条核对(third_party/bash/builtins/pushd.def、cd.def)

  • 顶分支(pushd.def:392-404):cd_to_string(新栈顶) 先于 free,失败则 return、栈/PWD/display 不动、不打印 dirs —— 实现的 chdir-first/早退路径逐点一致。
  • cd_to_string(:559-572)= cd_builtin(["--", word]):popd_chdir 用原栈词、错误形取 cd.def:427 %s: %s(strerror)、空词取 cd.def:335 "null directory"、prolog 归 popd,均对。
  • 非顶分支(:404-421)只删元素不 chdir、PWD 不动;-n 跳过 cd_to_string(:397);成功后才 dirs_builtin(NULL);-- 终止解析、余词忽略(默认顶弹)——全部与实现一致。
  • pushd_error(:540-546):offset==0 ⇒ "directory stack empty",否则 sh_erange —— popd_range_error 一致。±N→display 槽位映射(resolved==0 ⇔ 顶分支,含 -N==offset、-0 删末位不 chdir 等 quirk)对 1/2/3 层显示逐一推演无误。
  • GPL 红线:注释为行号引用+行为描述,无 C 代码、无逐行翻译,实现为原创 Rust。通过。

2. 边界矩阵实测(17 场景 × GNU 5.2.21(1) WSL oracle / 修复树 / master 基线 三方)

FIX 与 GNU 全部一致(仅两处票面已声明的既有分歧:PWD/dirs 存词形态 ./sub vs 绝对路径;诊断 prolog 为 exe 名)。BASE 逐条复现票面缺陷:顶弹后 pwd -P/cmd /c cd 不动、chdir 失败照弹 rc 0、popd -n +0 删 cwd 槽、popd -1(2条) 不 chdir、popd +0(单条) 清栈且 PWD=/、popd +9(单条) 旧措辞、OLDPWD 不刷新。深水区加试:3 条栈 popd -2(顶分支、目标不存在)GNU rc 1 + "popd: ./b: No such file or directory" + 栈原样 —— FIX 逐字节复现。

3. POSIX 目录桥交互(评审专项探针)

无 root 配置主机上 pushd -n /tmp; popd(Bridged)与 cd /tmp 的逻辑/物理状态逐字节相同:$PWD=/tmp、顶层 pwd -P 认 __RUBASH_PHYSICAL_PWD 钉住值。comsub 内 pwd -P 不认钉值——cd 既有子壳传播特性,popd 照抄同一形态,无新增"栈弹了 cwd 没动"分裂(BASE 在桥名上反而存在 $PWD 与 pwd -P 相斥,FIX 顺带修齐)。

4. 回归与全量双向对比(RUST_TEST_THREADS=1)

  • 新回归 9/9 绿;part_025 13/13 绿(含 2 处更新)。
  • master 基线 39 failed vs 修复树 38/38 failed(两次全量):稳定失败集两侧完全相同(本机 Niubash root 环境族);comm 非对称项(input_redirect_resolves_virtual_root_file_operand、fixture_text_tree_matches_normalized_output、part_036::test_wait_n...)经同树复跑翻转 + 单测 3×3 全绿证实为环境 flake,双向方向都有。无任何基线通过用例在修复树上稳定变红。

5. 夹带扫描

diff 恰 4 文件;三个 commit 的文件清单与信息一一对应;无 taskkill/进程清理代码;RUSTFLAGS=-D warnings 构建干净、cargo fmt --check 干净、Path 导入在用;CHANGELOG 落位正确。

非阻塞备注(建议开票,不拦合)

  • N1:popd 内层 cd 未检查 OLDPWD/PWD readonly。GNU 实证:readonly OLDPWD; popd → cwd 已动但栈不弹、rc 1、报 "OLDPWD: readonly variable"(bindpwd 失败连带 free 不执行);rubash rc 0 静默弹。同族缺口 pushd Swap/rotate 也有(set_pwd_from_stack 直写);cd.rs 已有 readonly 检查可镜像。
  • N2:越界文案操作数取 args.last(),popd +5 -n 会报 "-n:" 而 GNU 报 "+5:"(master 既有模式顺延,且解析器只认操作数前的 -n)。
  • N3:oracle 为 WSL bash 5.2.21(票面探针 5.3.0);本区域两版语义相同,17 场景矩阵已实证。

结论:APPROVE。 不合并(留给维护者)。worktree 已清理。

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