fix(build): include better-sqlite3 prebuilds in standalone bun bundle (#8847)

Refs: base-red #9737
This commit is contained in:
Diego Rodrigues de Sa e Souza
2026-08-08 13:51:57 -03:00
committed by GitHub
parent 3835f318d0
commit 6c95e2b354
3 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1 @@
- fix(build): include better-sqlite3 prebuilds in standalone bun bundle

View File

@@ -89,6 +89,15 @@ const NATIVE_ASSET_ENTRIES = [
src: ["node_modules", "better-sqlite3", "build"],
dest: ["node_modules", "better-sqlite3", "build"],
},
{
// #8847: Bun (and npx -g global installs) resolve better-sqlite3's native
// binary from prebuilds/ instead of build/Release/, so the compiled build/
// copy alone leaves a hollow package that falls back to sql.js (OOM under
// Bun). Ship the prebuilds alongside the compiled binary.
label: "better-sqlite3 prebuilds (Bun / global installs)",
src: ["node_modules", "better-sqlite3", "prebuilds"],
dest: ["node_modules", "better-sqlite3", "prebuilds"],
},
{
// TPROXY IP_TRANSPARENT addon (Fase 3 / Epic A). Built by build-tproxy-native
// before assembly; Linux-only + opt-in, so the source is absent on non-Linux

View File

@@ -0,0 +1,70 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { syncStandaloneNativeAssets } from "../../scripts/build/assembleStandalone.mjs";
/**
* Repro #8847: better-sqlite3 prebuilds are not included in the standalone
* bundle, so the bundled app fails when the platform's prebuild is needed
* (e.g. under Bun, which resolves the native binary via prebuilds/ rather
* than build/Release/).
*
* The test creates a synthetic node_modules/better-sqlite3/ tree with both
* the compiled build/Release/ binary AND the prebuilds/ directory, then
* confirms that syncStandaloneNativeAssets copies both into the standalone
* output. On the unfixed code this fails because NATIVE_ASSET_ENTRIES only
* lists better-sqlite3/build/.
*/
test("repro-8847: better-sqlite3 prebuilds are bundled alongside the compiled binary", async () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "repro-8847-"));
const projectRoot = path.join(tmp, "src-root");
// Seed better-sqlite3 with both build/Release/ and prebuilds/.
const bsqlDir = path.join(projectRoot, "node_modules", "better-sqlite3");
fs.mkdirSync(path.join(bsqlDir, "build", "Release"), { recursive: true });
fs.writeFileSync(
path.join(bsqlDir, "build", "Release", "better_sqlite3.node"),
"// native binary placeholder"
);
fs.mkdirSync(path.join(bsqlDir, "prebuilds"), { recursive: true });
for (const target of [
"darwin-arm64.node",
"darwin-x64.node",
"linux-arm64.node",
"linux-x64.node",
"linuxmusl-arm64.node",
"linuxmusl-x64.node",
"win32-arm64.node",
"win32-x64.node",
]) {
fs.writeFileSync(path.join(bsqlDir, "prebuilds", target), `// ${target}`);
}
const outDir = path.join(tmp, "standalone");
fs.mkdirSync(outDir, { recursive: true });
// Act: copy native assets into the standalone output.
await syncStandaloneNativeAssets(projectRoot, fs.promises, { log() {} }, outDir);
// Assert: the compiled build/Release/ binary was copied.
assert.ok(
fs.existsSync(
path.join(outDir, "node_modules", "better-sqlite3", "build", "Release", "better_sqlite3.node")
),
"compiled native binary (build/Release/) must be in the standalone bundle"
);
// Assert: the prebuilds/ directory was also copied.
const prebuildsDir = path.join(outDir, "node_modules", "better-sqlite3", "prebuilds");
assert.ok(fs.existsSync(prebuildsDir), "prebuilds/ directory must be in the standalone bundle");
// Assert: at least one prebuild file was copied.
assert.ok(
fs.existsSync(path.join(prebuildsDir, "linux-x64.node")),
"linux-x64 prebuild must be in the standalone bundle"
);
fs.rmSync(tmp, { recursive: true, force: true });
});