Repository navigation
fix(builtins): #830 popd default pop chdirs to the new stack top — process cwd follows the stack - #837
Conversation
…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.
独立评审(PR #837)— 结论:APPROVE独立复核代理(非实现代理)。自建 worktree 1. GNU 语义逐条核对(third_party/bash/builtins/pushd.def、cd.def)
2. 边界矩阵实测(17 场景 × GNU 5.2.21(1) WSL oracle / 修复树 / master 基线 三方)FIX 与 GNU 全部一致(仅两处票面已声明的既有分歧:PWD/dirs 存词形态 3. POSIX 目录桥交互(评审专项探针)无 root 配置主机上 4. 回归与全量双向对比(RUST_TEST_THREADS=1)
5. 夹带扫描diff 恰 4 文件;三个 commit 的文件清单与信息一一对应;无 taskkill/进程清理代码; 非阻塞备注(建议开票,不拦合)
结论:APPROVE。 不合并(留给维护者)。worktree 已清理。 |
Root cause (one paragraph)
popd's top-of-stack path (thenstack.remove(0)+ a PWD rewrite insrc/builtins/pushd.rs) mutated the directory stack and$PWDbut never touched the process working directory, while GNU'spopd_builtincds:builtins/pushd.def:392-403(top branch) runscd_to_string(pushd_directory_list[offset-1])—pushd.def:559-572, a literalcd_builtin(["--", word])call — before freeing the popped element. So afterpushd -n ./sub; popd, rubash'spwd/$PWD/dirsclaimed./subwhilepwd -Pand 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)
pushd -n ./sub; popdpwd -Pand external pwd =<cwd>/sub./sub,pwd -P/cmd /c cdstay putrmdir'd firstpopd: ./sub: No such file or directory, stack intact (dirsstill lists it), no stack printpopd -n +0(2 entries)popd -1(2 entries)-1 == offset→ top branch: cds to stack[1]popd -1/-2(3 entries)popd +0(1 entry)popd: directory stack empty(pushd_error, pushd.def:540-546)PWD=/popd +9(1 entry)popd: directory stack emptypopd: +9: directory stack index out of rangepushd sub; popdOLDPWD= pre-popd PWD (cd's bindpwd under cd_to_string)popd -- +8--ignores the index; default top popRange/verdict wording follows
pushd_errorexactly: offset 0 → "directory stack empty" (no operand), otherwise+N: directory stack index out of range(sh_erange).The fix
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 returnsEXECUTION_FAILUREwith the stack, PWD and display untouched — GNU frees the element only aftercd_to_stringsucceeds.OLDPWD= pre-popd PWD,PWD= promoted word, and the__RUBASH_PHYSICAL_PWDhandling mirrors cd.rs — a real chdir drops the override (sopwd -Pfollows the real cwd), the Windows-host POSIX bridge pins it (PopdChdir::Bridged, same predicate ascd.rs:437-455 logical_posix_test_dir; round 2).resolved_indexonce: 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,$PWDuntouched).dirs -caudited (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 fromget_working_directory. Regression-covered.Differential regression
tests/issue830_popd_default_chdir_regressions.rs— 9 cases; every scenario witnesses both thepwd -Pbuiltin 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_chainingpopd cases follow the corrected semantics:test_popd_appends_stderr: expected diagnostic updated to GNU'spushd_errorwording (popd +9on a spent stack → "directory stack empty"; the old assertion encoded the pre-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 out-of-range text).test_popd_double_dash_ignores_following_stack_index: harness files now anchor atCARGO_MANIFEST_DIR/target(POSIX spelling in the script, native form for the Rustfs::probes) — with the top-pop chdir in place the process cwd legitimately moves mid-script, so redirect paths resolved against the moving cwd would scatter.Full-suite baseline comparison
cargo test --no-fail-faston 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):bd4f7030(origin/master at branch time): 51 failing tests091bac26(rebased on origin/master992d5c5e): the same set, except (a) the 7 environment-independent 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 cases are red at baseline / green here (popd_no_cd…/non_top_popd…pass on both by design), (b)test_popd_appends_stderrupdated as above (green both sides after the update), (c) a handful of CWD-content-sensitive glob/parameter tests flip with the sandbox contents on both sides (e.g.append_fanout_globs) — pre-existing, direction-independent, none touch the dirstack.No test that passed at baseline fails after the fix.
Out of scope (observed, not changed)
pushd(non--n:dir/swap/rotate) also never chdirs — same family, but 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 is the popd default pop;pushd -n …; popdisolates it. Worth its own ticket.pushd -n ./sub; popd, rubash's$PWD/dirs element 0 spell the stored stack word (./sub) where GNU's bindpwd/get_working_directoryproduce the absolute form — the stored-word convention is pre-existing rubash stack behavior this PR does not revisit.GPL
Behavior derived from reading
third_party/bash/builtins/pushd.defandcd.def(specification only, per repo policy); the Rust implementation is original — no GPL code copied.