Skip to content

fix(cd): emit GNU's chdir: diagnostic on cd entry in a poisoned cwd (Refs #3574) - #3713

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3574-cd-deleted-cwd-chdir-diagnostic
Nov 16, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3574-cd-deleted-cwd-chdir-diagnostic

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3574

根因

中毒态(pwd -P 在 deleted cwd 失败,经 resetpwd 把 the_current_working_directory 置 NULL,rubash#3349)下,GNU 的 change_to_directory 入口在 tcwd == NULL 时调 get_working_directory ("chdir") 重推导,getcwd 失败即打印 chdir: error retrieving current directory: …,然后照常 chdir、照常成功。rubash 的 cd 入口没有这一步,全程静默。

GNU 锚点(bash 5.3):

  • builtins/cd.def:529-534 change_to_directory 入口:if (the_current_working_directory == 0) { t = get_working_directory ("chdir"); FREE (t); }
  • builtins/common.c:592-624 get_working_directory:getcwd 失败打印 for_whom: error retrieving current directory: getcwd: cannot access parent directories: …(bash_getcwd_errstr,general.c:70)
  • builtins/cd.def:353-388 CDPATH 循环(每单元一次 change_to_directory 调用)、cd.def:397-405 cdable_vars、cd.def:329-336 early return(too many / null directory 在 change_to_directory 之前返回)

修法(src/builtins/cd.rs,唯一产品文件)

execute_with_io 在 resolve_target / resolve_cdpath / resolve_cdable_vars 之后、move 探针之前插入入口重推导块:中毒态(logical_cwd is None,唯一置 None 处是 #3349 的中毒路径)时以 cwd.canonicalize()(session cwd 字段即本移植的 getcwd 真值)重推导——成功则无诊断且把缓存值恢复为物理拼写(与 GNU 一致);失败则按调用次数打印 chdir: 行,然后照常走 move。调用次数 = CDPATH 探过的单元数(命中则止;未命中则 +1 plain 尝试,+1(若 cdable_vars 值被替换));为此 resolve_cdpath 返回 (Option<Target>, usize)。early return(bad options / too many / HOME·OLDPWD 未设 / null directory)皆在块前,无行。

同态附带修正:deleted_dot_move 的 stayed_deleted_dot(#3335 的 cd: resetpwd 行)在中毒时置 None——GNU 中毒态下 make_absolute 的基是 NULL,./.. 的 canon 不可能因已删分量失败,verbatim chdir 后的 resetpwd 臂根本不跑(oracle p5/p12 只 1 行 chdir:、无 cd: 行)。未中毒态的 cd: 行不受影响。

Oracle 实测(GNU bash 5.3.20,脚本文件形):绝对 cd 中毒后恰 1 行;CDPATH 一败一中 2 行、两败 + plain 败 3 行;cdable_vars 成功 2 行;失败 cd 仍有 1 行;cd a b /cd "" 0 行;cd ./cd -P . 中毒后仅 1 行 chdir:、无 cd: 行;未中毒对照 0 行。

测试

  • 新钉 tests/issue3574_cd_deleted_cwd_chdir_diag.rs(Unix-only,10 cell:绝对 / CDPATH / . / .. / 无参 / cdable_vars / too-many / null / 未中毒对照)。
  • tests/issue3349_pwd_poisoned_logical_cache.rs cell5:期望 stderr 由 2×pwd: 改为 2×pwd: + 1×chdir:(该 cell 原称此行「owed separately」,现由本单支付)。

已知残差(本单不修,见 REPORT)

  • 相对操作数的绑定 $PWD 值(中毒态):GNU 绑 make_absolute 对 NULL 基的原样形(. / ..),rubash 绑 stale 基的逻辑目的——另立案。
  • pushd 面:GNU pushd 经 cd_builtin 移动,中毒态下也应报 chdir: 行;rubash pushd 不走 cd::execute_with_io——另立案。
  • 出生态(born-in-deleted-cwd):本单 gate(is_none())不覆盖,需单独分析。

本 PR 删除了什么

无。仅新增诊断输出与回归钉;resolve_cdpath 的返回由 Option<Target> 改为 (Option<Target>, usize),为私有函数,无公开面变化。

UNVERIFIED-BY-CAPTAIN

全部验证腿 OWED(远程构建路线全断:Simular box 钱包空 hibernated,老服务器与 X299 均须经 box 中转同断;本 VM 禁本地构建且内存低于构建门):钉 fail-first 红 / 修后绿、邻居 stem(#3349、#3335、#2796、#3394、#3395)、全量 lib 与 base 双边、RUSTFLAGS="-D warnings" cargo check --locked --lib --bins、GNU-vs-rubash 二进制对照。本地已跑:rustfmt --check(三文件)、check-marker-literals.py OK、git diff --check 干净。REPORT:~/workspace/fix-3574/REPORT.md。

caomengxuan666 and others added 3 commits November 15, 2026 17:05
…Refs #3574)

In the poisoned-cwd state (a failed `pwd -P` went through resetpwd
and left the_current_working_directory NULL, rubash#3349), GNU's
change_to_directory re-derives at entry through
get_working_directory ("chdir") (builtins/cd.def:529-534,
builtins/common.c:592-624): a failing getcwd prints
`chdir: error retrieving current directory: ...` and the move still
stands. rubash emitted no such line.

The entry fires once per change_to_directory call — the CDPATH
candidates in order, then the plain operand, then the cdable_vars
value (oracle: 1 failed CDPATH unit + hit = 2 lines; 2 failed units
+ plain fail = 3; cdable_vars success = plain + cdable_vars = 2) —
so resolve_cdpath now also reports how many candidates it probed,
and the entry block emits that many lines when the session logical
cache is poisoned (logical_cwd is None) and the re-derivation
fails. A succeeding re-derivation silently restores the cached
value, as in GNU. Early returns (bad options, too many arguments,
unset HOME/OLDPWD, null directory) never reach change_to_directory
and owe no line.

Companion correction in the same state: for a dot-only operand
GNU's make_absolute base is NULL, so canon cannot fail on a deleted
component and the verbatim chdir's resetpwd arm (the `cd:` line
rubash#3335 owes) never runs — the entry `chdir:` line is the only
diagnostic (oracle p5/p12). The stayed_deleted_dot diagnostic is
suppressed while poisoned; the unpoisoned face is unchanged.

Tests: new tests/issue3574_cd_deleted_cwd_chdir_diag.rs (10 cells:
absolute / CDPATH / dot / dotdot / no-arg / cdable_vars /
too-many / null / unpoisoned control); #3349 cell5 now expects
the `chdir:` line as well.

UNVERIFIED-BY-CAPTAIN: all verification legs OWED (remote build
routes down, no local builds) — nail fail-first/green, neighbour
stems (#3349, #3335, #2796, #3394, #3395), full lib vs base,
RUSTFLAGS="-D warnings" check, GNU-vs-rubash binary comparison.
Local-only checks done: rustfmt --check on the three touched files,
check-marker-literals.py OK, git diff --check clean.

本 PR 删除了什么:无。仅新增诊断输出与回归钉;resolve_cdpath
的返回由 Option<Target> 改为 (Option<Target>, usize),为私有
函数,无公开面变化。
… nail (Refs #3574)

The nail's poison_script runs both `pwd -P` and `pwd` after the rmdir, so
GNU 5.3.20 emits two GETCWD diagnostic lines before the cd entry's
`chdir:` line(s). Six cells asserted one; cell3 also asserted two pwd
faces while GNU's trailing pwd after the failed cd re-derives a third
time. Verified against the WSL GNU bash 5.3.20 oracle and the fixed
rubash binary (byte-identical, 2 pwd + 1 chdir / 2 pwd + 2 chdir).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3713 第2轮 — 建议:PASS
head: c1c3d82 base(merge-base): e81a1af 文件: 3 个 +407/-31
关联 issue: #3574(状态:open,我核过的时间:2026-11-16 19:20 CST)

机制核查:

  • 产品修法对:中毒态(the_current_working_directory == NULL,rubash 侧即 logical_cwd is None,[bug] 已删 cwd 下 pwd -P 失败后,默认 pwd / pwd -L 仍印陈旧逻辑串——GNU 经 resetpwd 毒化后同面转红 #3349 的 resetpwd 路径)下,GNU 的 change_to_directory 入口调 get_working_directory ("chdir")(builtins/cd.def:529-534),getcwd 失败经 builtins/common.c:592-624 打印 chdir: error retrieving current directory: … 后照常 chdir;成功则静默恢复缓存。本 PR 在 resolve_target / resolve_cdpath / resolve_cdable_vars 之后、move 探针之前插同样的入口重推导,并把 CDPATH 的候选数从 resolve_cdpath 带出以按 change_to_directory 调用次数打行——与 cd.def:353-388(CDPATH 循环)、cd.def:397-405(cdable_vars)、cd.def:329-336(too many / null directory 早退)的调用结构一致。
  • 同态附带修正(dot-only 操作数在中毒态不再走 stayed_deleted_dot 的 cd: 行)与 GNU 实测一致:中毒后 cd . / cd -P . 只有 chdir: 一行。
  • 可疑/无关改动:无。仅 src/builtins/cd.rs + 两个钉文件;resolve_cdpath 由 Option<Target> 改为 (Option<Target>, usize) 是私有函数,唯一另一调用点(resolve_search_operand,pushd 面)同步适配、行为不变。

续作记录(本轮动过代码):

  • c1c3d82 fix(tests): expect the poison script's two pwd lines in the issue3574 nail (Refs #3574)。
  • 原因:钉的 poison_script 里 pwd -P 与 pwd 各打一行 pwd 诊断,但 6 个 cell 只断言 1 行;cell3 还少算尾部 pwd 的第三行。产品无改动,只修钉的期望(以 GNU 5.3.20 实测为准)。
  • 新 head: c1c3d82(未 force-push,b044930b 之上叠加一个提交)。

腿(逐项给数字与名字):

  • 钉自跑(Linux,钉 #![cfg(unix)],Windows 上 0 格属平台限制):issue3574_cd_deleted_cwd_chdir_diag 10/10(修钉前 4/10,6 格因错误期望红);issue3349_pwd_poisoned_logical_cache 7/7。
  • fail-first(Linux,base e81a1af):同钉 3 passed / 7 failed,红名 = {issue3574_cd_absolute_after_poison_emits_one_chdir_line, issue3574_cd_dot_after_poison_emits_only_chdir_line, issue3574_cd_dotdot_after_poison_emits_one_chdir_line, issue3574_cd_failing_after_poison_emits_chdir_then_failure, issue3574_cd_no_arg_after_poison_emits_one_chdir_line, issue3574_cdable_vars_after_poison_emits_two_chdir_lines, issue3574_cdpath_failed_unit_then_hit_emits_two_chdir_lines};3 个通过的是 early-return / 未中毒对照(设计上 base 绿)。
  • 邻居(Linux):issue3335_cd_dot_deleted_cwd 15/15、issue2796_cd_symlink_dotdot_logical 12/12、issue3394_cdpath_tilde_regressions 8/8、issue3395_cd_physical_relative_cdpath_echo 5/5、issue3349 7/7。
  • lib 双边(Windows,先 build --bin):head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base(e81a1af) 失败名集 {同一条} / 差集 ∅(1351 passed / 1 failed 两边一致)。
  • -D warnings:cargo check --lib --bins --test issue3574_cd_deleted_cwd_chdir_diag = 0 警告。
  • 卫生:rustfmt --check(cd.rs + 两钉)= 0;marker-lint = OK(117 consts, 268 sites);utf8-boundary-hygiene = clean;git diff --check 干净。
  • CI(head c1c3d82,run 43853777477)已按名:GNU regression goldens and canaries 1354 passed / 1 failed、Rust tests (macOS) 1248 passed / 1 failed、Rust tests 同格——唯一红格皆 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(基线,日志里 4-5 处引用同一格);Regression pin stem census 10135 tests / 104 failed:其中 96 格与 master@d3f1ccef 的 census 失败集逐条相同,另 8 格(issue1902 ×1、issue371 ×5、issue754 ×2)属旧基底 census 面,我在 base(e81a1af) 与 head 上分别单跑这 3 个 stem 都是 全绿(8/8、9/9、11/11),且不含本 PR 新增 stem;新钉 issue3574_… 在 census 里 0 红。其余 job 全绿(含两个 cross-target check、Windows tests compile check、niubash build、API ledger tripwire)。
  • GNU 对照:WSL GNU bash 5.3.20(1)-release(脚本文件形)。6 个形状逐一与 head 二进制比对,stderr 逐字节一致:
    • 中毒后 cd <abs>:2×pwd + 1×chdir,rc=0;
    • cd . / cd ..:2×pwd + 1×chdir,rc=0(无 cd: 行);
    • cd(HOME):2×pwd + 1×chdir,pwd 随后成功;
    • CDPATH="/nonexistent:<r>"; cd dest:2×pwd + 2×chdir,CDPATH 公告与 pwd 输出一致;
    • shopt -s cdable_vars; v=<r>; cd v:2×pwd + 2×chdir;
    • cd /nonexistent:2×pwd + 1×chdir + cd: … 行,随后 pwd 再 1 行(共 3×pwd)。
      未中毒对照与 cd a b / cd "" 的 0 行行为也一致。
      平台注:GNU 侧为 Linux;Windows 上钉被 cfg(unix) 跳过,本 PR 的 cd 诊断面属 Unix 语义,Windows 结论待队长 Linux oracle 复核(我已在 WSL 跑过完整钉)。

F 点:无。

未跑/VOID 的腿:

  • Windows 钉自跑 = 0 格(钉 #![cfg(unix)],平台限制),已用 Linux 全跑替代。
  • census 的 8 个额外红格未追到 CI 环境内的确切触发条件(本地 base/head 单跑均绿);已如实列为旧基底 census 面,供队长复核。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 11:15
@caomengxuan666
caomengxuan666 merged commit de97682 into master Nov 16, 2026
8 of 12 checks passed
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