Skip to content

external_argument_path 把 /X/ 当盘符翻译成 X:\ ,吞掉正则 pattern / git pathspec 等非路径参数 #31

Closed

Description

@caomengxuan666

现象

external_argument_path(src/executor/path.rs:182)在 Windows 上对每一个外部命令参数调用 shell_path_to_windows,其中的"盘符规则"(path.rs:358-367)把 /X/ 形式(第 0 位 /、第 1 位字母、第 2 位 /)当盘符路径,翻译成 X:\。

这吞掉了一切以 /X/ 形式出现的非路径参数——正则 pattern、git pathspec、sed/awk 脚本片段。

复现

用任意原生 exe 转储 argv(这里是我的 gcc 编译的 argvdump):

$ argvdump /h /h/ /tmp/pat2.txt
argc=5
argv[0]=[D:/tmp/argvdump.exe]
argv[1]=[/h]        # len=2,不命中盘符规则 → 原样
argv[2]=[H:\]        # /h/ → H:\  ← 被翻译
argv[3]=[/h]
argv[4]=[C:/Users/ADMINI~1/AppData/Local/Temp\pat2.txt]

真实伤害(git 不是 WinuxCmd 命令,只经过 rubash 这一层):

$ git status /h/
fatal: H:\: 'H:\' is outside repository at 'D:/repo/unixwin-winuxsh'
# git 收到的是 H:\ 而不是 /h/

根因

path.rs:358-367 的盘符规则:

if normalized.len() >= 3
    && normalized.as_bytes()[0] == b'/'
    && normalized.as_bytes()[2] == b'/'
    && normalized.as_bytes()[1].is_ascii_alphabetic()
{
    // /X/ → X:\  —— 无条件触发,不区分路径和正则

/h/ 恰好满足(长度 3、[1] 是字母 h、[2] 是 /)。但正则 /h/、awk '/b/...'、git checkout /f/ 根本不是路径。

影响

  • 所有外部命令(git、python、node、rubash 无内置的任意程序)的 /X/ 形式参数被静默改写。
  • 与 winuxsh "参数一个都不会少" 的定位直接冲突——这跟 MSYS 的路径翻译是同一种病。
  • /tmp/... → %TEMP% 的映射是有意特性,没问题;问题是盘符规则对不存在/非路径的 /X/ 也强制触发。

建议

  • 盘符翻译只应在目标真的是路径时触发(如 is_file/is_dir 命中,或显式 /c/ 前缀)。
  • 或提供开关/转义(类似 MSYS MSYS2_NO_PATH_CONVERSION)。
  • 最低限度:/X/ 若解析后路径不存在,应保持原样传给子进程。

Activity

  1. arookieofc commented on Aug 13, 2026

    @arookieofc
    Member

    The /h/ forwarding regression is covered and passes in the current tree: cargo test --lib path (19/19), including windows_external_arguments_preserve_bare_drive_shaped_patterns. This confirms the exact /X/ argument is preserved; broader external path cases remain under issue tracking, so the issue remains open.

  2. arookieofc commented on Aug 13, 2026

    @arookieofc
    Member

    Fixed and committed as e0? (current commit: 74638a9): external argument conversion now preserves nonexistent /X/... arguments (regex/pathspec/sed fragments), while retaining explicit /c/... display-path conversion. Added Windows regression for /h/not-a-real-pathspec; path tests 20/20 and lib 163/163 pass.

  3. arookieofc commented on Aug 13, 2026

    @arookieofc
    Member

    已完成并验证:外部参数转换现在仅转换可确认存在的 shell display path,保留不存在的 /X/... regex/pathspec 参数。path 回归 20/20,lib 163/163,最小差分 25/26(唯一 glob case 是本机缺少测试目录导致的 stderr 环境噪音)。

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