Skip to content

[mkdir] -p 接盘符绝对路径:同盘 cwd 下「D:/…」按盘符相对落盘(目标不建、cwd 长树),反斜杠写法又不建父级 #1870

Done

Description

@qwased

环境

  • WinuxCmd 1.2.2(v1.2.2,当前最新 release)
  • Windows 11 10.0.26300
  • 证据方式:PowerShell 原生 argv 直接调用 mkdir.exe(无 MSYS、无 shell 包装、无引号展开):& '<install>\winuxcmd\usr\bin\mkdir.exe' -p '...'

摘要

mkdir -p 对「盘符开头的绝对操作数」有两处写侧缺陷,都会让创建落到非预期位置:

# 症状 严重度
D1 cwd 与操作数同盘时,D:/a/b/c 形态被当成盘符相对路径:在 cwd 下建出 .\a\b\c,D:\a\b\c 根本没建,rc=0 静默成功 高:静默放错位置,脚本以为目录已建好
D2 D:\a\b\c 反斜杠形态下 -p 不创建中间父级,直接 No such file or directory(rc=1) 高:-p 语义失效

原生 POSIX 方言(/d/a/b/c)反而是正确的:落盘正确、cwd 无杂项。也就是说问题专属于 X:/... 与新式反斜杠 X:\... 两种盘符绝对写法。

D1 复现(落盘错位)

$mk = '<install>\winuxcmd\usr\bin\mkdir.exe'

# 前置:D:\probe 为空的 cwd,D:\wt 事先不存在
New-Item -ItemType Directory 'D:\probe\rep1' -Force | Out-Null
Push-Location 'D:\probe\rep1'
& $mk -p 'D:/wt/x/y'
"rc=$LASTEXITCODE"
"D:\wt\x\y            -> $(Test-Path 'D:\wt\x\y')"             # 期望 True
"D:\probe\rep1\wt\x\y -> $(Test-Path 'D:\probe\rep1\wt\x\y')" # 期望 False
Pop-Location

实测(cwd 在 D:):

rc=0
D:\wt\x\y            -> False   # ← 目标没建
D:\probe\rep1\wt\x\y -> True    # ← 建到 cwd 下面去了

把 cwd 换到另一个盘(C:\probe\rep2)跑同一条命令 -p 'D:/wt/x/z':D:\wt\x\z 正确创建,cwd 无新增。即触发条件是 cwd 与操作数同盘,落点按 D:wt/x/y(盘符相对)解释。

D2 复现(-p 不建父级)

Push-Location 'D:\probe\rep1'
& $mk -p 'D:\wt2\sub\leaf'      # D:\wt2 事先不存在
"rc=$LASTEXITCODE"              # 1
# mkdir: cannot create directory 'D:\wt2\sub\leaf': No such file or directory

New-Item -ItemType Directory 'D:\wt2' | Out-Null
& $mk -p 'D:\wt2\sub\leaf'      # 首个父级存在了,但 sub 仍需创建
"rc=$LASTEXITCODE"              # 仍为 1,同样的 ENOENT
Pop-Location

结论:反斜杠绝对路径的 -p 不创建任何中间目录,只有「父级已全部存在」时才会成功;同一路径改成正斜杠 D:/wt2/sub/leaf 则 rc=0——但会触发 D1 的落盘错位。

期望

  1. mkdir -p 'D:/a/b/c' 与 mkdir -p 'D:\a\b\c' 语义一致:在 D: 上创建 a\b\c 全链,不得在 cwd 产生任何条目。
  2. 盘符绝对操作数不应依赖 cwd 所在盘解析(D1 里 cwd 一换盘结果就不同,说明解析走了盘符相对分支)。
  3. -p 必须创建中间父级(D2)。
  4. 若无法正确解析,宁可报错,也不要静默建到别处。

疑似病根(未做代码级验证,仅供参考)

  • rust2/crates/winux-cmds/src/mkdir.rs 的 create_parents(folded 分支在其函数体前段)在「按折叠后的盘符重新锚定」之后重建父链,锚定/拼接处可能把 D:/a/b/c 的第一段当成相对段;
  • rust2/crates/winux-cmds/src/canon.rs 里 prepare_operand 的 carrier-colon 折叠(测试 p("D:name") -> "D␺name"、p("D:") -> "D:")说明「裸盘符」与「盘符 + 名字」走的是不同分支;D1 的表现像是 D:/wt/x/y 落进了后者的相对解析;
  • C++ 侧对应点:src/utils/native_path.cppm 的 fold_carrier_colons_w / normalize_api_operand_w。

若 1.2.2 实际走的是另一支实现,请按行为而不是按上面文件定位。

与已有 issue 的关系

附:POSIX 宿主下的第三种表现(交叉参考,另行开在 niubash)

同一 exe 经 Niubash 1.5.1 的 niu -c 调用时(niu 会把 /d/... 转成原生形态喂给子进程),目标目录会正确创建,但 cwd 里会多出一个空目录 D+U+F03A(carrier-colon 折叠的产物):

mkdir -p /d/work/proj/out    # 目标 OK,cwd 多出 D<U+F03A>
mkdir -p "D:/work/proj/out"  # 同上
mkdir -p /d                  # 目标就是盘根、无需创建,仍会多出

同样的 /d/... 拼写用 PowerShell 原生 argv 直调则完全干净,所以这部分我按「shell 侧 argv/环境 × carrier 折叠」归到上层仓库:unixwin/niubash#1101(含用户可见症状与清理困难,供交叉定位)。

