Skip to content

fix(pwd): PWD family unconditionally POSIX /<drive>/ form — retire the native-PWD style hatch (#537) - #556

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/pwd-persona-form-537
Oct 12, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/pwd-persona-form-537

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

[P0] fix #537: PWD family unconditionally POSIX /<drive>/ form — retire the native-PWD style hatch

Fixes #537. Blocks niubash 1.4.0.

1. cf322dd 考古(根因修正)

Issue 把根因记为"cf322dda 把 $PWD 改为恒盘符形态"——考证后需修正:

  • d7b6960(2026-08-20 "preserve native paths for Winuxsh embedding") 才是 hatch 起点:WINUXSH_SHELL_PATH_STYLE 存在时 shell_pwd_display_path/startup PWD 保留原生 D:/ 拼写(宿主产品想要原生显示)。
  • cf322dd(2026-09-27 "canonical PWD form") 的动机是 bashdb:bashdb 用 (cd dir; pwd) canonical 化、_Dbg_is_file 以行首 / 分类绝对路径,frame 文件名 D:/ 会掉进 _Dbg_dir join。该提交把 PWD/pwd/cd 收敛为单一 canonical 形态,形态由 shell_path_style_enabled 门控——默认 POSIX /<drive>/,宿主 export 变量时保原生。它没有"改恒盘符",反而把盘符形态收进了开关。
  • 真触发器(本 PR 实测):旗舰宿主把开关钉死 native——niubash-runtime/src/shell.rs:222-225 在整个 shell 生命周期 set_var("NIU_SHELL_PATH_STYLE","native") + 遗留桥 WINUXSH_SHELL_PATH_STYLE="native"。于是每个真实 niu/winuxsh 会话里 $PWD 恒为盘符形态(issue 的"恒"在旗舰资产上成立,机制是宿主常设变量,不是引擎无条件)。nvm.sh v0.40.8 nvm_find_up(corpus:606)的 ${path_%/*} 上溯只认 ""/"."/找到 .nvmrc,D: 永不缩短 → 死循环;probe 24 60s 零输出。
  • hatch 自相矛盾的三重证据:① Self-produced path values unify to POSIX form (approved A from #329): PWD vs (pwd) mismatch, TMPDIR default backslash form, full-path 0 #331(closed,[family] Env-inherited POSIX paths re-rendered to D:/ drive form break scripts that splice $HOME/$DIR into sed s:...:...: (nvm install.sh writes 'export NVM_DIR=""', webi bootstrap dies) #329 裁决 Option A)锁定测试 bash_variable_is_posix_form 断言 BASH 无冒号——在 niu 会话里今天就是红的;② cf322dd 自己的 bashdb 目标要求 POSIX canonical PWD,hatch 使 bashdb 在 niu 下永远达不到 byte-parity;③ GNU bash on MSYS 没有任何模式让 $PWD 报 D:/。

2. 设计权衡(判断标准:MSYS 真实行为最高参照)

Oracle 实测(D:/Git/usr/bin/bash.exe = 真 msys bash 5.2.37 x86_64-pc-msys):
$PWD/$(pwd)/cd 后恒 /d/...;盘符形态路径喂给同一 find_up 循环任何 bash 都死循环(D: 剥不动)——分歧只在引擎给出的 PWD 形态。

  • 候选 a(采纳):persona 一致——PWD 恒 POSIX。PWD 是脚本消费的 shell 语义(nvm 上溯、bashdb 行首 / 分类),不是装饰;要原生拼写应该在宿主 prompt 渲染层做。[family] Env-inherited POSIX paths re-rendered to D:/ drive form break scripts that splice $HOME/$DIR into sed s:...:...: (nvm install.sh writes 'export NVM_DIR=""', webi bootstrap dies) #329 已裁决 self-produced 值 POSIX-first,hatch 是最后一道反例。
  • 候选 b(否决):折中——显示保持、语义归一。rubash 没有独立的"prompt-PWD 通道"(PS1 \w 读 $PWD),做双通道等于再造一个不变量;且只要任何路径让 $PWD 可为盘符形态,nvm 挂起就在那个形态下活着。
  • 候选 c(否决):上游删宿主 set_var。那是 niubash 仓的事且兜不住任意环境注入(本会话 winuxcmd/niugit 环境就带 native 变量);引擎侧不读即免疫。宿主侧清理(niubash shell.rs 的 set_var 桥)留 follow-up,现在对引擎无害。

改动面:shell_pwd_display_path 无条件 POSIX(path.rs)、shell_path_style_enabled 删除(无残余读者)、startup PWD/自注入 TMPDIR 恒 POSIX(init.rs)、pwd 逻辑回显去原生分支(pwd.rs)、命令替换快路径 $(pwd -P)/$(cd X && pwd[-P]) 接入同一 canonical 渲染(此前裸 current_dir 在任何设置下都漏 D:/,这是本 PR 测试在验证中抓出的额外漏点)、docs 合约行退役标注、probe 24 升级为回归测试(style 变量 set/unset 双格,60s 守卫)。

3. 矩阵对比(Windows 10 19045 x64,60s 守卫,stdin=/dev/null;探针=benchmarks/24-nvm-load.sh)

构建 平台 style 变量 cwd=仓库根 cwd=干净 tmp
41de831(修复前主树 exe) Windows 原生 SET(宿主形态) rc=124 @60084ms 零输出 rc=127 @88ms(tmp 无 corpus,快败无挂)
41de831 Windows 原生 UNSET rc=0 @369ms —
e4eeda0(本 PR) Windows 原生 SET rc=0 @2637ms rc=127 @134ms(同上快败)
e4eeda0 Windows 原生 UNSET rc=0 @529ms rc=127 @79ms(同上)
e4eeda0(WSL2 自建同源) Linux 原生 SET rc=0 rc=127(快败)
e4eeda0(WSL2) Linux 原生 UNSET rc=0 rc=127(快败)
  • 修正 issue 矩阵的一个测量口径:issue 的"干净 env"只 -u 三个污染变量,留着 WINUXSH_SHELL_PATH_STYLE=native——所以 8/8 格全挂;env -i 全净或仅 -u style 变量即过。挂起的唯一开关就是这个变量(与 perf(executor): hot-path residue — test marks gate, word materialization once, assignment scan admission (rubash#242) #501、三污染变量、cwd 均无关,与 issue 的排除结论一致,只是把"恒盘符"落到真机制上)。
  • PWD 形态抽查(fix,style=SET):PWD=/d/repo/rubash-wt-537、cd / 后 /d/;旧构建同格报 D:/repo/...;旧构建继承 PWD=/d/foo(不存在)时按 GNU 语义重导出但仍渲染盘符形态,新构建渲染 /d/...。
  • MSYS oracle:msys bash 5.2.37 $PWD/$(pwd) 恒 /d/...(复现命令:D:/Git/usr/bin/bash.exe -c 'echo $PWD')。

4. 回归与套件对比

5. 宿主侧 follow-up(不阻塞本 PR)

  • niubash crates/niubash-runtime/src/shell.rs:222-225 的两个 set_var 桥可删(AGENTS.md 本就要求 rubash 改读后移除);变量对引擎已惰性。
  • 宿主 prompt 要原生 D:/ 拼写:宿主自渲染(进程 CWD 宿主自知),或后续给引擎加显式 prompt 层接口——不回 PWD。

修复后 README 生态表 nvm.sh 行的 byte-clean 声明在空 NVM_DIR 规范形态(probe 24/ perf 套件形态)下重新为真,并已限定标注。

…e native-PWD style hatch (#537)

Root cause (precise): the engine has NOT rendered PWD in constant drive
form since cf322dd — that commit made PWD/pwd/cd one canonical form
gated by shell_path_style_enabled (POSIX /<drive>/ by default, native
D:/ when a host exports WINUXSH_SHELL_PATH_STYLE / __RUBASH_PATH_STYLE).
But the flagship hosts pin the gate native for the whole shell lifetime
(niubash-runtime/src/shell.rs:222-225 sets NIU_SHELL_PATH_STYLE=native
plus the deprecated bridge var), so every real niu/winuxsh session runs
with a drive-form $PWD — and nvm.sh v0.40.8's nvm_find_up
(corpus:606) terminates its ${path_%/*} walk only on "/./a found
.nvmrc: 'D:' never shortens, death loop (probe 24: 60s timeout, zero
output). GNU bash 5.3.0 force-fed the same PWD form hangs identically;
real MSYS bash 5.2.37 (D:/Git/usr/bin/bash.exe) NEVER produces that
form: $PWD/$(pwd)/post-cd are always /d/....

Design (per the #329 approved Option A policy and the msys persona
--version claims): PWD form is shell semantics, not decoration —
scripts consume it (nvm walk, bashdb _Dbg_is_file's leading-'/'
classification). A host that wants native spelling in its prompt must
render it host-side; the engine must not bend PWD. The hatch also
directly contradicted #331's own locked tests (bash_variable_is_posix_
form asserts BASH has no colon — it fails under niu today) and
undercut cf322dd's own bashdb goal.

Changes:
- shell_pwd_display_path: unconditional POSIX /<drive>/ rendering;
  shell_path_style_enabled deleted (no remaining readers).
- init.rs shell_pwd_display: startup PWD and injected TMPDIR default
  always POSIX (ee6bea9's stated intent, now unconditional).
- pwd builtin: logical echo drops the native branch; dead
  windows_slash_drive_display_to_native removed.
- command-substitution fast paths ($(pwd -P), $(cd X && pwd[-P]))
  routed through the same canonical renderer — the raw process cwd
  leaked D:/ form through them even on pre-cf322dda default settings.
- docs/host-contract.md: NIU_SHELL_PATH_STYLE retired engine-side
  (kept as a host prompt-render hint); README nvm row qualified.
- tests/issue537_pwd_posix_persona.rs: probe 24 upgraded to a
  regression (nvm auto-load completes < 60s from a drive-letter cwd,
  run twice: host-style var set and unset), plus the PWD-form family
  contract. Host-side cleanup of niubash's set_var bridge can follow
  separately (the vars are inert to the engine now).

Verification (Windows 10 19045 x64, 60s guards, stdin=/dev/null):
- fix build, style=SET (the pre-fix hang trigger), probe 24: rc=0 in
  2.6s (pre-fix 41de831: rc=124 at 60084ms); style=UNSET: rc=0 529ms
  (pre-fix also passes — the var, not the build date, is the trigger;
  the issue's 'clean env' cells carried the var).
- Linux native (WSL2, same source built in WSL): 4/4 cells rc=0.
- Full cargo test --no-fail-fast vs fresh master, same session env:
  zero new failures; 14 master-only failures fixed (issue331 family,
  cd/pushd/cdable-vars, comsub-cwd isolation, prompt \w, script-file
  drive path — all style-var dependent); the 11 branch-only names are
  load flakes that pass in isolation on both sides (examples family
  documented flaky since cf322dd). cargo fmt --check clean.

Fixes #537
@caomengxuan666 caomengxuan666 added bug Something isn't working platform:windows Divergence specific to the Windows build P0-critical 紧急:卡死/崩溃/静默数据丢失/功能完全不可用,无条件先修 labels Oct 12, 2026
@caomengxuan666
caomengxuan666 merged commit b8cb166 into master Oct 12, 2026
7 of 8 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 12, 2026
…forms, verbatim fidelity (#570)

* tests+docs: boundary contract audit - pin the three invariants (native core / POSIX forms / verbatim)

The core design (Windows native, no MSYS/cygwin, POSIX-form tool operands,
verbatim where promised) had zero contract tests - agents kept rediscovering
the boundaries by trial and error. This pins them against current master:

- tests/path_contract_boundary_native_core.rs (invariant A): PE import-table
  parser (no new deps) proves rubash.exe imports only Windows system DLLs
  and never an msys/cygwin runtime (the msys-2.0.dll strings in .rdata are
  the per-child detection probes, so the check reads imports, not bytes);
  the builtin surface runs with a System32-only PATH; native children
  receive Win32 cwd translations, never the POSIX persona. The installed
  winuxcmd toolchain (WINUXCMD_HOME-guarded) gets the same zero-POSIX-
  runtime check per tool.
- tests/path_contract_boundary_posix_forms.rs (invariant B): POSIX-in /
  POSIX-out for cd/pwd/$PWD/$(pwd)/redirect targets (the #537/#556
  contract, native-style host vars inert); native D:/ operands accepted;
  winuxcmd touch/ls/env/timeout/xargs + find -exec child lookup take
  explicit POSIX operands (find start-path gap is filed, not pinned);
  literal $-operands stay verbatim through the tool layer; PWD reaches
  POSIX-aware AND native children verbatim on the env channel.
- tests/path_contract_boundary_verbatim.rs (invariant C): the #511/#555
  family (cat/rm/cp/touch vs '$FIX/x' operands) never touches the
  expansion neighbor; git rev specs (origin/feat/x, HEAD~1..HEAD, HEAD --
  path, slash branches) cross verbatim; commit -m-shaped message bytes
  survive rubash's native-argv quoting byte-exactly; GIT_SSH_COMMAND-style
  env values pass through untouched.

15 cases, all green; Windows-boundary tests no-op on POSIX. Known red edges
are filed as issues, deliberately not pinned: rubash#566 (bare cd /c under
a configured shell root), WinuxCmd#1170 (find POSIX start paths),
niu-git#7 (git.exe launcher drops argv quotes), rubash#568 (/mnt/c
verbatim-vs-translate contract contradiction, master --lib red on Windows).

- docs/windows-path-contract.md: the four-layer boundary table (in-shell
  semantics / tool operands / sub-command lookup / native-child argv) -
  POSIX-form availability, verbatim guarantees, and who owns the conversion
  at each layer - plus how to drive niu correctly (POSIX inside the shell,
  native or per-tool POSIX outside, when D:/ forms are mandatory) and the
  agent-driving guide (why probes hit rubash.exe directly, why D:/ for git
  literals, why LF script files beat nested -c quoting) for future task
  briefs. AGENTS.md gets a pointer at the WinuxCmd section.

Full cargo test --no-fail-fast vs a pristine eb06295 worktree, same
session: failure sets identical (46 pre-existing host-env failures, zero
new, zero fixed). cargo fmt --check clean.

* fix(ci): gate the boundary contract suites to the windows target

The contract's verification target IS the Windows-native behavior, so the
three path_contract_boundary_* files are now #![cfg(windows)]-gated as a
whole: unix CI cfg-strips them to empty test binaries and proves only
compilation, not the cases (the cases need a Windows runner; the winuxcmd
toolchain leg additionally self-skips without WINUXCMD_HOME). Module docs
state this explicitly. The redundant per-test runtime guards and the
require_windows helper (cfg(windows)-gated but referenced unconditionally
- the actual linux/darwin/android build break) are gone with the gate.

Verified locally: cargo check --tests --target x86_64-unknown-linux-gnu and
--target aarch64-linux-android both finish clean; Windows-side the suites
are unchanged (5 + 6 + 4, all green); cargo fmt --check clean.
@caomengxuan666
caomengxuan666 deleted the fix/pwd-persona-form-537 branch October 13, 2026 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working P0-critical 紧急:卡死/崩溃/静默数据丢失/功能完全不可用,无条件先修 platform:windows Divergence specific to the Windows build

Projects

None yet

1 participant