Files
oh-my-pi/scripts/ci-release-build-binaries.ts
T
can1357 0cd852053e Merge branch 'farm/6a62fa2b/fix-bunfs-override-validation-fallback'
Serve bundled pi-* and TypeBox through an in-process virtual namespace
on Bun 1.3.14+ where `--compile` extras are unreachable via any
filesystem API (issue #3423).

Merge resolution:
- Reconciled `TYPEBOX_SHIM_PATH` with main's #3414 fall-through:
  `__resolveTypeBoxShimPath(isCompiled, sourcePath, exists)` returns the
  `omp-legacy-pi-bundled:` virtual specifier in compiled mode (no FS
  probe) and the on-disk source path otherwise, dropping to null when
  the shim file is missing so bare typebox imports fall through to native
  resolution.
- Removed the now-dead `--compile` extras path: dropped
  `LEGACY_COMPAT_BUILD_ENTRYPOINTS` usage from both build-binary.ts and
  ci-release-build-binaries.ts and deleted scripts/binary-entrypoints.ts;
  the bundler reaches every surface via legacy-pi-bundled-registry.ts.
- Deleted obsolete tests for the removed bunfs machinery
  (legacy-pi-compat-entrypoints, legacy-pi-typebox-shim-validation) and
  added regression coverage for __resolveTypeBoxShimPath.
- Dropped the binary-compiling smoke driver per maintainer request.

Verified: bun check clean; 77 extensibility tests pass; instrumented
compiled-binary run confirmed onLoad fires for all bundled specifiers.
2026-06-25 05:08:36 +02:00

211 lines
6.6 KiB
TypeScript

#!/usr/bin/env bun
import * as fs from "node:fs/promises";
import * as path from "node:path";
interface BinaryTarget {
id: string;
platform: string;
arch: string;
target: string;
outfile: string;
}
const repoRoot = path.join(import.meta.dir, "..");
const binariesDir = path.join(repoRoot, "packages", "coding-agent", "binaries");
const entrypoint = "./packages/coding-agent/src/cli.ts";
// Worker threads spawn `new Worker(Bun.main, { argv })` — they re-enter the
// binary's own entry module — so no separate worker modules are compiled.
// Legacy pi-* extension compat surfaces are served through an in-process
// virtual namespace (`legacy-pi-compat.ts`), reached via the main module
// graph, so no extra `--compile` entrypoints are required (issue #3423).
const isDryRun = process.argv.includes("--dry-run");
const targets: BinaryTarget[] = [
{
id: "darwin-arm64",
platform: "darwin",
arch: "arm64",
target: "bun-darwin-arm64",
outfile: "packages/coding-agent/binaries/omp-darwin-arm64",
},
{
id: "darwin-x64",
platform: "darwin",
arch: "x64",
target: "bun-darwin-x64",
outfile: "packages/coding-agent/binaries/omp-darwin-x64",
},
{
id: "linux-x64",
platform: "linux",
arch: "x64",
target: "bun-linux-x64-baseline",
outfile: "packages/coding-agent/binaries/omp-linux-x64",
},
{
id: "linux-arm64",
platform: "linux",
arch: "arm64",
target: "bun-linux-arm64",
outfile: "packages/coding-agent/binaries/omp-linux-arm64",
},
{
id: "win32-x64",
platform: "win32",
arch: "x64",
target: "bun-windows-x64-modern",
outfile: "packages/coding-agent/binaries/omp-windows-x64.exe",
},
];
function parseRequestedTargets(): Set<string> | null {
const flagIndex = process.argv.findIndex(arg => arg === "--targets");
const flagValue =
flagIndex >= 0
? process.argv[flagIndex + 1]
: process.argv.find(arg => arg.startsWith("--targets="))?.split("=", 2)[1] ?? Bun.env.RELEASE_TARGETS;
if (!flagValue) {
return null;
}
return new Set(
flagValue
.split(",")
.map(value => value.trim())
.filter(Boolean),
);
}
function shouldAdhocSignDarwinBinary(target: BinaryTarget): boolean {
return target.platform === "darwin" && process.platform === "darwin";
}
async function runCommand(command: string[], cwd: string, env: NodeJS.ProcessEnv = Bun.env): Promise<void> {
const proc = Bun.spawn(command, {
cwd,
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 embedNative(target: BinaryTarget): Promise<void> {
if (isDryRun) {
console.log(`DRY RUN bun --cwd=packages/natives run embed:native [${target.platform}/${target.arch}]`);
return;
}
await runCommand(["bun", "--cwd=packages/natives", "run", "embed:native"], repoRoot, {
...Bun.env,
TARGET_PLATFORM: target.platform,
TARGET_ARCH: target.arch,
});
}
async function buildBinary(target: BinaryTarget): Promise<void> {
console.log(`Building ${target.outfile}...`);
await embedNative(target);
if (isDryRun) {
console.log(`DRY RUN bun build --compile --no-compile-autoload-bunfig --no-compile-autoload-dotenv --no-compile-autoload-tsconfig --no-compile-autoload-package-json --minify-identifiers --keep-names --define process.env.PI_COMPILED="true" --root . --target=${target.target} ${entrypoint} --outfile ${target.outfile}`);
return;
}
const buildEnv = shouldAdhocSignDarwinBinary(target)
? { ...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",
"--minify-identifiers",
"--keep-names",
"--define",
'process.env.PI_COMPILED="true"',
"--root",
".",
"--target",
target.target,
entrypoint,
"--outfile",
target.outfile,
],
repoRoot,
buildEnv,
);
// Bun 1.3.12 emits a truncated Mach-O signature on darwin builds.
if (shouldAdhocSignDarwinBinary(target)) {
await runCommand(["codesign", "--force", "--sign", "-", path.join(repoRoot, target.outfile)], repoRoot);
}
}
async function generateBundle(): Promise<void> {
if (isDryRun) {
console.log("DRY RUN bun --cwd=packages/stats scripts/generate-client-bundle.ts --generate");
console.log("DRY RUN bun --cwd=packages/coding-agent scripts/generate-docs-index.ts --generate");
console.log("DRY RUN bun --cwd=packages/coding-agent scripts/embed-mupdf-wasm.ts --generate");
return;
}
await runCommand(["bun", "--cwd=packages/stats", "scripts/generate-client-bundle.ts", "--generate"], repoRoot);
await runCommand(["bun", "--cwd=packages/coding-agent", "scripts/generate-docs-index.ts", "--generate"], repoRoot);
await runCommand(["bun", "--cwd=packages/coding-agent", "scripts/embed-mupdf-wasm.ts", "--generate"], repoRoot);
}
async function resetArtifacts(): Promise<void> {
if (isDryRun) {
console.log("DRY RUN bun --cwd=packages/natives run embed:native --reset");
console.log("DRY RUN bun --cwd=packages/stats scripts/generate-client-bundle.ts --reset");
console.log("DRY RUN bun --cwd=packages/coding-agent scripts/generate-docs-index.ts --reset");
console.log("DRY RUN bun --cwd=packages/coding-agent scripts/embed-mupdf-wasm.ts --reset");
return;
}
await runCommand(["bun", "--cwd=packages/natives", "run", "embed:native", "--reset"], repoRoot);
await runCommand(["bun", "--cwd=packages/stats", "scripts/generate-client-bundle.ts", "--reset"], repoRoot);
await runCommand(["bun", "--cwd=packages/coding-agent", "scripts/generate-docs-index.ts", "--reset"], repoRoot);
await runCommand(["bun", "--cwd=packages/coding-agent", "scripts/embed-mupdf-wasm.ts", "--reset"], repoRoot);
}
async function main(): Promise<void> {
const requestedTargets = parseRequestedTargets();
const selectedTargets = requestedTargets
? targets.filter(target => requestedTargets.has(target.id))
: targets;
if (requestedTargets) {
const unknownTargets = [...requestedTargets].filter(
requestedTarget => !targets.some(target => target.id === requestedTarget),
);
if (unknownTargets.length > 0) {
throw new Error(`Unknown release target(s): ${unknownTargets.join(", ")}`);
}
}
if (selectedTargets.length === 0) {
throw new Error("No release targets selected.");
}
await fs.mkdir(binariesDir, { recursive: true });
// Generate inside the try so resetArtifacts() always restores the empty
// checked-in placeholders, even if a generate or build step throws.
try {
await generateBundle();
for (const target of selectedTargets) {
await buildBinary(target);
}
} finally {
await resetArtifacts();
}
}
await main();