Skip to content

fix(pushd): resolve relative targets against the validated cwd (Refs #3396) - #3478

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3396-pushd-relative-getcwd
Nov 15, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3396-pushd-relative-getcwd

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

normalize_stack_dir(src/builtins/pushd/stack.rs)把相对操作数拼到原始 $PWD 上:未设时基为 /(查 /a)、空时基为空(查 /a)、陈旧时基为 /wrong(查 /wrong/a),于是 unset PWD; pushd a 报 pushd: a: No such file or directory、rc 1、PWD 留空/陈旧、栈不动。

GNU 锚点

  • pushd.def:296-302:Dir 操作数委托给 cd_builtin。
  • cd.def:522ff change_to_directory:把操作数原样 chdir,相对路径由内核按真实 cwd 解析,与 $PWD 无关。
  • common.c:592 get_working_directory:即 getcwd;bindpwd(cd.def:136-175)据此重建 PWD。
  • G-02 下 session cwd 字段就是真实 cwd;解析基取已验证的逻辑 cwd($PWD 仅当其仍指当前目录时有效,否则取物理 session cwd,cd/paths.rs validated_logical_pwd,[bug] 逻辑 PWD 未校验是否指代当前目录:伪造/陈旧 $PWD 被 pwd/cd 盲信 #2143)。

修法

normalize_stack_dir(dir, env_vars) → normalize_stack_dir(dir, env_vars, cwd),相对基改走 validated_logical_pwd;两处调用点(Dir 臂与 pushd - 的 OLDPWD 臂)传入 session cwd。健康 PWD 下行为不变(基仍是 $PWD 本身);仅未设/空/陈旧时改走真实 cwd。pushd -n(存原始词,不调 cd)不受影响。

回归钉

tests/issue3396_pushd_relative_getcwd_regressions.rs:未设/空/陈旧三形、./a、链式 pushd ../b、pushd/popd 往返、dirs -l、-n 控制、cd 与绝对目标控制。期望以 GNU 实测为准(种子 seed_r104a.sh 10/10;扩展形同法探针确认)。

本 PR 删除了什么

无。仅改解析基的取值来源,未删除任何公开面、测试或文档。

验证状态

UNVERIFIED-BY-CAPTAIN。钉与修法已写,未在本车道执行:本地构建禁令(写墙间歇 EDQUOT;本轮为 clone 已回收已完工车道 target 约 1.9G)、Simular box 盘满、老服务器 ssh 未恢复、X299 车道直连未通。以下腿欠队长复核轮(X299):新 stem issue3396_pushd_relative_getcwd_regressions 全绿、相邻 issue3197/issue3196/issue3336 stem、全量 lib 与 base 逐数对齐、RUSTFLAGS="-D warnings" cargo check --locked --lib --bins。静态已过:rustfmt --check、check-utf8-boundary-hygiene.sh、check-marker-literals.py 全 clean;fail-first 依据为裁决预筛实测(rubash 4/10,三 pushd 相对格失败)+ 现尖代码直读。

Fail-first nails for rubash#3396: pushd must resolve a relative target
against the real cwd (getcwd), not $PWD. GNU pushd.def:296-302 delegates
the Dir operand to cd_builtin, whose change_to_directory (cd.def:522ff)
chdirs the operand as-is; bindpwd rebuilds PWD from get_working_directory
(common.c:592). Covers PWD unset / empty / stale, ./a, chained ../b,
pushd/popd round-trip, dirs -l, the -n raw-store control, and the cd /
absolute-target controls. Not yet executed in this lane (no build slot);
legs owed to the captain review round.
…3396)

normalize_stack_dir joined every relative pushd operand onto the raw
$PWD, so 'unset PWD; pushd a' looked up /a and 'PWD=/wrong; pushd a'
looked up /wrong/a (rc 1, PWD left empty/stale, stack untouched).

GNU pushd.def:296-302 delegates the Dir operand to cd_builtin, whose
change_to_directory (cd.def:522ff) chdirs the operand as-is, so the
kernel resolves a relative operand against the real cwd, never $PWD
(common.c:592 get_working_directory is getcwd); bindpwd then rebuilds
PWD from getcwd. Under G-02 the session cwd field is that real cwd, so
the base is now validated_logical_pwd (cd/paths.rs, rubash#2143): $PWD
while it still names the working directory, the physical session cwd
otherwise. Healthy-$PWD behaviour is unchanged; pushd -n (raw store,
no cd call) is untouched. Both call sites (Dir arm, pushd - OLDPWD arm)
pass the session cwd.

Nails: tests/issue3396_pushd_relative_getcwd_regressions.rs (6 tests).
Legs owed to the captain review round (no build slot in this lane).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3478 第1轮 — 建议:PASS
head: c7c412a base(merge-base): 3745712 文件: 3 个 +190/-16
关联 issue: #3396(状态:open,我核过的时间:2026-11-15 本地)

机制核查:

  • 相对 pushd 操作数的解析基改为「已验证逻辑 cwd」,与 GNU 一致:pushd.def:296-302 把 Dir 操作数委托给 cd_builtin;cd.def:522ff change_to_directory 将操作数原样 chdir,内核按真实 cwd 解析相对路径,与 $PWD 无关;common.c:592 get_working_directory = getcwd,bindpwd 据此重建 PWD。diff 把 normalize_stack_dir 的基从原始 $PWD 换成 validated_logical_pwd(env_vars, cwd)($PWD 仅在其仍指当前目录时有效,否则物理 session cwd),两个调用点(Dir 臂 + pushd - 的 OLDPWD 臂)都传了 session cwd。pushd -n 不走 cd、保持原始词与 PWD 不动,行为未变。机制正确,无顺手改无关面。

腿(逐项):

  • 钉自跑:issue3396_pushd_relative_getcwd_regressions — Windows 上 cfg(unix) 编译期裁掉(0 tests,预期内);WSL Linux head 实跑 6 passed / 0 failed
  • fail-first:WSL Linux merge-base 实跑 4 failed / 2 passed,红名 = {issue3396_pushd_relative_pwd_unset, pwd_empty, pwd_stale, dot_slash_and_chain};对照 {nocd_unset_pwd_control, controls_cd_and_absolute_pushd} 双侧绿——fix-pins 全红、对照全绿,无废钉
  • lib 双边(Windows):head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state}(1349/1/4)= base 失败名集 → 差集 ∅
  • -D warnings:lib+bins cargo check 0 警告
  • 卫生:rustfmt --check 三个文件 clean;marker-lint OK(117/268);utf8-boundary-hygiene clean
  • CI:3 红任务逐个归因——GNU regression goldens: issue2875_input_procsub_child_runs_as_rubash_shell=基线(1349/1);Rust tests (Linux): issue2875=基线;Rust tests (macOS): issue2875=基线(1242/1)。无其他红
  • GNU 对照:wsl /usr/local/bin/bash 5.3.20(1)-release 脚本探针(target/devin-w8-audit/probe3396.sh):unset PWD→pushd a 后 PWD=WORK/a、dirs=WORK/a WORK、popd 回 WORK;PWD=空→WORK/b;PWD=/wrong→WORK/c;./a→../b 链与 dirs/popd 往返一致;PWD=/wrong 下 pushd -n z 不改 PWD(仍 /wrong)且不动 cwd——6 形状全部与钉期望逐字节一致

F 点:无。

续作记录:无(未动代码)。

未跑/VOID 的腿:Windows 侧钉自跑为预期 VOID(cfg(unix)),已由 WSL Linux 双侧实跑覆盖;其余全跑。

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