Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(server): keep libsql native loader external to the CLI bundle (#178)
Leo06a7c7624c0b
diff --git a/scripts/lib/cli-external-packages.test.ts b/scripts/lib/cli-external-packages.test.tsindex 48b73c850..7a07cf60a 100644--- a/scripts/lib/cli-external-packages.test.ts+++ b/scripts/lib/cli-external-packages.test.ts@@ -10,8 +10,8 @@ import * as Schema from "effect/Schema";import serverPackageJson from "../../apps/server/package.json" with { type: "json" };import {- CLI_RUNTIME_EXTERNAL_PREFIXES,findInlinedExternalPackages,+ isRuntimeExternalCliDependency,selectCliPackagedRuntimeDependencies,selectCliRuntimeExternalDependencies,shouldBundleCliDependency,@@ -33,7 +33,18 @@ const decodeManifest = Schema.decodeUnknownSync(Schema.fromJsonString(PackageMandescribe("shouldBundleCliDependency", () => {it("bundles ordinary runtime dependencies", () => {- for (const id of ["effect", "@effect/platform", "hono", "@t3tools/shared/hostProcess"]) {+ for (const id of [+ "effect",+ "@effect/platform",+ "hono",+ "@t3tools/shared/hostProcess",+ "@libsql/client",+ "@libsql/core",+ "@libsql/hrana-client",+ "@libsql/isomorphic-ws",+ "libsqlite3",+ "libsqlx",+ ]) {assert.strictEqual(shouldBundleCliDependency(id), true, id);}});@@ -46,6 +57,13 @@ describe("shouldBundleCliDependency", () => {for (const id of ["playwright-core","node-pty",+ "libsql",+ "libsql/promise",+ "@neon-rs/load",+ "@libsql/darwin-arm64",+ "@libsql/linux-x64-gnu",+ "@libsql/linux-x64-musl",+ "@libsql/win32-x64-msvc","ffi-rs","@yuuang/ffi-rs-win32-x64-msvc","@ff-labs/fff-node",@@ -90,13 +108,13 @@ describe("selectCliRuntimeExternalDependencies", () => {it("selects every external root declared by the server", () => {assert.deepStrictEqual(Object.keys(selectCliRuntimeExternalDependencies(serverPackageJson.dependencies)).sort(),- ["@ff-labs/fff-node", "msgpackr-extract", "node-pty", "playwright-core"],+ ["@ff-labs/fff-node", "libsql", "msgpackr-extract", "node-pty", "playwright-core"],);});});describe("selectCliPackagedRuntimeDependencies", () => {- it("stages bundled packages that load platform-native bindings", () => {+ it("stages external native packages and lazy runtime packages", () => {assert.deepStrictEqual(selectCliPackagedRuntimeDependencies({effect: "4.0.0",@@ -178,8 +196,7 @@ it.layer(NodeServices.layer)("external package dependency closure", (it) => {// Runtime-external only. The build-only entries resolve `bun:*` and are never// loaded by Node, so their closure genuinely does not need to be external.- const isRuntimeExternal = (name: string) =>- CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => name.startsWith(prefix));+ const isRuntimeExternal = (name: string) => isRuntimeExternalCliDependency(name);it.effect("finds the runtime-external packages on disk", () =>Effect.gen(function* () {@@ -189,7 +206,13 @@ it.layer(NodeServices.layer)("external package dependency closure", (it) => {// Without this the closure check below can pass vacuously: if nothing is// read, nothing is checked. These are the packages whose closure actually// broke WSL, so require them by name.- for (const required of ["node-pty", "node-gyp-build-optional-packages", "detect-libc"]) {+ for (const required of [+ "node-pty",+ "node-gyp-build-optional-packages",+ "detect-libc",+ "libsql",+ "@neon-rs/load",+ ]) {assert.ok(found.includes(required),`expected ${required} in the pnpm store; the closure check is only meaningful if it can read these (found ${found.length})`,@@ -258,6 +281,14 @@ var x = 1;assert.strictEqual(result.regionCount, 2);});+ it("flags an inlined libsql loader before desktop packaging", () => {+ const result = findInlinedExternalPackages(+ region("../../node_modules/.pnpm/[email protected]/node_modules/libsql/index.js") ++ region("../../node_modules/@neon-rs/load/dist/index.js"),+ );+ assert.deepStrictEqual(result.inlined, ["@neon-rs/load", "libsql"]);+ });+it("flags scoped external packages", () => {const result = findInlinedExternalPackages(region("../../node_modules/@ff-labs/fff-node/dist/src/index.js"),diff --git a/scripts/lib/cli-external-packages.ts b/scripts/lib/cli-external-packages.tsindex c30df7367..d2b253682 100644--- a/scripts/lib/cli-external-packages.ts+++ b/scripts/lib/cli-external-packages.ts@@ -30,6 +30,14 @@ export const CLI_RUNTIME_EXTERNAL_PREFIXES = [// Inlining it into the ESM server bundle removes that CommonJS binding."playwright-core","node-pty",+ // Keep computed native imports and detect-libc resolution inside libsql.+ // The trailing slash keeps unrelated names such as libsqlite3 bundled;+ // the bare package name is matched exactly below.+ "libsql/",+ "@libsql/darwin-",+ "@libsql/linux-",+ "@libsql/win32-",+ "@neon-rs/load","ffi-rs","@yuuang/","@ff-labs/",@@ -65,11 +73,6 @@ export const CLI_BUILD_ONLY_EXTERNAL_PREFIXES = ["@effect/sql-sqlite-bun",] as const;-// These packages stay bundled, but load target-specific native bindings with a-// computed require. Ship their package roots so the staged install also brings-// the correct optional binding for the target OS and architecture.-export const CLI_BUNDLED_NATIVE_RUNTIME_PACKAGES = ["libsql"] as const;-// Mastra constructs this import at runtime to keep Node-only dependencies out// of Cloudflare Worker bundles. The CLI bundler cannot see it, so desktop// packaging must stage the package and its production dependency closure.@@ -80,8 +83,16 @@ export const CLI_EXTERNAL_PACKAGE_PREFIXES = [...CLI_BUILD_ONLY_EXTERNAL_PREFIXES,] as const;+function matchesExternalPrefix(id: string, prefixes: ReadonlyArray<string>): boolean {+ return prefixes.some((prefix) =>+ prefix.endsWith("/")+ ? id === prefix.slice(0, -1) || id.startsWith(prefix)+ : id.startsWith(prefix),+ );+}+export function isRuntimeExternalCliDependency(id: string): boolean {- return CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix));+ return matchesExternalPrefix(id, CLI_RUNTIME_EXTERNAL_PREFIXES);}/**@@ -95,7 +106,7 @@ export function isRuntimeExternalCliDependency(id: string): boolean {* inlined while node-pty (a declared dependency) stayed external.*/export function isExternalCliDependency(id: string): boolean {- return CLI_EXTERNAL_PACKAGE_PREFIXES.some((prefix) => id.startsWith(prefix));+ return matchesExternalPrefix(id, CLI_EXTERNAL_PACKAGE_PREFIXES);}/** True when the CLI bundle should inline `id` rather than leave it external. */@@ -121,7 +132,6 @@ export function selectCliPackagedRuntimeDependencies(Object.entries(dependencies).filter(([name]) =>isRuntimeExternalCliDependency(name) ||- CLI_BUNDLED_NATIVE_RUNTIME_PACKAGES.some((packageName) => name === packageName) ||CLI_LAZY_RUNTIME_PACKAGES.some((packageName) => name === packageName),),);