Repository navigation
fix(executor): spawn foreign shebang interpreters on direct exec (Refs #3415) - #3443
Conversation
…#3415) Windows has no kernel #!, so a directly-executed script naming a non-shell interpreter (#!/usr/bin/env node, #!/bin/echo ...) was parsed as bash: a non-shell body died with a syntax error (rc 2) and a shebang-only file exited 0 in silence. Spawn the named interpreter with the script as its operand instead (the MSYS/Git Bash model), on the foreground, pipeline-stage and exec-builtin faces; sh/bash/dash/rubash shebangs and shebangless files keep the shell path. GNU anchor: execute_cmd.c:6126 shell_execve (exec first; ENOEXEC tail only for shebangless text, :6225-6260) + execute_shell_script (:6001-6043, re-exec through the NAMED interpreter, one optional argument word) + getinterp (:5969). Regression nails: tests/issue3415_nonshell_shebang_direct_exec.rs (9 tests, Windows only; unix lets the kernel own #!). UNVERIFIED-BY-CAPTAIN
Captain review — FAIL(返工:
|
`cargo fmt --check` reported 9 violations in the PR's new/changed code (exec.rs:887, path.rs:2413/:2467, pin :117/:132/:148/:172/:194/:203). Whitespace-only reflow, no semantic change; `cargo fmt --check` now rc=0.
…3415) Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
【WIN-AGENT-AUDIT】 机制核查:
腿(X299 = 本机 Windows 实跑,钉为 #![cfg(windows)] 专属):
钉更正(本轮实际执行后发现的 fixture 缺陷,test-only):
续作记录: 未跑/VOID 的腿:
|
Refs #3415。
现象
Windows 上直接执行带非 shell 解释器 shebang 的脚本时,引擎把文件当 bash 脚本解析:非 shell 体报语法错(rc=2,报错头是脚本名);shebang-only 文件静默 rc=0。Git Bash 对照:正常派生解释器执行。
根因
Windows 没有内核
#!,直执行分两条路都把文件吞进 shell:execute_same_shell_script(external_finish.rs):direct_windows_shell_script_path认出无扩展名文本文件后,exec_format_refusal只对不可解析的解释器拒绝——node能解析就放行,随后execute_direct_shell_script在进程内按 bash 解析。external_command_for_named_program_with_exec_name,path.rs):should_run_with_shell的文件一律经shell_wrapped_command用sh包裹;exec内置(builtins/exec.rs)直接调同一包裹函数。改动
src/executor/path.rs:新增foreign_shebang_interpreter(解析#!行:解释器词 + 至多一个可选参数词;/usr/bin/env X走目标词判定;shell 解释器与不可解析的解释器返回 None)与shebang_interpreter_command(拼出[解释器] [可选参数] [脚本原样拼写] [原参数...]的派生命令;WinuxCmd dispatcher 解析到的解释器按 funnel 旧例补 applet 名)。funnel 在shell_wrapped_command之前先试这条分支。src/executor/external_finish.rs:execute_same_shell_script在拒绝检查之后、直接执行之前,对可解析的非 shell shebang 拒绝(返回Ok(false)),让普通磁盘命令路径去派生解释器。src/builtins/exec.rs:exec内置在 shell 包裹之前先试解释器派生。should_run_with_shell恒为假,新分支不可达,内核继续拥有#!。GNU 锚点
execute_cmd.c:6126shell_execve:先 execve,只有无 shebang 的文本文件才落入 ENOEXEC 后的 shell 执行(:6225-6260)。execute_cmd.c:6001-6043execute_shell_script:无 hash-bang exec 的系统经具名解释器重执行,解释器为#!后第一个词、至多一个参数词、随后是脚本名。execute_cmd.c:5969getinterp:跳过#!后空白,取一个空白分隔词;execute_cmd.c:5957-5964MSDOS 分支把 CR 当行结束符(与既有拒绝分类器的split_whitespace解析一致)。回归钉
tests/issue3415_nonshell_shebang_direct_exec.rs(Windows only,9 个用例):直接解释器 + 可选参数 + 原参数(以 scratch 目录下的 batch 文件为解释器,逐字节断言 argv)、shebang-only 不再静默、/usr/bin/env hostname启动器臂、绝对路径解释器、cmd.exe /c单参数臂、sh/bash shebang 与无 shebang 锁定、exec内置面、管道段面。本 PR 删除了什么
无。纯加法:新增两个
pub(crate)助手与一处拒绝分支,未删除任何函数、测试或公开面。UNVERIFIED-BY-CAPTAIN
#!行多于一个参数词时只取第一个(与 GNUexecute_shell_script一致;Linux 内核则把余行当单个参数传给 env);env本体缺失但目标可解析时直接派生目标(env 在此只做 PATH 查找);解释器 argv[0] 取解析后的 Windows 路径而非 shebang 原拼写。