Skip to content

fix(builtins): literal-file existence gate on the /bin|/usr/bin RedirectToBuiltin faces (#2093) - #2147

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2093-literal-file-redirect
Oct 29, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2093-literal-file-redirect

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #2093.

Root cause (GNU C source is the specification)

The registry's SlashKind::RedirectToBuiltin claims six exact spellings (/bin, /usr/bin × echo/printf/pwd). Both claim faces answered BUILTIN semantics unconditionally — no check on the literal file the spelling names:

  • src/executor/external_inner.rs handle_external_shortcuts (echo arm — the external-shortcut face, not host-flag gated)
  • src/executor/external_file_builtins.rs handle_external_file_builtins (pwd/printf arms — the external-file-builtins face, host-flag gated)

GNU spec: findcmd.c search_for_command treats a slash-bearing word as an absolute program (findcmd.c:383-385, absolute_program => savestring(pathname)), and execute_cmd.c shell_execve execve's it verbatim — so on any host where the literal file exists, the file answers (--help/--version banner, no xpg_echo, physical-pwd default). Exactly the three divergence families measured in the issue against GNU bash 5.3.20.

Fix (the issue's adjudicated 候选修法 — 存在性门)

New path.rs gate literal_executable_file(): probes exactly the literal resolution find_user_command_uncached's slash branch performs first (anchor_at_session_cwd over shell_path_to_windows, then executable_candidate) — deliberately without that branch's Windows basename-PATH fallbacks, so a PATH-resolvable echo with no literal /bin/echo file keeps the redirect.

  • Gate open (literal file exists and is executable) → the face declines and the real exec path runs the same file it resolved.
  • Gate closed (no literal file — the Windows no-/bin default) → redirect stays; Windows 常态不变.
  • The documented gating asymmetry is preserved: printf/pwd ride the external_file_builtins host flag, echo does not; the gate alone decides on the echo face.

Contract text updated in command_registry.rs SlashKind::RedirectToBuiltin and docs/builtins.md §4.3.

Verification

  • Regression test: tests/issue2093_literal_file_redirect_regressions.rs — Windows pins drive the gate through __RUBASH_SHELL_ROOT (empty root → redirect stays byte-for-byte; root with literal .bat files → the files answer; host-flag matrix pins the asymmetry). Unix pins compare rubash's answer with a direct spawn of the same literal file (the file is its own oracle): echo --version, xpg_echo insensitivity, printf --version, pwd physical default through a symlink tail.
    • Windows: 3/3 pass. WSL Linux (GNU bash 5.3.20 host): 4/4 pass.
  • GNU oracle: WSL GNU bash 5.3.20 script-file probes reproduce the issue's three families (coreutils 9.4 banners rc 0, literal a\tb under xpg_echo, physical pwd under symlinked $PWD).
  • A/B vs pristine d5c172ae: closed-root matrix byte-identical; open-root matrix flips exactly the six-spelling faces to file semantics. Linux suite slice (comsub2/varenv/alias/exportfunc/intl/trap drivers, the files touching the six spellings): pristine-vs-fixed identical modulo the binary's own diagnostic path.
  • Windows battery: lib 1063 pass; the only failing targets (issue0085/1207/1949/395/2046-sync-b/2046-s6/2065/2031/2029 + the issue1708 compile break) fail identically on pristine master — pre-existing, not from this change.

fmt disclosure

cargo fmt fails with os error 206 (filename or extension too long — crate-wide scan over the 862-file tests/ dir), same precedent as prior hooks; explicit per-file rustfmt run instead (rustfmt --check clean on all five touched files). Committed with --no-verify for that reason only.

…ectToBuiltin faces (#2093)

Root cause: the six claimed slash spellings (/bin, /usr/bin of
echo/printf/pwd) answered BUILTIN semantics unconditionally, with no
check on the literal file the spelling names. GNU spec: findcmd.c
search_for_command treats a slash-bearing word as an absolute program
(findcmd.c:383-385, absolute_program => savestring(pathname)) and
execute_cmd.c shell_execve execve's it verbatim — a real file at that
path answers with its own semantics (--help/--version banner, no
xpg_echo sensitivity, physical pwd). Rubash faces: external_inner.rs
handle_external_shortcuts (echo arm) and external_file_builtins.rs
handle_external_file_builtins (pwd/printf arms).

Fix (the issue's adjudicated 候选修法): path.rs literal_executable_file()
probes exactly the literal resolution find_user_command_uncached's
slash branch performs first (anchor_at_session_cwd over
shell_path_to_windows, then executable_candidate) — deliberately
WITHOUT that branch's Windows basename-PATH fallbacks, so a
PATH-resolvable echo with no literal /bin/echo file keeps the
redirect (Windows no-/bin default unchanged: byte-identical in the
A/B closed-root matrix). Both faces decline to the real exec path
when the gate opens; the documented gating asymmetry is preserved
(printf/pwd ride the external_file_builtins host flag, echo does
not — the gate alone decides on the echo face). Contract text
updated in command_registry.rs SlashKind::RedirectToBuiltin and
docs/builtins.md §4.3.

Tests: tests/issue2093_literal_file_redirect_regressions.rs —
Windows pins drive the gate through __RUBASH_SHELL_ROOT (empty root
= redirect stays byte-for-byte; root with literal .bat files = the
files answer; host-flag matrix pins the asymmetry), unix pins
compare rubash's answer with a direct spawn of the same literal
file (echo --version, xpg_echo, printf --version, pwd physical
default through a symlink tail). Results: 3/3 Windows, 4/4 WSL
GNU-bash-5.3.20 Linux. GNU oracle probes reproduce the issue's
three divergence families; A/B vs pristine d5c172a: closed-root
matrix identical, Linux suite slice (comsub2/varenv/alias/
exportfunc/intl/trap drivers) pristine-vs-fixed identical modulo
the binary's own diagnostic path. No regression in the Windows
battery (lib 1063 pass; every failing target, and the
issue1708 compile break, fails identically on pristine master).

fmt disclosure: hook `cargo fmt` fails with os error 206 (filename
or extension too long, crate-wide scan over the 862-file tests/
dir) — same precedent as prior hooks; explicit per-file rustfmt
run instead (rustfmt --check clean on all five touched files).
@caomengxuan666

Copy link
Copy Markdown
Member Author

主会话审阅记录(2026-10-30):本 PR 其余 CI 全绿,唯 Windows「GNU regression goldens and canaries」的 regression 套件 1 红——issue_1903::issue1903_foreign_winux_marker_is_honored(cat: f: Broken pipe,rc 1 vs 期望 0;套件其余 223 过)。

该钉不在本 PR 的六拼写面内(cat 非 echo/printf/pwd),但本 PR 动了 path.rs 的解析路径,且 fifo 的 Broken pipe 形状本身也有时序 flake 的前科,单次数据分不清「路径改动连带」还是「fifo 竞态」。主会话已触发 failed-jobs 重跑做二分:重跑转绿 = flake,按用户裁定(合同批破例进 1.5.0、审过就合)直接合;同钉再红 = 视为本 PR 连带,请作者侧定位(优先看 path.rs 解析变化对该钉 marker 路径的影响),主会话不自行改作者分支。

@caomengxuan666
caomengxuan666 merged commit 7089d2d into master Oct 29, 2026
17 of 18 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2093-literal-file-redirect branch October 30, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant