Skip to content

WIP: fix #2211 — comsub cd resolves against the session cwd - #2218

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2211-comsub-cd-stale-cwd
Oct 30, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2211-comsub-cd-stale-cwd

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2211.

Defect

cd "$BASE"; echo "$(cd sub && pwd -P)" failed with cd: sub: No such file or directory (or silently named the startup directory for ./.. operands), while the same body with any redirection on the inner cd, or in a plain subshell, worked. The un-redirected collect path and the redirected path disagreed about the cwd.

Mechanism

GNU keeps exactly one cwd: cd moves the process's (builtins/cd.def change_to_directory → chdir), and the child forked by subst.c:7143 command_substitute inherits it. Under G-02 the rubash process cwd never moves after startup — ShellState.cwd is the truth, and every spawn already receives it via Command::current_dir. Two in-process collect-path shortcuts still resolved bare operands against the process cwd:

  • the $(cd X && pwd) fast path (command_substitution_cd_pwd_output_impl) canonicalized the un-anchored target string;
  • the $(<file) quick read (comsub_cat_file_substitute) opened the un-anchored operand.

The redirected/subshell forms run the real builtin in the cloned executor, which reads the session field — hence the boundary the issue reported. No new transport was needed: the session field already is the transport (the #2188 lesson applied by analogy — the collect path simply never consulted it).

Changes

  • src/executor/command_substitution.rs
    • Both shortcuts anchor a relative operand at shell_state.cwd before resolving — the same join the cd builtin performs.
    • The cd/pwd fast path computes a pure pathname resolution, so operand forms whose GNU resolution is not one now decline to the real executor instead of being approximated: a leading - (cd options; the cd - OLDPWD keyword with its printed destination, cd.def:341-352), CDPATH-eligible names while CDPATH carries a nonempty unit (step-5 search + announcement), operands expanding to empty (cd.def:331-336 "null directory" vs the operand-less $HOME move), and bare names cdable_vars could still claim at the failure arms (LCD_DOVARS). Admission gates mirror resolve_cdpath / cdable_vars_could_apply; the operand-less $HOME arm is untouched (cd.def pins it to physical resolution).
  • src/builtins/cd/paths.rs — the //home rider: the exactly-two-slashes evidence for a logical destination is now the spelling the path was built from (the target's own spelling for an absolute move, the old directory's for a relative one), never the joined text. Joining /+etc manufactured a // neither side spelled (GNU binds /etc); joining //+etc read as a collapsible /// (GNU binds //etc). preserve_leading_double_slash itself is unchanged.
  • tests/issue2211_comsub_cd_stale_cwd_regressions.rs — 13 pins (below).

Pins

13 pins, GNU expectations, fixture tree with the launch directory different from every directory the script cds into:

  • Defect pins (red on master, green here): issue2211_comsub_cd_uses_current_cwd_after_prior_cd, issue2211_comsub_cd_follows_latest_cwd, issue2211_comsub_cd_dash_uses_oldpwd, issue2211_comsub_cd_bare_flag_is_home_move, issue2211_comsub_cd_cdpath_hit, issue2211_comsub_cd_cdable_vars_hit, issue2211_comsub_cd_empty_expansion_operands, issue2211_comsub_cd_failure_arms_unchanged (its Not-a-directory arm), issue2211_comsub_cat_file_relative_operand, issue2211_pwd_double_slash_only_from_spelling.
  • Controls (green on master and here): issue2211_comsub_cd_redirected_controls, issue2211_comsub_cd_plain_subshell_control, issue2211_comsub_cd_at_startup_cwd_control.

Local verification (Linux, debug)

  • Oracle battery (script files vs GNU bash 5.3.20): the minimal repro, ; form, CDPATH hit + announcement, cd -, bare cd -P/-L, empty-expansion operands, and the full /–//–/// matrix are byte-identical in stdout (and stderr, except the one pre-existing diagnostic-text item below).
  • cargo test --lib: 983 passed, 0 failed (includes the command-registry source-scan tests).
  • Adjacent targets: issue537 (2 passed), issue1184 f4 (7), f5 (2), f6 (5 passed, 5 ignored — the pre-existing ignores); issue485/issue659/issue392 targets are platform-gated off Linux (0 run). Full matrix is CI's job.

Adjacent divergences found, NOT in this PR (evidence in the cluster REPORT for separate filing)

  1. Literal cd "" / cd '' inside the shortcut: split_shell_words drops the empty quoted word, so the body reaches the operand-less $HOME arm; GNU reports "null directory". Word-formation layer, wider blast radius than this issue.
  2. The fast path renders the physical form for the bare (logical) pwd spelling — diverges only when the logical path traverses a symlink or a // root; pre-existing, different mechanism (needs the child's post-cd $PWD).
  3. The cd builtin's cdable_vars failure diagnostic names the variable (cd: via: ...); GNU names the attempted value (cd.def error tail prints the substituted path). Real-builtin path, not the comsub shortcut.

…ubash#2211)

GNU keeps exactly one cwd: cd moves the process's
(builtins/cd.def change_to_directory -> chdir) and the child forked
by subst.c:7143 command_substitute inherits it, so $(cd sub &&
pwd) resolves sub against the directory the shell most recently
cd'd to. Under G-02 the rubash process cwd never moves after
startup; the session cwd field is the truth every spawn already
receives via Command::current_dir. Two in-process collect-path
shortcuts still resolved bare operands against the process cwd:

- the $(cd X && pwd) fast path canonicalized the un-anchored
  target — after any earlier cd the inner cd failed with ENOENT
  against the startup tree, and ./.. operands silently named the
  startup directory;
- the $(<file) quick read opened the un-anchored operand.

Both now anchor a relative operand at shell_state.cwd first, the
same join the cd builtin performs.

The fast path computes a pure pathname resolution, so operand
forms whose GNU resolution is not one now decline to the real
executor (which runs the actual builtin in a cloned executor):
leading '-' (cd options and the OLDPWD keyword with its printed
destination, cd.def:341-352), CDPATH-eligible names while CDPATH
carries a nonempty unit (the step-5 search with its announcement),
operands expanding to empty (cd.def:331-336 null directory vs the
operand-less $HOME move), and bare names cdable_vars could still
claim at the failure arms (LCD_DOVARS).

Same issue, the $PWD seam: the exactly-two-slashes evidence for a
logical destination is the spelling the path was built FROM — the
target's own spelling for an absolute move, the old directory's
for a relative one — never the joined text. Joining "/"+"etc"
manufactured a // neither side spelled (GNU binds /etc); joining
"//"+"etc" read as a collapsible /// (GNU binds //etc).

Regression pins: tests/issue2211_comsub_cd_stale_cwd_regressions.rs
(13 pins; 10 red on master, 3 controls green on master).

Refs #2211.
…rc 0 (rubash#2211 follow-up)

The session-cwd anchor added for #2211 regressed the catfile-comsub
golden's p3/q9 rows: joining the operand onto the session cwd turns
an empty operand into the session directory itself (Path::join("")
returns the base), so `$(< "")` / `$(< "$uf")` landed on the is_dir
arm and read as silent empty with rc 0.

GNU opens the redirect operand as spelled: builtins/evalstring.c:759
open_redir_file() runs open(fn, O_RDONLY) on the word from
redir.c:298 redirection_expand(); open("") fails ENOENT and
internal_error prints ": No such file or directory", the
substitution yields empty with EXECUTION_FAILURE
(subst.c:7169-7173). The anchor is a resolution context, not a
concatenation: an empty word names nothing in any directory, so it
now reaches the open attempt un-joined and fails there like any
other unopenable name — the pre-anchor behavior for this one word.
The real executor path on the same build already produced the GNU
bytes for every empty-operand shape; the quick read now mirrors it.

Pin: issue2211_comsub_cat_file_quoted_empty_operand — red on the
anchored code (exactly the three quoted-empty rows flip to rc=0;
the unquoted-empty ambiguous-redirect row and the nonempty control
do not move), green with the fix. On un-fixed master the shapes
pass only because the un-anchored open of "" fails on its own.
golden_catfile_comsub passes locally with the fix (the test is not
platform-gated; the CI lane exercising it is the Windows goldens
job).

Refs #2211.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 30, 2026 13:42
@caomengxuan666
caomengxuan666 merged commit ceed7c5 into master Oct 30, 2026
10 of 11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2211-comsub-cd-stale-cwd branch October 30, 2026 13:42
caomengxuan666 added a commit that referenced this pull request Oct 30, 2026
…ub cd fast path

The $(cd X && pwd) fast path returned canonicalize() output — the physical
path — for both pwd spellings. GNU's pwd_builtin with no -P prints $PWD,
the logical chain cd_builtin maintains (builtins/cd.def: bindpwd stores
the logical spelling; the -L default keeps `..` and symlink components
unresolved). So $(cd link && pwd) printed the resolved target directory
where GNU prints the path through the link, and $(cd //etc && pwd) lost
the POSIX double-slash root.

The physical probe still gates the fast path (existence / is-dir), but
the bare-pwd rendering now goes through the builtin's own chain —
logical_destination_display over validated_logical_pwd, exposed from the
cd module as logical_destination_display_from — so the fast path cannot
drift from the builtin it models; pwd -P keeps the physical rendering.
Rendering (rather than declining logical spellings) also avoids
re-expanding the operand: declining would run a side-effecting operand
twice (once in the fast path's probe expansion, once in the real
executor).

Known stacking dependency: logical_destination_display itself carries a
// seam on master (old="/" + relative operand manufactures //, old="//"
+ relative collapses); PR #2218 fixes that helper. On this base the fast
path and the builtin agree on every probed cell because they share the
helper; the cd // session cells verify fully once #2218 is in the base.

Pins: 9 tests in
tests/issue2220_comsub_pwd_logical_rendering_regressions.rs (5 red on
master, 4 controls green both ways; symlink fixtures are unix-only,
matching the #2211 cluster's local-dev test target).

Refs #2220
caomengxuan666 added a commit that referenced this pull request Oct 30, 2026
Master carries PR #2218 (rubash#2211: comsub cd session-cwd anchoring, operand decline gates, logical_destination_display // seam fix in paths.rs), PR #2224 (census) and PR #2226 (walker) since this branch forked at 01836d3.

One conflict, in command_substitution_cd_pwd_output_impl (src/executor/command_substitution.rs), resolved as the union of both sides per the cluster REPORT overlap map:
- operand read stays quote-aware (split_shell_words_with_quote_info, #2219): the raw-empty decline is decided pre-expansion;
- #2211's post-expansion declines kept (leading '-', CDPATH search, expanded-empty) and operand_form is set only after every decline has passed;
- #2219's unquoted-blank (field-splitting) decline kept;
- downstream untouched: session-cwd anchoring (#2211), the cdable decline keyed on operand_form, and the #2220 logical rendering tail over paths.rs's seam-fixed logical_destination_display.

Re-verified on the merged tree (Linux): issue2219 9/9, issue2220 9/9 (incl. the //etc pin), issue2221 5/5, issue1415 adjacency 1/1, issue2211 14/14 (the session-// + relative-operand seam cells are pinned there), --lib 983 pass / 0 fail.
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