Skip to content

fix(executor): expand tilde-leading PATH elements at lookup time (Refs #3826) - #3832

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3826-path-tilde-expansion
Nov 17, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3826-path-tilde-expansion

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Refs #3826

根因

PATH 里的 ~/... 条目在查找时不展开:find_in_path_walk_unix
(src/executor/path.rs)与 Windows 走查(经 path_scan_scaffold)
把每个 PATH 条目原样与命令名拼接,~/bin 从不匹配磁盘路径,
于是经该条目的运行、command -v、hash、hash -t、type
全部落空;-a 枚举(command_paths)与 memo 化未命中探针
(memoized_miss_still_absent)同样原样拼接。

GNU 在查找时逐条目展开(findcmd.c:544 find_in_path_element):
xpath = (posixly_correct == 0 && *path == '~') ? bash_tilde_expand (path, 0) : path。
赋值时不展开(echo $PATH 两侧皆字面量),POSIX 模式下不展开
(posixly_correct == 0 门)。

修法

新增 expand_path_element_tilde(src/executor/path.rs,复用
赋值层 tilde_expand::expand_tilde_segment:~/~/... 走 $HOME,
~+/~-/~N 走 dirstack,~user[/rest] 走 getpwnam,不可解
用户留原样;set -o posix 时留原样),在 4 个拼接点应用:
unix 走查、path_scan_scaffold(Windows 走查)、command_paths
(-a 枚举)、memoized_miss_still_absent(unix 未命中探针)。
hash 走 find_user_command,随走查一并修复,无需另改。

钉

tests/issue3826_path_tilde_expansion.rs,11 格:运行 /
command -v / hash / hash -t / export / 变量赋值 /
type(缺陷格,base 7 红);PATH 赋值仍字面量 / 绝对条目 /
裸 ~ 条目(对照格,两侧皆绿);set -o posix(守卫格,
修法不得在 POSIX 模式下展开)。

GNU 锚点:findcmd.c:544、lib/tilde/tilde.c:361-410(~user)。
种子 R133-C(GNU 10/10,rubash 3/10)。

本 PR 删除了什么

无删除。仅新增 1 个测试文件 + 1 个 helper 与 4 处调用,
不删任何函数与测试。

验证(X299,腿已跑完)

  • base fail-first:stem 4/11(3 对照 + 1 守卫过,7 缺陷格红),rc=101
  • 修后 stem:11/11,rc=0
  • lib 双边:base 与修后完全一致——1368 passed;唯一红格两侧皆为
    已知基线 issue2084
  • RUSTFLAGS="-D warnings" cargo check --locked --lib --bins:Finished,无警告
  • rustfmt --check(三文件):rc=0
  • marker-lint / utf8-boundary hygiene(本地):均过
  • GNU oracle 对照(Linux,种子形状 + POSIX 模式):已探
  • CI 命名(队长复核时 adjudicate:goldens + census 预期为基线红)

注:第一轮 stem 因钉文件 Cow::into_owned 误用编译失败(E0599),
已由提交 8434cf0f 修钉(仅改钉,无产品改动);重跑后 fail-first
与 11/11 均已拿到。第一轮腿脚本的 lib/warnings 步 rc 取的是 tail
的值(恒 0),结论以逐格 test result 行与 "Finished" 行为准。

UNVERIFIED-BY-CAPTAIN

…3826)

GNU findcmd.c:544 expands a '~'-leading PATH element at lookup
time; rubash joins the element verbatim, so a '~/bin' entry never
matches. 11 cells: run / command -v / hash / hash -t / export /
variable-assignment / type (defect), PATH-assignment-stays-literal /
absolute-entry / bare-tilde (controls), set -o posix (guard).

UNVERIFIED-BY-CAPTAIN
…#3826)

