Skip to content

fix(cd): share the deleted-cwd dot-only move between -L and -P, trust stored logical cwd - #3386

Merged
caomengxuan666 merged 2 commits into
unixwin:masterfrom
Uarz:fix/issue-3335
Nov 12, 2026
Merged

caomengxuan666 merged 2 commits into
unixwin:masterfrom
Uarz:fix/issue-3335

Conversation

@Uarz

@Uarz Uarz commented Nov 11, 2026

Copy link
Copy Markdown
Member

Fixes #3335

What was fixed

Honest note first: the issue's two title symptoms (cd . in a deleted cwd — pushd entry fails rc 1; cd entry lacks the getcwd diagnostic and the PWD /. suffix) were already fixed at HEAD by #2796's 20f9fb5 — reproducing them on f5f65d3 gives rc 0 + getcwd diagnostic + PWD /. on both entries, matching GNU 5.3.20. Digging the same mechanism family found three live residuals, fixed here by mechanism, not per case:

  1. -P has no dot-only arm (src/builtins/cd.rs): cd -P . / cd -P ./ fell into the anchor fallback, silently binding the physical path with no diagnostic. The Logical branch's dot-only arm is now a shared deleted_dot_move closure used by both modes, with the physical cwd as the make_absolute base under -P (GNU under -P binds the physical spelling; probe confirms a symlink entry binds $D/a/real/.) and the logical base under -L, both checked before the anchor fallback.
  2. Logical spelling lost in a deleted cwd (old_logical derivation): logical_value_names_cwd and validated_logical_pwd both require a successful canonicalize, which always fails in a deleted cwd, so the code fell back to the physical cwd and a symlink entry's cd . bound $D/a/real/. instead of GNU's $D/link/.. Now, when the cwd itself fails to canonicalize, the stored logical_cwd is trusted (it cannot be falsified in a deleted cwd; GNU's tcwd is exactly the stored string, common.c:589).
  3. pushd/popd do not maintain session cwd state (execute_stack_builtin): pushd never wrote logical_cwd and never warmed the cwd inode anchor, so cd .. / cd -P .. / cd -P . from a pushd entry in a deleted cwd failed rc 1. On a successful move (status 0 and cwd changed) the funnel now sets logical_cwd to the bound PWD and refreshes the anchor — the same maintenance execute_cd does; -n and dirs do not move the cwd, so the gate skips them automatically.

GNU: change_to_directory verbatim-chdirs newdir when canon fails, and under -P the verbatim chdir is the first attempt (builtins/cd.def:522); dot-only operands succeed on the cwd inode, resetpwd("cd") emits the getcwd diagnostic (cd.def:183), set_working_directory(tdir) binds the uncanonicalized make_absolute form, giving PWD its /. suffix; get_working_directory prints {caller}: error retrieving current directory: getcwd: cannot access parent directories: {strerror} (builtins/common.c:592); tcwd is a stored string never revalidated against the filesystem (common.c:589); pushd's Dir operand goes through cd_builtin (builtins/pushd.def:296-302), so pushd is a cd-family mover and must maintain the same session cwd state.

Merge note: this PR touches execute_stack_builtin in src/executor/trap_stack_builtins.rs, the same function #3336 (PR #3384) touches — different hunks (lines ~497 vs ~511 on the base), so git should merge them, but the captain should review the combined function since both change what the funnel maintains on a move.

What was deleted

Nothing. No files, features, or tests were removed.

Verification (all run, per-item counts)

Owed

  1. Relative-path redirect file creation in a deleted cwd: cd "$PWD" 2>err.txt (relative path) reports err.txt: No such file or directory in rubash; GNU creates the file through the cwd inode. This is an independent redirect-creation divergence, not part of [bug] cd . in a deleted cwd: pushd entry still fails rc 1; cd entry lacks getcwd diagnostic and PWD /. suffix #3335's cd face; the pin's control cells therefore assert the diagnostic tail with absolute paths. Suggest a separate issue.
  2. pushd/popd bindpwd-readonly edge: with PWD/OLDPWD readonly, GNU's chdir still succeeds and tcwd is updated; this change's maintenance gate is on status == 0, so that edge still leaves stale state. Fixing it needs the logical value from the stack top rather than env PWD; left for a follow-up.

… stored logical cwd

The issue's two title symptoms (cd . in a deleted cwd: pushd entry fails rc 1; cd entry lacks getcwd diagnostic and PWD /. suffix) were already fixed at HEAD by unixwin#2796's 20f9fb5 — reproducing them on f5f65d3 gives rc 0 + getcwd diagnostic + PWD /. on both entries, matching GNU 5.3.20. Digging the same mechanism family found three live residuals, fixed here by mechanism, not per case:

1. -P has no dot-only arm (src/builtins/cd.rs): 'cd -P .' / 'cd -P ./' fell into the anchor fallback, silently binding the physical path with no diagnostic. The Logical branch's dot-only arm is now a shared deleted_dot_move closure used by both modes, with the physical cwd as the make_absolute base under -P (GNU under -P binds the physical spelling; probe confirms a symlink entry binds $D/a/real/.) and the logical base under -L, both checked before the anchor fallback.
2. Logical spelling lost in a deleted cwd (old_logical derivation): logical_value_names_cwd and validated_logical_pwd both require a successful canonicalize, which always fails in a deleted cwd, so the code fell back to the physical cwd and a symlink entry's 'cd .' bound $D/a/real/. instead of GNU's $D/link/.. Now, when the cwd itself fails to canonicalize, the stored logical_cwd is trusted (it cannot be falsified in a deleted cwd; GNU's tcwd is exactly the stored string, common.c:589).
3. pushd/popd do not maintain session cwd state (execute_stack_builtin): pushd never wrote logical_cwd and never warmed the cwd inode anchor, so 'cd ..' / 'cd -P ..' / 'cd -P .' from a pushd entry in a deleted cwd failed rc 1. On a successful move (status 0 and cwd changed) the funnel now sets logical_cwd to the bound PWD and refreshes the anchor — the same maintenance execute_cd does; -n and dirs do not move the cwd, so the gate skips them automatically.

GNU: change_to_directory verbatim-chdirs newdir when canon fails, and under -P the verbatim chdir is the first attempt (cd.def:522); dot-only operands succeed on the cwd inode, resetpwd('cd') emits the getcwd diagnostic (cd.def:183), set_working_directory(tdir) binds the uncanonicalized make_absolute form, giving PWD its /. suffix; get_working_directory prints '{caller}: error retrieving current directory: getcwd: cannot access parent directories: {strerror}' (common.c:592); tcwd is a stored string never revalidated against the filesystem (common.c:589); pushd's Dir operand goes through cd_builtin (pushd.def:296-302), so pushd is a cd-family mover and must maintain the same session cwd state.

Refs unixwin#3335

Regression pin tests/issue3335_cd_dot_deleted_cwd.rs (15 tests; fail-first 7 passed / 8 failed on the unpatched tree, 15/15 pass after; supervisor verified both rounds with the patch method). The 8 red cells are exactly the three residuals: cd -P ., cd -P ./, symlink -P ., symlink cd . (logical spelling), pushd-symlink cd ., pushd-entry cd .., cd -P .., cd -P .; the 7 green cells lock the already-fixed title symptoms and controls. Two GNU probe matrices (cd ./, ./., -L, -P, both symlink entries, pushd-entry ../-P ../-P ., controls) are byte-identical with GNU 5.3.20 after the fix. Adjacent: unixwin#2796 12/12, unixwin#2797 7/7, unixwin#2798 5/5, unixwin#2799 7/7, unixwin#2143 8/8, unixwin#3268 cd-redirect 19/19, unixwin#3268 birth 12/12, unixwin#3198 7/7, unixwin#3275 4/4, unixwin#2391 6/6, unixwin#2408 4/4, unixwin#3196 6/6, unixwin#3197 3/3, unixwin#2937 24/24, unixwin#2220 9/9, unixwin#1415 1/1, unixwin#392 2/2 (supervisor re-ran unixwin#2796, issue3198, unixwin#2937). cargo fmt clean.
@caomengxuan666 caomengxuan666 added the bug Something isn't working label Nov 11, 2026
@caomengxuan666 caomengxuan666 self-assigned this Nov 11, 2026
@caomengxuan666 caomengxuan666 added gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger P3-low 低:优化、文档、远期规划,有机会再做 area:path path model, command lookup, drive/POSIX spellings, argv translation labels Nov 11, 2026
@caomengxuan666

Copy link
Copy Markdown
Member

Thanks for the PR. This is queued for captain review — collaborator PRs are reviewed by the captain before merge, and #3335 stays open until that review lands.

CI at intake: all 10 checks green (1 skipping). First pass: the honest correction in the body is noted (the two title symptoms were already fixed at HEAD by #2796) — the PR fixes the three live residuals by mechanism, pin 15/15 fail-first (7 passed / 8 failed on the unpatched tree, exactly the 8 residual cells). Two things noted for the captain: your Owed items (relative-path redirect creation in a deleted cwd; the bindpwd-readonly edge) are follow-up candidates, and the merge note is recorded — this PR and #3384 touch the same execute_stack_builtin function, so the combined function needs one review. No action needed from you.

@caomengxuan666

Copy link
Copy Markdown
Member

Captain review — PASS (merge conditions below)

Joint review with PR #3384 (same funnel, one dossier: review-3384-3386/CAPTAIN-VERIFY.md). All numbers are the reviewer's own runs on the old server (tree identity hash-verified against the PR head edbc0249; real merge-base f5f65d35 computed with git).

Declaration reconciliation — every claim checks out, including the honest note

  • The note that the issue's two title symptoms were already gone at HEAD is accurate: on the base, the cd-entry and pushd-entry cd . probe shapes already match GNU byte-for-byte (the [bug] cd 对软链父目录的 .. 按物理路径解析,默认与 -L 下均偏离逻辑目录 #2796 landing covers them); the pin's base run keeps those cells green as locks.
  • Pin bilateral: head 15 passed / 0 failed; base (top-up form) 7 passed / 8 failed — the eight red cells are exactly the three residuals named in the body (the -P dot-only forms, the symlink logical-spelling forms, the pushd-entry maintenance forms), per-name.
  • Lib: 1245 passed / 0 failed / 4 ignored, name sets identical across base and both heads.
  • Adjacent stems re-run at the declared counts: issue2796 12/12, issue2797 7/7, issue2798 5/5, issue2799 7/7, issue3198 7/7, issue2937 24/24.
  • fmt, marker-lint, UTF-8 hygiene clean; full-target -D warnings shows no error under src/ and no new point from this diff.
  • Three-way probes (GNU 5.3.20 vs base vs head): every B-shape in the matrix (entry × mode × symlink) matches GNU on the head, including the physical spelling under -P and the logical spelling under -L. GNU anchors re-read in the 5.3 sources (cd.def:522 verbatim arm, cd.def:183 resetpwd, common.c:589/592) are accurate.

Two things the merge round must know

  1. The merge note in this body is optimistic: the two stack-funnel changes do not auto-merge. A trial merge conflicts in trap_stack_builtins.rs. The union shape is pre-verified in the dossier — anchor refresh gated on the move itself (the companion PR's gate, which also covers bind-failure moves), logical-cwd maintenance gated on a successful move (this PR's gate) — and a combined tree passes both pins (7/7 and 15/15) and the full probe matrix. Whoever merges second applies that shape on update-branch and re-runs both pins on the merged head.
  2. Deletion accounting: the body says nothing was removed, which holds for files, features, and tests. For the record, the cd.rs diff does retire the Logical branch's inline dot-only arm (−35 lines) in favor of the shared deleted_dot_move closure the body describes; the merge body states this explicitly so the landed record is exact.

Owed items — captain delimitation (evidence in the dossier)

  1. Relative-path redirect creation in a deleted cwd: the premise does not hold. Measured on the GNU oracle in five variants (external and builtin redirects, the exact cd "$PWD" 2>err.txt shape, and a redirect attached to a move toward a surviving absolute target), GNU declines to create the file in every variant — err.txt: No such file or directory, status 1, file absent; a deleted directory cannot gain entries. Base, this head, and the combined tree all match GNU byte-for-byte on those shapes. No divergence, nothing to file; the pin's use of absolute paths for its diagnostic control is the right call.
  2. The bindpwd-readonly edge is real but narrower than stated, and one part of it is introduced here: after a readonly pushd whose bind fails (status 1, move standing) into a directory that is then deleted, a following cd . on this head — and on the combined tree — emits the readonly diagnostics but loses the getcwd diagnostic that both GNU and the base print, while status and PWD still match GNU. The companion PR's move-gated anchor refresh already covers the anchor half of this family (its probe C5 passes on the combined tree and fails on this head alone, where the status-0 gate skips the bind-failure move). Recommendation recorded in the dossier: one family item for the bindpwd-readonly maintenance (acceptance shape: the getcwd diagnostic present in that corner), rather than separate items per PR.

Merge conditions: final head CI finished all-green (the current head already is); if this PR merges second, the union protocol above applies.

# Conflicts:
#	src/executor/trap_stack_builtins.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:path path model, command lookup, drive/POSIX spellings, argv translation bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger P3-low 低:优化、文档、远期规划,有机会再做

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] cd . in a deleted cwd: pushd entry still fails rc 1; cd entry lacks getcwd diagnostic and PWD /. suffix

2 participants