Skip to content

fix(commands): route bash scripts using shell syntax - #136

Closed
wuchulonly wants to merge 1 commit into
masterfrom
fix/bash-shell-syntax-routing
Closed

wuchulonly wants to merge 1 commit into
masterfrom
fix/bash-shell-syntax-routing

Conversation

@wuchulonly

@wuchulonly wuchulonly commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

当输入 curl URL -o file.json; wc -c file.json 时,原有分发逻辑按空白分词,将 file.json; 保留为一个参数,随后把整段脚本交给内置 curl。结果是后续命令被当作 curl 的参数,wc 没有执行。同一路径还会丢失引号内的空参数,并改写多行字符串和 heredoc 的内容。

本次改动复用已有的 mvdan.cc/sh/v3 依赖,先解析原始脚本的 Shell 语法,再决定执行方式:

  • 只有单个、参数均为字面量,且不包含变量赋值、展开、重定向或后台执行等语法的注册命令,才直接调用内置实现。
  • 需要 Shell 解释的脚本保留原文,通过现有 Shell 适配器执行,并由 ProcessContainment 钩子接入进程监督。
  • 未启用适配器时,原有的内置命令与 Shell 管道桥接复用同一套判断,并根据语法树中的操作符位置拆分。超出其支持范围的注册命令组合会明确提示启用 EnableShellCommands,避免把后续命令误当成参数。
  • Registry.Run 将已经解析好的参数数组作为普通数据处理,不再根据参数中的分号、管道符等字符猜测 Shell 语法。

已通过的验证:

  • go test -race -count=1 ./pkg/commands ./core/commandline ./pkg/exts/terminal ./core/extension ./core/registry
  • go vet ./pkg/commands ./core/commandline ./pkg/exts/terminal
  • go mod tidy -diff
  • git diff --check

新增回归测试覆盖连写操作符、条件短路、换行、重定向、展开、引号与转义、空参数、heredoc、监督器接管和取消后的清理。使用本地模拟的 curl 验证“下载文件后统计字节数”的原始场景,无需访问网络。现有两个方向的内置命令与 Shell 管道测试也保持通过。

@M09Ic

M09Ic commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Superseded by #146, which ports this parser onto the current terminal tool and is merged. Closing so the two fixes are not applied twice.

@M09Ic M09Ic closed this Sep 22, 2026
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.

2 participants