GNU findcmd.c:544 expands a '~'-leading PATH element inside the
walk (xpath = (posixly_correct == 0 && *path == '~') ?
bash_tilde_expand (path, 0) : path); rubash joined the element
verbatim, so '~/bin' never matched. Add expand_path_element_tilde
(reusing tilde_expand::expand_tilde_segment: ~/... via $HOME,
~+/~-/~N via the dirstack, ~user via getpwnam, unresolvable user
verbatim; skipped in POSIX mode) and apply it at the four join
points: the unix walk, path_scan_scaffold (Windows walk),
command_paths (-a enumeration) and memoized_miss_still_absent
(the unix memoized-miss probe, which must agree with the walk).
hash follows via find_user_command, so no hash change is needed.

UNVERIFIED-BY-CAPTAIN
String::from_utf8_lossy returns Cow<str>; .replace() on it already
yields an owned String, so the trailing .into_owned() is both
redundant and invalid (String has no into_owned). The stem failed
to compile on X299 (E0599, both base and fixed legs) — this commit
repairs the pins only, no product change.

UNVERIFIED-BY-CAPTAIN
@caomengxuan666 caomengxuan666 changed the title test: add fail-first regression pins for PATH tilde expansion (Refs #3826) fix(executor): expand tilde-leading PATH elements at lookup time (Refs #3826) Nov 17, 2026
@caomengxuan666

Copy link
Copy Markdown
Member Author

PR #3832 第1轮 — 建议:PASS(附相邻残差,范围裁量见下)
head: 8434cf0 base(merge-base): 969230b 文件: 3 个 +259/-1
关联 issue: #3826(状态:open,我核过的时间:2026-11-17 11:26 +0800)

机制核查:

  • GNU 锚点属实:findcmd.c:544 find_in_path_element 原文 xpath = (posixly_correct == 0 && *path == '~') ? bash_tilde_expand (path, 0) : path;(vendored third_party/bash 核对一致)。bash_tilde_expand 入口 general.c:1247;~/~/ 走 $HOME(tilde.c:344-356,含 APPDATA/_WIN32 与 get_home_dir 回退)、~user[/rest] 走 getpwnam(tilde.c:361-410,不可解留原文 savestring)、~+/~-/~N 由 preexpansion hook 处理(general.c:1120-1138 bash_special_tilde_expansions)。
  • POSIX 门:GNU posixly_correct == 0;rubash 侧 shell_option_enabled(env_vars,"posix") 读 __RUBASH_SETOPT_posix,POSIXLY_CORRECT 导入与 set -o posix 均经 set_shell_option 汇入(init.rs:2026、set/options.rs:256)——与既有 posix 门同一机制。
  • 复用 helper expand_tilde_segment(tilde.rs:772):~/~/→tilde_home、~user→unix getpwnam_r / windows /etc/passwd、特殊前缀→dirstack、不可解→verbatim,与 tilde.c:329-410 对齐。
  • 覆盖点核对:unix 走查 find_in_path_walk_unix(path.rs:1310)、Windows 走查 scaffold(path.rs:909,find_in_path_via_listings 与 windows 版 memoized_miss_still_absent 共用)、-a 枚举 command_paths(lookup_paths.rs:325)、unix 未命中探针 memoized_miss_still_absent(path.rs:1161)。unix 走查把展开放在 fold/transport 判定之前,as_written 产出展开后拼接——与 GNU sh_makepath(xpath_expanded, name)(makepath.c:66-122)一致。
  • 缓存指纹:command_lookup_fingerprint 含 HOME/USERPROFILE/PATH/PWD(path.rs:395-435);OLDPWD/dirstack 变化必伴随 PWD 变化,scaffold 缓存失效覆盖充分。
  • diff 里无可疑/无关改动;无桥接/翻译层/半成品修法。
  • 相邻残差(同缺陷类、issue 未点名的两个 PATH 查找面,本 PR 未覆盖):
    1. compgen -c 命令枚举(complete.rs:3028 shell_path_entries 直拼,无展开)。GNU 对应面 command_word_completion_function 逐元素展开(bashline.c:2317-2321 if (*current_path == '~') bash_tilde_expand)。
    2. ./source 的 PATH 查找(source/invocation.rs:134 shell_path_entries,无展开)。GNU source.def:170 find_in_path → findcmd.c:701 → find_user_command_in_path → find_in_path_element:544,同一展开点。
      实测(WSL GNU bash 5.3.20(1)-release,脚本 target/issue-suites/results/y1/pr3832_gnu_probe.sh):compgen -c mycmd 列出成员、. myscript → SOURCED rc=0;rubash head(8434cf0ff,target/debug/rubash.exe,脚本 target/issue-suites/results/y1/pr3832_residual.sh)两者皆落空(compgen rc=1 无输出;. → myscript: No such file or directory rc=1)。是否属本 issue 范围由队长裁;若算同类残差可考虑同 helper 补两处调用点。

腿(逐项给数字与名字):

  • 钉自跑:issue3826_path_tilde_expansion 11 passed / 0 failed(Windows,head 8434cf0)
  • fail-first:base 969230b + 原样钉文件 → 4 passed / 7 failed,rc=101;红名 = {issue3826_run_via_tilde_path_entry, issue3826_command_v_via_tilde_path_entry, issue3826_hash_via_tilde_path_entry, issue3826_hash_t_prints_expanded_path, issue3826_export_path_with_tilde_entry, issue3826_path_via_variable_assignment, issue3826_type_via_tilde_path_entry},绿名 = {absolute_home_path_entry_works, bare_tilde_entry_not_found, path_assignment_stays_literal, posix_mode_skips_tilde_expansion}——与 PR 声称「7 缺陷格红 + 3 对照 + 1 守卫绿」完全一致
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state}(1368 passed)/ base 失败名集 同左(1368 passed)/ 差集 ∅
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --lib --bins → Finished,0 警告
  • 卫生:rustfmt --check(path.rs / lookup_paths.rs / 钉文件)rc=0;marker-lint OK(118 consts / 268 allowlisted);utf8-boundary clean
  • CI(head 8434cf0 run 43998737109):红任务 2 个——「GNU regression goldens and canaries」红格 = {issue_753::case_9_declare_indexed_operand_binds_nothing, upstream_suite_slice_snapshots},与 master 同 run(44002101833 @9e895f1f)逐格一致=基线;「Regression pin stem census」红 stem 38 个 vs master 同 run 37 个,差集仅 {issue3633_coproc_eof_regressions::dead_coproc_unbinds_after_pipe_read}(unix-only coproc EOF 时序格,断言 "COPROC:63 60" vs "COPROC:",0.01s 完成;与 PATH 查找零交集,且该 stem 在 master 最近两次 run 43998401645/44002068162 均过 → 判 flake,建议队长知悉)。新钉 issue3826_path_tilde_expansion 在 census 11/11 全过。其余任务全绿。
  • GNU 对照:WSL /usr/local/bin/bash = GNU bash 5.3.20(1)-release (x86_64-pc-linux-gnu);脚本 target/issue-suites/results/y1/pr3832_gnu_probe.sh 逐形状:run→TILDE/rc=0、command -v→展开路径/rc=0、hash+hash -t→rc=0+展开路径、type→mycmd is <展开路径>、PATH 赋值仍字面量、裸 ~ 元素→rc=127(该机 /usr/bin/mycmd 存在但内部失败,rc 同)、posix→rc=127、type -a→列展开路径——与本 PR 行为一致(Windows 侧 command -v/hash -t/type 实测输出为 /c/... 拼法,为平台显示差异)。

F 点:无

续作记录:无(只审未改)

未跑/VOID 的腿:无

@caomengxuan666

Copy link
Copy Markdown
Member Author

批次汇总(y1 波:#3832 / #3831 / #3830 / #3829)— 四个 PR 均已按全协议审毕,建议判词如下(均为建议,终审归队长):

阻塞项:无。四个 PR 的 CI 红任务均为已知基线(goldens {issue_753::case_9, upstream_suite_slice_snapshots} + census 基线红族),新钉 stem 在各自 census 中全过;未合入、未 approve、未改判,终审与合入顺序归队长。

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.

1 participant