Skip to content

fix(native-path): fold the POSIX drive dialect in canonicalize_path_w (#1231) - #1245

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/1231-canonicalize-drive-dialect
Oct 19, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/1231-canonicalize-drive-dialect

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #1231.

Problem

canonicalize_path_w (native_path.cppm) — the shared component-wise resolver behind realpath, readlink -f/-e/-m and stat -L — walked POSIX drive-dialect operands (/e/Data) as if they had no root name: the component walk started at the current directory, so the operand stranded on the cwd drive. From any cwd, realpath /e literally resolved <cwd>\e; with a foreign cwd the whole surface reported ENOENT for files that exist.

The argv operand boundary (normalize_api_operand_w) has folded the drive form since #1145, but these three resolvers receive the raw operand text and never pass through that boundary.

Fix (shared layer, one place)

canonicalize_path_w has exactly three callers (realpath, readlink, stat), so the blast radius is the broken surface itself.

Verification (PS/cmd native argv; MSYS-channel probes fake-green, #1230)

Probe tree E:\probe-w1 (Data dir, file, junction), cwds C:\Users\cmx and D:\repo, red on a fresh e991ba8 build, green on this branch. Oracles: PowerShell Resolve-Path (Win32) + WSL GNU coreutils (/mnt/e/...) three-way agreement; the local "Git Bash" is Niubash and pre-translates dialect argv, so it was used only as an in-shell convention reference, never as a probe channel.

operand (cwd=C:, cwd=D:) baseline e991ba8 this branch
realpath /e/probe-w1/Data ENOENT E:\probe-w1\Data
readlink -f /e/probe-w1/Data ENOENT E:\probe-w1\Data
stat -L /e/probe-w1/Data cannot statx OK
stat /e/probe-w1/Data cannot statx OK
realpath /e <cwd>\e (wrong) E:\
stat -f /e/probe-w1/Data cannot statx OK
realpath /e/probe-w1/jlink (reparse) ENOENT resolves to target

Health layer, same matrix, no degradation on either build: ls / du / cat / cp / cygpath / mktemp with /e/..., /cygdrive/e/... and /mnt/e/... operands all unchanged; verbatim shell-namespace surface intact (ls /usr/bin incl. the X.exe leaf rule, realpath /etc echoes the mount name, cat /dev/null empty with exit 0, and /e/... still resolves natively with a shell root configured).

New regressions: readlink (2), realpath (4, incl. bare /x = drive root, -m native spelling, /nodrive stays a POSIX word) and stat (4, incl. lstat/-L dialect behavior; symlink cases skip on non-elevated hosts).

ctest

Full suite via scripts/build.py --run-tests (MSVC/Ninja/Debug): 2992/2992 green on the final state of this branch. Two earlier runs showed b2sum_check_reports_unreadable_listed_files, head_count_suffixes, head_missing_file_reports_gnu_shaped_open_error and mktemp_p_cygdrive_dir_outputs_cygdrive_form failing non-deterministically; all four pass in isolation (repeated x5) and the same flake signature appears on the untouched e991ba8 baseline build (its suite run failed mktemp_p_cygdrive_dir_outputs_cygdrive_form too), so none are introduced here.

Known adjacent gaps (out of scope, unchanged by this PR)

…#1231)

canonicalize_path_w - the shared resolver behind realpath, readlink -f/-e/-m
and stat -L - walked '/e/Data' operands as if they had no root name, so the
component walk started at the current directory and every dialect operand
stranded on the cwd drive ('realpath /e' from a foreign cwd literally
resolved '<cwd>\e'; PS probe with native argv, #1230 discipline).

Single-source the recognition rule: extract the drive fold that
normalize_api_operand_w (#1145) applied inline into fold_drive_dialect_w,
and consult it in canonicalize_path_w before the component walk and before
the shell-tree mapping (shell_tree_map_w already declines drive forms). The
rule stays 'first segment is exactly one drive letter': /cygdrive/X (#1145)
and /mnt/X (#1172) keep folding through their own branches, multi-letter
first segments (/usr, /etc, /mntcc) are never drives, and the verbatim
exemption surface gains nothing.

stat.cpp routes its operand through make_api_path_operand_w like every
other consumer, so the lstat probe, -L resolution, the symlink display line
and -f report the named file instead of a cwd-stranded ENOENT.

New regressions: readlink/realpath/stat dialect operands resolve from a
foreign cwd, bare '/x' is the drive root, '-m' keeps the native spelling,
multi-letter first segments stay ordinary POSIX words, and stat lstat/-L
report dialect symlinks.
@caomengxuan666
caomengxuan666 force-pushed the fix/1231-canonicalize-drive-dialect branch from 3da7218 to 05abdc5 Compare October 19, 2026 05:07
@caomengxuan666

Copy link
Copy Markdown
Member Author

CI run 1: x64 failed only stat.stat_dereference_follows_drive_dialect_symlink (2991/2992). The fix itself worked — CI�s output shows stat -L /c/.../link exiting 0 with the target�s metadata (Size: 4 ... regular file). The test expectation was wrong: it looked for the target name "f.txt" in the output, but [GNU] stat echoes the operand as typed ("link") and never prints the target name under -L. The expectation had never executed locally because symlink creation requires privileges (tests skip on non-elevated hosts); CI runners can create symlinks and exercised it for the first time.

Fixed in 05abdc5: assertions now check the GNU shape — operand echoed, target�s size/regular-file metadata, and no lstat -> display — matching the observed CI output verbatim. Re-running CI.

@caomengxuan666

Copy link
Copy Markdown
Member Author

Independent code review — verdict: APPROVE-WITH-NITS

Reviewed independently from a fresh worktree at 05abdc5, with a merge-preview against origin/main (3c5d9ad, includes #1243): merge is clean (ort, no conflicts; #1243 touches mkdir/ln/CHANGELOG + their tests, zero file overlap with this PR's stat.cpp / native_path.cppm / readlink+realpath+stat tests).

Verified (all re-executed, PS native argv per #1230; my own "Git Bash" channel was discarded as an oracle once it proved to be Niubash with MSYS argv pre-translation — exactly the fake-green trap this PR calls out)

  1. Extraction equivalence: fold_drive_dialect_w vs the inlined normalize_api_operand_w logic — token-for-token identical (shell_ns::is_drive_form == the old condition: separator[0] + one drive letter + size==2 || is_separator([2]); body: upcase, X:\, substr(3) when size>3, normalize_separators). No behavior drift; rule is now single-sourced.
  2. Fold timing in canonicalize_path_w: applied before the shell-tree mapping (else-if; shell_tree_map_w's drive-form decline predates the PR at e991ba8, so no double mapping) and before the component walk, so /e gains a root name and stops stranding on cwd. Probed: /e/.. → E:\, trailing slash /e/prog/ → directory-correct, bare /e → E:\, /-rooted no-drive operands and //e/x untouched, /mntcc and /nodrive stay ordinary POSIX words.
  3. Nonexistent drive (/q, no Q:): lexical fold → Q:\ (rc 0), -m /q/foo → Q:\foo, permissive /q/foo → Q:\foo rc 0. Consistent with MSYS2 drive-automount semantics and with the Per-command gaps resolving verbatim POSIX argv: cp/mv/ls(file,-d) fail on /d/... drive paths while cat/mktemp/ls(dir) resolve them (blocks niubash#124 Option B) #1145 argv-boundary convention — right call for this codebase.
  4. Baseline-vs-PR matrix diff (E:\probe-cr1245 tree, foreign cwd C:\Windows, shell root configured (machine env) and explicitly cleared): all dialect rows flip red→green exactly as claimed (realpath /e/... ENOENT→E:\..., readlink -f, stat, stat -L, junction traversal A2 → E:\probe-cr1245\prog); verbatim rows (/usr/bin, /etc, /dev/null, /cygdrive/e/..., /mnt/e/..., /mntcc) byte-identical on both builds in both configurations, with one intended exception → nit 2.
  5. Caller closure: command-layer callers are exactly realpath/readlink/stat (realpath.cpp:149, readlink.cpp:152, stat.cpp:1098) — see nit 1 for a wording caveat. No double-fold with fix(mkdir,ln): fold POSIX drive-dialect operands before any write (#1233, #1234) #1243: normalize_api_operand (mkdir 裸盘符根 /x 形态在 cwd 盘创建/误报——创建主路径未过盘符折叠(mkdir -p 会真建影子目录) #1233/ln 裸盘符根 /x 形态被 std::filesystem::absolute 拼接到 cwd——链接解析盘符随 cwd 漂移 #1234) folds at the mkdir/ln entry points, this PR folds in the resolver; paths that pass through both are native-spelled by then and is_drive_form no longer matches.
  6. stat.cpp: minimal, correct — all probes (lstat is_link, -L canonicalize, read_symlink) moved to operand.normalized, display line keeps echoing the raw operand (GNU %n semantics).
  7. Regressions: 10 new cases (readlink 2 / realpath 4 / stat 4) pass locally; the 2 stat symlink cases skip gracefully on non-elevated hosts (1314). Full suite on the merge-preview tree (05abdc5+fix(mkdir,ln): fold POSIX drive-dialect operands before any write (#1233, #1234) #1243): 3004 tests, single failure mktemp_p_cygdrive_dir_outputs_cygdrive_form — one of the four flakes disclosed in the description; passes x3 in isolation here and also on the untouched e991ba8 baseline build. Not introduced by this PR.
  8. Hitchhiking scan: single commit over e991ba8, 5 files, all substantive; no format churn. Predecessor WIP discard verified lossless: native_path.cppm, stat.cpp, readlink_unit_test.cpp in the discarded diff are byte-identical to what landed (same blob pairs); the discarded cp.cpp (spot-checked) was pure clang-format reflow, safe to have restored to e991ba8.
  9. Health layer: ls/du/cat/cp/cygpath/mktemp rows (incl. /usr/bin leaf rule, /dev/null) byte-identical baseline vs PR except mktemp's random suffix. No degradation. CI: current checks green (build x64/arm64, compare, oracle); history 1 failed → 3 passed on the same single commit.

Nits (non-blocking)

  1. "exactly three callers" is slightly imprecise: winux_fifo_pipe_name_w (native_path.cppm:827, FIFO pipe rendezvous naming) is a 4th internal caller. Harmless — its inputs are pre-normalized through make_api_path_operand in file_io.cppm, so the fold is a no-op there — but the PR description's blast-radius sentence could acknowledge it.
  2. Untested intended side effect: with a shell root configured, stat /usr/bin changed behavior (baseline ENOENT → now resolves through the shell tree, aligning stat with ls/readlink -f and the oracle). Correct and desirable, but it has no regression row — worth a follow-up test (e.g. stat on a shell-namespace operand with WINUXSH_ROOT set) and arguably a CHANGELOG line (this PR currently carries none, unlike mkdir 裸盘符根 /x 形态在 cwd 盘创建/误报——创建主路径未过盘符折叠(mkdir -p 会真建影子目录) #1233/ln 裸盘符根 /x 形态被 std::filesystem::absolute 拼接到 cwd——链接解析盘符随 cwd 漂移 #1234).
  3. realpath /cygdrive/e/... gap — please file the follow-up ticket. Confirmed: realpath -m /cygdrive/e/... still strands (identical on baseline and this branch, both with and without a shell root), while stat on the same spelling works via the argv boundary — the two spellings disagree across commands today. The fix belongs in canonicalize_path_w (run the cygdrive/mnt prefix folds before the single-letter fold, mirroring normalize_api_operand_w's sequence). Pre-existing, correctly declared out of scope here; /mnt/e/... under an unconfigured root has the same shape.

@caomengxuan666
caomengxuan666 merged commit 6e8ea19 into main Oct 19, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1231-canonicalize-drive-dialect branch October 21, 2026 08:17
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.

realpath/readlink -f 裸盘符根 /x 形态静默拼接 cwd——canonicalize_path_w 共享入口缺盘符折叠

1 participant