Skip to content

fix(path): POSIX drive-form operand pre-translation covers every child class, context/position/existence invariant (closes #592) - #600

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/592-argv-translation-completeness
Oct 13, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/592-argv-translation-completeness

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

fix(path): POSIX 路径操作数预翻译对上下文/参数位/存在性无条件生效 (closes #592)

现象(issue 三实证,全部在本 PR 上复现→修复后转绿)

  1. for 循环/子壳/函数里 sed -n 1p /d/...、du -s /d/... ENOENT,顶层同命令 OK;find | xargs sed -i 链失败——报错伪装成 ENOENT。
  2. git -C /d/已存在 status 正常;git worktree add /d/待建路径 branch 不翻译——实际落到 D:\d\...(当前盘根下相对路径),agent 实踩。
  3. WinuxCmd#1178(du/sed 工具侧自解析)已合并,但契约是 rubash 侧预翻译覆盖所有外部命令所有 path 形操作数,不依赖每个工具自查。

根因(file:line)

  • 引擎:src/executor/path.rs external_command_for_named_program(修复前 L896-920)的 argv 漏斗里 preserve_native_args || shell_wrapped || posix_aware → arg.clone()——POSIX-aware 子类(winuxcmd 树内一切,含 sed/du/usr/bin/git.exe)整类 verbatim,盘符形态(/d/x)从不翻译。Option B"子进程自有 POSIX 层自解析"的前提对在售工具链三处不成立:
  • 宿主(漂移假象来源):niu(niubash crates/niubash-runtime/src/shell.rs::normalize_winuxcmd_slash_drive_args,挂点 L838/2101/4992/5004/5045)在顶层 AST 点按工具名单做翻译 → 顶层 OK;for/子壳/函数/script 内不经宿主 → 引擎 verbatim → ENOENT。这正是 issue 标题"随执行上下文漂移"的机制。引擎补齐后两个层面收敛到同一行为。
  • 工具链直打复现(排除 rubash):git.exe -C /d/已存在 status rc=0,git.exe worktree add /d/新路径 落 D:\d\新路径——存在性门在 git 侧,rubash verbatim 放大了它。

设计判据(规则表)

什么算 path 形操作数(候选,对所有非 shell 包裹子类):

形态 例 处理
/X/... 单字母盘符形 /d/repo/x 翻译 → D:\repo\x(#592 起 POSIX-aware 子类也翻;此前仅原生类)
/c/... 显示路径拼写 /c/Windows 恒翻译(任何方言,先于存在性)
/mnt/X[/...] /mnt/c/Users 恒翻译(#444 全类,不变)
虚拟系统根 /tmp、/usr/bin、/etc/... 恒翻译(#164 全类 + #177 拼写解析,不变)
/cygdrive/X/... /cygdrive/d/x verbatim(WinuxCmd normalize_api_operand_w 自折叠,#568 四路复测)
/dev、/dev/* /dev/null verbatim(rubash#120 子进程描述符表)
裸 / / verbatim(操作数字符,niubash#153)
开关形 /X...(非盘符) /nologo、/CN=test verbatim(native 开关;cmd /C 由 preserve_native_args 兜住)
x:/...、x:\... D:/repo 原样(已是 Win32 形态;C:// 宿主伪影由 repair 规则修复)
相对词 / 前导 \ rel/file、\n verbatim

参数位豁免/处理:

位置 例 处理
位置操作数 sed -n 1p /d/f 翻译
旗标消费的分离值(-C 类) git -C /d/repo、find /d/start -name x 翻译(对漏斗即普通词,按形状判定)
目标=待创建路径 git worktree add /d/new、tee /d/new 翻译,无存在性门(只有 __RUBASH_ARGV_DIALECT=legacy 恢复旧存在性门)
附着值 --opt=/x git --git-dir=/x、--flag=/usr/bin verbatim(开关 token,值语义;现有豁免,不新开口子。git 附着拼写自身不认 POSIX——工具账;分隔拼写 --git-dir /x 的路径词照常翻译且实测可用)
env 赋值值 env VAR=/posix ls verbatim(env 通道契约 §4.5;赋值在漏斗外已被 env 内建剥离,结构上到不了 argv 翻译)
grep/sed/awk 首位置模式/脚本/程序文本;find -name 族值 grep -F /d/x、sed /n/d、awk '{...}'、find -name /d* verbatim(数据操作数:形状无法分辨路径与模式,niubash#62;与宿主 winuxcmd_path_translation_mask 同一张表,不新开豁免口子。-e/--regexp/-f/--file 语义与宿主一致:-e 值是数据,-f 值是文件)
dirname/basename 全部位置操作数 dirname /a/b/c.txt → /a/b verbatim(不触 FS 的纯字符串变换:GNU dirname.c/basename.c 对操作数零 stat,操作数是回显文本——翻译对 FS 边界毫无收益、纯破坏 GNU 串语义,issue71 pin;与宿主名单的差异在此一处,引擎更守 GNU)
rev spec / 分支名 origin/feat/x、HEAD~1..HEAD、feat/slash.main 不呈盘符/虚拟根形态,天然不碰(invariant C② 契约测试锁)

执行上下文:顶层 / for 循环 / 子壳 / 花括号组 / 函数 / 管道 / 命令替换 / script 文件 / -c——全部经同一漏斗 external_command_for_named_program,上下文无关是结构性保证(漏斗不感知上下文),矩阵测试逐格锁定。

verbatim 红线对照(#568 裁决 / #444 断言 / 既有豁免表,零新增豁免):

修法

  • external_command_for_named_program:POSIX-aware 臂不再整类 verbatim——非豁免词走 external_argument_path(与原生类同一漏斗:盘符形态无条件翻译、/cygdrive//dev/裸 //开关/相对词规则共用);argv_data_primary_positions 按命令名+词位算出数据主位置(宿主同表移植:grep/sed/awk/find 语法数据 + 不触 FS 的 dirname/basename 字符串变换),命中即 verbatim。legacy 方言下 posix_aware 仍为 false → 落回原生漏斗的旧存在性门语义(回滚阀保留不动)。
  • 三个被 supersede 的 pin 反转(fix(path): translate /mnt/X drive argv for every child class (niubash#164) #444 先例):windows_virtual_system_root_arguments_keep_option_b_shapes_verbatim(/c/Windows 移出)、windows_mnt_drive_argument_translates_for_every_child(/d/repo/x 翻转)、virtual_system_root_operand_resolution_keeps_option_b_and_legacy_verbatim(同)、windows_native_argv_never_mixed_dialect(POSIX-aware 阶段改为双类同形断言)。
  • 新 pin:漏斗级 drive_form_arguments_translate_for_the_posix_aware_child / data_primary_operands_stay_verbatim_for_the_applet / legacy_dialect_reverts_drive_form_translation_for_the_applet;集成级 tests/issue592_argv_translation_completeness.rs(①上下文×参数位×存在性矩阵——真实 sed/tee 子进程,含 script 文件上下文;②verbatim 豁免对照——真实 grep -F/sed 地址/find -name/env 值;③legacy 绕过回归;④真实 git worktree add /d/新路径 端到端,worktree list 验位)。
  • 文档:docs/windows-path-contract.md(L2/L4 行 + §2 L2 [path][P2] POSIX absolute-path operand translation drifts by execution context: sed/du fail inside for-loops/subshells, fine at top level #592 条目 + §6 台账)、docs/platform-identity.md(per-child dialect 段落改为"引擎拥有全类 path 形翻译")。

验证

  • cargo check + RUSTFLAGS=-D warnings ✅;cargo fmt --check ✅;unix/macos 目标 cargo check ✅(新代码全在 #[cfg(windows)] 内;unix 下 cargo check --tests -D warnings 的 issue544/531/530 告警为预存,fresh master 同)。
  • cargo test --lib 660 全绿(含 3 个新漏斗 pin);tests/path_contract_boundary_*、issue177/331/566/588 全绿(1 个 B② 反例 pin 按 [path][P2] POSIX absolute-path operand translation drifts by execution context: sed/du fail inside for-loops/subshells, fine at top level #592 契约更新:$nosuchvar 字面分量不再展开的本质不变,回显形态随漏斗翻译)。
  • 全量 cargo test --no-fail-fast(RUST_TEST_THREADS=1,.cargo/config 强制):失败集与 fresh master(fcf7be16,独立基线 worktree + 子模块主仓复制/gitdir 指向)双向 comm 为空(43 个预存宿主环境失败:bashdb fixture/pwsh 缺失/prompt/heredoc 环境类;零新增、零消失)。
  • 真实端到端:for 循环/子壳/函数/花括号/管道/命令替换/script 文件 × sed -n 1p /d/... 全部 rc=0;git worktree add /d/tmp/wt-fixed wtbranch 落位 D:\tmp\wt-fixed(修复前 D:\d\tmp\wt-fixed);__RUBASH_ARGV_DIALECT=legacy 循环内旧行为复现(已存在翻译/缺失 verbatim)。
  • 已知边界(记录不修):xargs/find -exec 链中由工具自构造的子 argv(xargs sed -i /d/...)不经引擎漏斗,归 WinuxCmd 工具侧([bug] fc -e - <n> with empty history silently succeeds #1178/[bug] Negative index on unset array drops bad-subscript diagnostic #1170 车道);commit -m '/d/x' 类自由文本以形状契约为准(与原生类既有行为一致)。

回滚阀:__RUBASH_ARGV_DIALECT=legacy 原样保留。

closes #592

…d class, context/position/existence invariant (closes #592)

The argv funnel handed drive-form operands (/d/...) to POSIX-aware
children verbatim on the Option-B premise that each tool resolves them
itself. The shipped toolchain breaks that premise three ways: sed/du
1.1.5 have no operand resolution at all (WinuxCmd#1178), usr/bin/git.exe
existence-gates it (git -C /d/existing works while worktree add /d/new
silently lands at D:\d\new), and the niubash host's name-list
translation (normalize_winuxcmd_slash_drive_args) runs only at top-level
AST points - so the same command drifted by execution context (issue
#592: sed/du fail inside for-loops/subshells, fine at top level).

The engine now owns path-form pre-translation for EVERY non-shell-wrapped
child class: the POSIX-aware arm routes non-exempt words through
external_argument_path (the native funnel), making the translation
invariant across execution context (single funnel: -c, script files,
loops, subshells, functions, pipelines, command substitution), argument
position (positional operands, -C-style flag values, to-be-created
targets - no existence gate outside the legacy hatch) and target
existence.

Data operands stay verbatim (invariant C, niubash#62 - shape cannot
separate ls /d/ from grep -F /h/): argv_data_primary_positions ports the
niubash host's winuxcmd_path_translation_mask table (grep/sed/awk first
positional pattern/script/program text, find pattern-primary values) and
adds the filesystem-blind string transforms basename/dirname (GNU
dirname.c/basename.c never stat their operand; issue71 pins dirname
/a/b/c.txt -> /a/b). All pre-existing exemptions keep their exact
behavior: natives, shell-wrapped children, /cygdrive/d (folds in-child,
#568 probe), /dev/*, bare /, cmd switches, attached --opt=/x values, the
env channel, rev specs. __RUBASH_ARGV_DIALECT=legacy reverts the whole
widening to the pre-#592 existence-gated funnel.

Superseded pins flipped (#444 precedent): /c/Windows and /d/x verbatim
pins for the applet class now assert the translated form; the B2
negative ls-$nosuchvar pin keeps its no-re-expansion intent.
New pins: funnel-level drive-form/mask/legacy tests plus
tests/issue592_argv_translation_completeness.rs (context x position x
existence matrix with real sed/tee, verbatim exemption probes with real
grep -F/sed/find/env, legacy bypass regression, and a real git worktree
add /d/new end-to-end asserting the worktree list location).

cargo check -D warnings clean (windows + unix + macos targets), fmt
clean, full cargo test --no-fail-fast failure set identical to fresh
master fcf7be1 bidirectionally (41 pre-existing host-environment
failures, zero new, zero lost).
@caomengxuan666
caomengxuan666 merged commit 1b81fab into master Oct 13, 2026
6 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/592-argv-translation-completeness branch October 13, 2026 09:14
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
…601)

40 commits (PRs #496-#600) folded into [1.4.0] by theme instead of
per-commit listing:
- GNU semantics waves 1-4b with their issue families
- argv re-expansion P0 pair (#511/#555), PWD POSIX persona (#537)
- -s on-demand stdin driver as the canonical library reader (#586)
- CLI entry-shape semantics (#589-#591) and the GNU usage contract (#585)
- diagnostic message fidelity (#551) and parser diagnostic tokens
- Windows path contract (#568/#592)
- lexer/executor performance rounds (#554/#562/#501)
- docs: boundary invariants, platform gap, baselines, debugger M0

Commit message deliberately carries no release/version claim.
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.

[path][P2] POSIX absolute-path operand translation drifts by execution context: sed/du fail inside for-loops/subshells, fine at top level

1 participant