feat(阶段5): 让 bun build --compile 的产物真正自足

编译产物拿到没有 node_modules 的目录里跑,原来是一路崩的 —— 而且**在仓库
目录里跑时全都正常**,因为它顺着 cwd 找到了 node_modules,假装没事。
这类问题只会在服务器上第一次启动时暴露。逐个堵掉:

- `import.meta.dir` 在二进制里恒为 `/$bunfs/root`,往上三级就是文件系统根:
  `data/test_case` 悄悄变成 `/data/test_case`,`.env` 去读 `/.env`。
  新增 runtime.ts 显式分叉:编译后按 cwd,开发时按仓库根(后者不能改,
  compose 挂给判题沙箱的是仓库根的 data/test_case)。

- sql.js / tree-sitter 的 wasm、jieba 的 .node 和 4.8MB 词典,
  原来都靠运行时 require.resolve / Bun.resolveSync / __dirname 去 node_modules 里找。
  全部改成 `with { type: "file" }` 内嵌成资源。jieba 尤其绕:它的 index.js
  运行时探测平台再 require 子包,dict.js 用 __dirname 读 dict.txt,两条都
  依赖磁盘布局,所以单独包了 vendor/jieba.ts 直接 require 内嵌的 .node。

- SQL 判题要 spawn 一个能被 SIGKILL 的子进程,原来 spawn 的是 child.ts 的路径,
  编译后那个文件不存在。改成二进制自己按 argv 分发:新增 main.ts 作为唯一入口,
  serve / worker / sql-child 三个子命令,镜像里只需要一份运行时。

## 顺带修掉一个能拖垮生产的坑

「spawn 自己」意味着只要 argv 分发这一环出问题(子命令改名没同步、compose 里
command 写错、拿别的入口编了二进制),「起自己」就变成「把整个程序再跑一遍」,
而那一遍又会 spawn 一个自己 —— 指数增长。

这不是假想:开发时用一个没有分发器的临时入口编了个二进制,一跑就递归 fork
105MB 的进程,几秒触发 global OOM,内核把开发机的终端杀了
(`selftest invoked oom-killer ... Killed process ... Alacritty`)。
同样的错误发生在服务器上就是判题机连同数据库一起拖死。

加了递归闸:父进程 spawn 时打 OJ2_SQL_CHILD 标记,带标记的进程一律拒绝再 spawn,
最多一层就停在一条明确的 SYSTEM_ERROR 上。

## 验证

产物拷进只有它自己一个文件的目录,2G 内存上限下跑,7 项全过:
jieba 分词(自定义词「循环结构」「死循环」命中)、tree-sitter Python3/C 各两条
(含一条**预期失败**的规则做对照,否则「语言没加载成功」和「规则通过」返回值
一模一样,分不出来)、SQL 判题 AC、SQL 只读防护仍拦住 PRAGMA。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-07 23:13:57 -06:00
co-authored by Claude Opus 5
parent 0f94e7ecbd
commit ce21a2bb8f
11 changed files with 231 additions and 31 deletions
+10 -5
View File
@@ -1,4 +1,10 @@
import { Language, Parser, type Node } from "web-tree-sitter"
// 语法 wasm 内嵌成资源。原来是 `Bun.resolveSync(pkg + "/" + name, import.meta.dir)`
// 编译成单二进制后 import.meta.dir 是 /$bunfs/root,解析不到 node_modules。见 vendor/jieba.ts
import cWasmPath from "tree-sitter-c/tree-sitter-c.wasm" with { type: "file" }
import pythonWasmPath from "tree-sitter-python/tree-sitter-python.wasm" with { type: "file" }
// web-tree-sitter 自己的运行时 wasmParser.init() 要用
import treeSitterWasmPath from "web-tree-sitter/web-tree-sitter.wasm" with { type: "file" }
export interface AstRule {
engine?: string
@@ -66,16 +72,15 @@ const languages = new Map<string, Language>()
async function loadLanguage(language: string) {
if (!(language in mappings)) return null
if (!initPromise) initPromise = Parser.init()
// locateFile 指到内嵌的 tree-sitter.wasmemscripten 默认按脚本所在目录找,
// 单二进制里那个目录是 /$bunfs/root,它自己找不着
if (!initPromise) initPromise = Parser.init({ locateFile: () => treeSitterWasmPath })
await initPromise
const cached = languages.get(language)
if (cached) return cached
const packageName = language === "C" ? "tree-sitter-c" : "tree-sitter-python"
const wasmName = language === "C" ? "tree-sitter-c.wasm" : "tree-sitter-python.wasm"
const wasmPath = Bun.resolveSync(`${packageName}/${wasmName}`, import.meta.dir)
const loaded = await Language.load(wasmPath)
const loaded = await Language.load(language === "C" ? cWasmPath : pythonWasmPath)
languages.set(language, loaded)
return loaded
}