Files
oh-my-pi/packages/coding-agent/scripts/build-binary.ts
T
roboomp 802e3f3e63 fix(extensibility): resolved compat shims to bunfs entry paths in compiled binaries
Reviewer caught that the new `legacy-pi-ai-shim.ts` is only referenced
via a computed string path, so Bun's `--compile` static analyzer cannot
trace it into bunfs. The same gap existed for the pre-existing
`typebox.ts` shim — `path.resolve(import.meta.dir, "../typebox.ts")`
collapses to `/$bunfs/typebox.ts` in compiled mode (where
`import.meta.dir` is `/$bunfs/root`), which does not exist in bunfs.
Legacy plugins importing bare `@sinclair/typebox` or `@(scope)/pi-ai`
therefore hit "Cannot find module" in release builds.

- Branch `TYPEBOX_SHIM_PATH` and `LEGACY_PI_AI_SHIM_PATH` on
  `isCompiledBinary()`: in compiled mode they point at the
  `--root`-relative bunfs entry path with a `.js` extension
  (`/$bunfs/root/packages/coding-agent/src/extensibility/<file>.js`);
  in dev they keep the `path.resolve(import.meta.dir, "../<file>.ts")`
  source path so tests still resolve against the workspace tree.
- List both shims as additional `--compile` entrypoints in
  `scripts/build-binary.ts` so Bun actually emits them into bunfs at the
  paths the runtime expects.

Verified with a focused `bun build --compile` probe: `Bun.resolveSync`
returns the expected bunfs path for both shims when they are listed as
entries, and fails when they are not. Dev-mode test suite unchanged
(17 pass / 0 fail across the four legacy-pi compat test files).
2026-05-27 12:34:27 +00:00

89 lines
3.1 KiB
TypeScript

#!/usr/bin/env bun
import * as path from "node:path";
const packageDir = path.join(import.meta.dir, "..");
const outputPath = path.join(packageDir, "dist", "omp");
function shouldAdhocSignDarwinBinary(): boolean {
return process.platform === "darwin";
}
async function runCommand(command: string[], env: NodeJS.ProcessEnv = Bun.env): Promise<void> {
const proc = Bun.spawn(command, {
cwd: packageDir,
env,
stdout: "inherit",
stderr: "inherit",
});
const exitCode = await proc.exited;
if (exitCode !== 0) {
throw new Error(`Command failed with exit code ${exitCode}: ${command.join(" ")}`);
}
}
async function main(): Promise<void> {
await runCommand(["bun", "--cwd=../stats", "scripts/generate-client-bundle.ts", "--generate"]);
try {
await runCommand(["bun", "--cwd=../natives", "run", "embed:native"]);
try {
const buildEnv = shouldAdhocSignDarwinBinary() ? { ...Bun.env, BUN_NO_CODESIGN_MACHO_BINARY: "1" } : Bun.env;
await runCommand(
[
"bun",
"build",
"--compile",
"--no-compile-autoload-bunfig",
"--no-compile-autoload-dotenv",
"--no-compile-autoload-tsconfig",
"--no-compile-autoload-package-json",
"--keep-names",
"--define",
'process.env.PI_COMPILED="true"',
"--external",
"mupdf",
"--root",
"../..",
"./src/cli.ts",
// Worker entrypoints. Bun's `--compile` discovers the literal in
// `new Worker("…", …)` at each spawn site, but only actually
// emits the worker into the bunfs root when it is listed here as
// an explicit additional entry. Paths are relative to this
// script's cwd (packages/coding-agent) and the `--root` above
// (../..) makes them appear inside the binary at
// `/$bunfs/root/packages/<pkg>/src/<worker>.js`, which is
// exactly what the literals at the spawn sites resolve to.
"../stats/src/sync-worker.ts",
"./src/tools/browser/tab-worker-entry.ts",
"./src/eval/js/worker-entry.ts",
// Legacy pi-* extension compat shims served by `legacy-pi-compat.ts`.
// Both are reached only via the computed `TYPEBOX_SHIM_PATH` /
// `LEGACY_PI_AI_SHIM_PATH` constants (which `--compile`'s static
// analyzer cannot trace), so each shim must be listed here to land
// in bunfs alongside the workers above. The bunfs entry path is
// `--root`-relative with a `.js` extension, e.g.
// `/$bunfs/root/packages/coding-agent/src/extensibility/typebox.js`,
// which is what the `isCompiledBinary()` branch in
// `legacy-pi-compat.ts` resolves to at runtime.
"./src/extensibility/typebox.ts",
"./src/extensibility/legacy-pi-ai-shim.ts",
"--outfile",
"dist/omp",
],
buildEnv,
);
// Bun 1.3.12 emits a truncated Mach-O signature on darwin builds.
if (shouldAdhocSignDarwinBinary()) {
await runCommand(["codesign", "--force", "--sign", "-", outputPath]);
}
} finally {
await runCommand(["bun", "--cwd=../natives", "run", "embed:native", "--reset"]);
}
} finally {
await runCommand(["bun", "--cwd=../stats", "scripts/generate-client-bundle.ts", "--reset"]);
}
}
await main();