影响

  • 批量脚本里 mkdir -p "$OUT"($OUT 是盘符绝对路径)可能把整棵输出目录建到脚本 cwd 下面,同时目标路径不存在,后续写文件全部落在意外位置;因为 rc=0,脚本不会察觉。
  • 反斜杠写法在 -p 下完全不可用(父级创建失效),会逼用户改成正斜杠,从而踩 D1。

Activity

  1. qwased commented on Nov 24, 2026

    @qwased
    Author

    补充:跨盘复核(C / D / E,以及不存在的盘符)

    针对"是不是只有 D 盘"这一点,用 PowerShell 原生 argv(无 shell 包装)复核了同盘场景:

    cwd 命令 目标 X:\_wt_probe_\n\x cwd 里多出的影子
    C:\_wt_cwd_ mkdir.exe -p 'C:/_wt_probe_/n/x' ❌ 未创建(rc=0) ✅ .\_wt_probe_\n\x
    E:\_wt_cwd_ mkdir.exe -p 'E:/_wt_probe_/n/x' ❌ 未创建(rc=0) ✅ 同上

    即 D1 与盘符无关:任何 X:/... 在 cwd 位于 X: 时都按盘符相对落盘并返回 0;D2(X:\... 的 -p 不创建中间父级)在 C/D/E 上表现一致。

    另补一条 POSIX 宿主侧的交叉观察(已开在 unixwin/niubash#1101):同一个 mkdir.exe 经 niu 调用时,/c/...、/d/...、/e/... 与 C:/...、D:/...、E:/... 六种拼写都会在调用方 cwd 留下 <对应盘符>+U+F03A 的空目录;连不存在的 Z:/x 也会在报 ENOENT(rc=1)之后留下 Z+U+F03A。杂项名的字母取自操作数里的盘符,与 cwd 在哪个盘无关。

    版本:WinuxCmd 1.2.2(v1.2.2)、Windows 11 10.0.26300。所有探测目录已清理。

  2. caomengxuan666 commented on Nov 25, 2026

    @caomengxuan666
    Member

    队长 triage:受理,本单立为该面的主单(niubash #1101 的 niu 传参形态挂链至此)。

    源码层位(队长已核,与你的定位吻合)。 mkdir.rs 的 mkdir_fold 调 canon::prepare_operand(折叠本体在 winux_core::path,canon.rs 的钉把折叠契约写明了:D:name → D<U+F03A>name、裸 D: 保持盘符、/cygdrive/e/Data → E:\Data);create_parents 随后分两路 walk——folded 形重新锚定 root 后走一条、未折叠形按 split_slashes 逐段走另一条。D1(D:/… 同盘被当盘符相对)与 D2(D:\… 不建父级)都落在这个「折叠产物 × walk 分支选择」的面上,你的疑似病根方向成立;具体哪一段锚定/拼接出错,要等实机矩阵 + 代码走读定案,本 triage 不先下结论。

    危害口径。 D1 最重:rc=0 静默、目标不建、树长在 cwd 下,脚本无从察觉——这类「成功但落错位置」按队长口径优先于响亮的失败(D2)。

    还差什么。 全部行为面在 Windows,队长实机验证机(X299)目前宕机;恢复后按你的 D1/D2 复现脚本先跑原生 argv 三形矩阵(/d/…、D:/…、D:\… × 同盘/异盘 cwd)确立,再派修。优先级建议 P2(D1 的静默错位单看接近 P1 性质,但触发面限于盘符绝对写法,先挂 P2、复核时再议)。

  3. caomengxuan666 commented on Nov 25, 2026

    @caomengxuan666
    Member

    CAPTAIN-VERIFY (host matrix, devin-windows / Windows Server 2022, published v1.2.3 binaries driven directly from PowerShell) — D1 and D2 reproduce exactly as filed; this issue stays the main entry for the family.

    • D1a same-drive D:/wt/x/y: rc=0, nothing created at the target, the tree lands under the cwd instead — the silent wrong-location drop is real. D1b cross-drive behaves correctly, so the trigger is precisely same-drive.
    • D2 backslash forms, same- and cross-drive, with and without -p: no parents created, rc=1.
    • The POSIX /d/… form is clean, as you recorded.
    • Git Bash (GNU coreutils 8.32) passes every one of these shapes — these are true divergences, not MSYS-convention behavior. The fix(fs_win): api_path restores the root separator on bare-drive landings #1871 fix does not touch this surface, as expected.

    Ruling: the fix lives in this repo — anchor drive-absolute operands at the drive root before the -p walk (prepare_operand / create_parents), with the C++ twin in native_path.cppm reviewed in the same pass. The six-shape matrix from the verification run is the acceptance set. A fix lane is dispatched on this basis. (The niu-side sibling #1101's remaining exposure reduces to this issue; #4188's lookup-side half is ruled separately in rubash.)

  4. added 4 commits that reference this issue on Nov 25, 2026
  5. caomengxuan666 commented on Nov 25, 2026

    @caomengxuan666
    Member

    Closed by #1875 (merged c88a1f03, captain-verified above): drive-absolute operands now take the anchored decomposition before the -p walk, so D1's silent same-drive drop and D2's missing parents are both gone — fail-first pins and the six-shape host matrix are green, with the caller's spelling preserved in -v output and diagnostics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions