Files
open-design/tools/pack/tests/mac-prebundle.test.ts
T
lefarcenandGitHub 4c73aba565 fix(pack): align prebundle better-sqlite3 pin to 12.10.0 so mac daemon starts (#5680)
The packaged macOS daemon fails to start for a large share of users: it
exits code=1 at boot with ERR_MODULE_NOT_FOUND, `missing_module` =
better-sqlite3 (~270 hits / 4 days, dominant across 0.13.0–0.15.0). With
the daemon dead the whole app is unusable and the shell keeps retrying —
the "app keeps crashing / restarting" reports.

Root cause: mac-prebundle.ts / win-prebundle.ts pin the daemon's runtime
externals at `better-sqlite3@12.9.0`, but apps/daemon/package.json and
the pnpm lockfile resolve `12.10.0`. The assembled app declares 12.9.0
as a dependency; electron-builder 26's pnpm node_modules collector keeps
a dependency only when the on-disk version semver-satisfies the pin.
12.10.0 does not satisfy exact `12.9.0`, so the collector DROPS
better-sqlite3 from Contents/Resources/app/node_modules with a non-fatal
warning, and the prebundled daemon's `import Database from 'better-sqlite3'`
can't resolve at runtime. blake3-wasm (pinned 2.1.5, matching disk)
survives, which is why it only occasionally shows the same error.

Fix: bump the pin to 12.10.0 in both prebundle manifests to match the
daemon / lockfile. Unit assertions updated (red on main's 12.9.0, green
here).
2026-07-15 08:34:33 +00:00

181 lines
6.3 KiB
TypeScript

import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
MAC_DAEMON_PREBUNDLE_ESM_REQUIRE_BANNER,
MAC_PREBUNDLE_ESBUILD_TARGET,
MAC_PREBUNDLE_POLICIES,
MAC_PREBUNDLE_RUNTIME_DEPENDENCIES,
MAC_PREBUNDLED_DAEMON_CLI_RELATIVE_PATH,
MAC_PREBUNDLED_DAEMON_SIDECAR_RELATIVE_PATH,
MAC_PREBUNDLED_WEB_SIDECAR_RELATIVE_PATH,
assertMacPrebundleMetafile,
findForbiddenMacPrebundleInputs,
renderMacPackagedMainEntry,
shouldInstallInternalPackageForMacPrebundle,
shouldUseMacStandalonePrebundle,
} from "../src/mac-prebundle.js";
describe("mac standalone prebundle policy", () => {
it("is enabled only for standalone web output", () => {
expect(shouldUseMacStandalonePrebundle("standalone")).toBe(true);
expect(shouldUseMacStandalonePrebundle("server")).toBe(false);
});
it("keeps server-mode package topology unchanged", () => {
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName: "@open-design/web",
webOutputMode: "server",
}),
).toBe(true);
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName: "@open-design/packaged",
webOutputMode: "server",
}),
).toBe(true);
});
it("excludes internal packages replaced by mac standalone prebundles", () => {
for (const packageName of [
"@open-design/daemon",
"@open-design/desktop",
"@open-design/packaged",
"@open-design/sidecar",
"@open-design/sidecar-proto",
"@open-design/web",
]) {
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName,
webOutputMode: "standalone",
}),
).toBe(false);
}
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName: "@open-design/contracts",
webOutputMode: "standalone",
}),
).toBe(true);
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName: "@open-design/platform",
webOutputMode: "standalone",
}),
).toBe(true);
});
it("documents the explicit code-level bundle boundaries", () => {
expect(MAC_PREBUNDLE_ESBUILD_TARGET).toBe("node24");
expect(MAC_PREBUNDLE_POLICIES.packagedMain.externals).toEqual(["electron"]);
expect(MAC_PREBUNDLE_POLICIES.daemonCli.externals).toEqual(["better-sqlite3", "blake3-wasm"]);
expect(MAC_PREBUNDLE_POLICIES.daemonSidecar.externals).toEqual(["better-sqlite3", "blake3-wasm"]);
expect(MAC_PREBUNDLE_POLICIES.webSidecar.externals).toEqual([]);
expect(MAC_DAEMON_PREBUNDLE_ESM_REQUIRE_BANNER).toContain("createRequire");
// Must match apps/daemon/package.json / the pnpm lockfile, or
// electron-builder's collector drops the module from the shipped app and
// the daemon dies at boot with ERR_MODULE_NOT_FOUND (issue #4638).
expect(MAC_PREBUNDLE_RUNTIME_DEPENDENCIES).toEqual({
"better-sqlite3": "12.10.0",
"blake3-wasm": "2.1.5",
});
expect(MAC_PREBUNDLED_DAEMON_CLI_RELATIVE_PATH).toBe("app/prebundled/daemon/daemon-cli.mjs");
expect(MAC_PREBUNDLED_DAEMON_SIDECAR_RELATIVE_PATH).toBe("app/prebundled/daemon/daemon-sidecar.mjs");
expect(MAC_PREBUNDLED_WEB_SIDECAR_RELATIVE_PATH).toBe("app/prebundled/web-sidecar.mjs");
});
});
describe("findForbiddenMacPrebundleInputs", () => {
it("matches forbidden dependency roots after path normalization", () => {
expect(
findForbiddenMacPrebundleInputs({
forbiddenInputs: MAC_PREBUNDLE_POLICIES.webSidecar.forbiddenInputs,
inputs: [
"src/index.ts",
"C:\\repo\\node_modules\\next\\dist\\server.js",
"/repo/node_modules/openai/index.mjs",
],
}),
).toEqual([
"C:/repo/node_modules/next/dist/server.js",
"/repo/node_modules/openai/index.mjs",
]);
});
});
describe("assertMacPrebundleMetafile", () => {
it("accepts a safe web sidecar metafile", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-mac-prebundle-"));
const metafilePath = join(root, "safe.json");
try {
await writeFile(
metafilePath,
JSON.stringify({ inputs: { "/repo/apps/web/sidecar/index.ts": {} } }),
"utf8",
);
await expect(
assertMacPrebundleMetafile({ metafilePath, policyName: "webSidecar" }),
).resolves.toBeUndefined();
} finally {
await rm(root, { force: true, recursive: true });
}
});
it("rejects a packaged main metafile that pulled in web runtime closure", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-mac-prebundle-"));
const metafilePath = join(root, "unsafe.json");
try {
await writeFile(
metafilePath,
JSON.stringify({ inputs: { "/repo/node_modules/@open-design/web/dist/sidecar/index.js": {} } }),
"utf8",
);
await expect(
assertMacPrebundleMetafile({ metafilePath, policyName: "packagedMain" }),
).rejects.toThrow(/packaged main prebundle included forbidden inputs/);
} finally {
await rm(root, { force: true, recursive: true });
}
});
it("rejects a daemon metafile that bundled wasm-backed runtime dependencies", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-mac-prebundle-"));
const metafilePath = join(root, "unsafe-daemon.json");
try {
await writeFile(
metafilePath,
JSON.stringify({ inputs: { "/repo/node_modules/blake3-wasm/dist/node/index.js": {} } }),
"utf8",
);
await expect(
assertMacPrebundleMetafile({ metafilePath, policyName: "daemonSidecar" }),
).rejects.toThrow(/daemon sidecar prebundle included forbidden inputs/);
} finally {
await rm(root, { force: true, recursive: true });
}
});
});
describe("renderMacPackagedMainEntry", () => {
it("renders the prebundled runtime entry shim", () => {
expect(renderMacPackagedMainEntry(true)).toContain("./prebundled/packaged-main.mjs");
expect(renderMacPackagedMainEntry(true)).not.toContain("@open-design/packaged");
});
it("renders the package entry shim for non-prebundled mode", () => {
expect(renderMacPackagedMainEntry(false)).toContain("@open-design/packaged");
expect(renderMacPackagedMainEntry(false)).not.toContain("./prebundled/packaged-main.mjs");
});
});