build(server): make the CLI bundle loadable as a Node single-executable #11316
Mergedjuliusmarmingemerged 6 commits into
sea/desktop-bundle fromrefs/pull/11316/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge changed the title
- apps/server/vite.config.ts lines 56 to 56OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -37,6 +43,14 @@3743 },3844 pack: {3945 entry: ["src/bin.ts"],40- outDir: "dist",41- sourcemap: true,46+ outDir: packExecutable ? "dist-exe" : "dist",47+ sourcemap: !packExecutable,4248 clean: true,49+ ...(packExecutable50+ ? {51+ exe: {52+ fileName: "t3",53+ outDir: "dist-exe",54+ // The code cache only loads on the platform that produced it, so55+ // executables are always built natively, never cross-compiled.56+ seaConfig: { useCodeCache: true },apps/server/scripts/cli.ts line 212Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -177,0 +192,21 @@192+ const fs = yield* FileSystem.FileSystem;193+ const repoRoot = yield* RepoRoot;194+ const serverDir = path.join(repoRoot, "apps/server");195+196+ yield* Effect.log("[cli] Building single-executable...");197+ const spawnCommand = yield* resolveSpawnCommand("vp", ["pack"]);198+ yield* runCommand(199+ ChildProcess.make(spawnCommand.command, spawnCommand.args, {200+ cwd: serverDir,201+ env: { ...process.env, T3CODE_PACK_EXE: "1" },202+ stdout: config.verbose ? "inherit" : "ignore",203+ stderr: "inherit",204+ shell: spawnCommand.shell,205+ }),206+ );207+208+ // The executable can only `import` built-ins. A file-backed import209+ // passes the bundler and `node dist/bin.mjs`, then throws inside the210+ // binary, so read the emitted module graph rather than trusting config.211+ const bundlePath = path.join(serverDir, "dist-exe/bin.mjs");212+ const specifiers = findEsmImportsOfExternalPackages(yield* fs.readFileString(bundlePath));
- scripts/lib/cli-external-packages.ts line 123OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -101,3 +104,20 @@101104 );102105 }103106107+/**108+ * Scan an emitted bundle chunk for ESM imports of packages that are not Node109+ * built-ins.110+ *111+ * Inside a Node single-executable, `import` statements and `import()` can only112+ * resolve built-in modules; any file-backed specifier throws at module113+ * evaluation (static) or at first use (dynamic). External packages therefore114+ * have to be reached through `createRequire`, which reads the real filesystem115+ * in every runtime. The bundler cannot enforce this, so the check reads what it116+ * produced.117+ *118+ * Bun-only entry points are exempt: they sit behind a runtime-conditional119+ * `import()` that Node never evaluates.120+ */121+export function findEsmImportsOfExternalPackages(source: string): ReadonlyArray<string> {122+ const specifiers = new Set<string>();123+ const staticImport = /^import\s[^;]*?\sfrom\s+["']([^"']+)["']/gm;
- scripts/lib/cli-external-packages.ts line 128OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -103,0 +108,21 @@108+ * Scan an emitted bundle chunk for ESM imports of packages that are not Node109+ * built-ins.110+ *111+ * Inside a Node single-executable, `import` statements and `import()` can only112+ * resolve built-in modules; any file-backed specifier throws at module113+ * evaluation (static) or at first use (dynamic). External packages therefore114+ * have to be reached through `createRequire`, which reads the real filesystem115+ * in every runtime. The bundler cannot enforce this, so the check reads what it116+ * produced.117+ *118+ * Bun-only entry points are exempt: they sit behind a runtime-conditional119+ * `import()` that Node never evaluates.120+ */121+export function findEsmImportsOfExternalPackages(source: string): ReadonlyArray<string> {122+ const specifiers = new Set<string>();123+ // `import x from`, `import "side-effect"`, and `export ... from` all load the124+ // module at evaluation time.125+ const staticImport = /^import\s[^;]*?\sfrom\s+["']([^"']+)["']/gm;126+ const sideEffectImport = /^import\s+["']([^"']+)["']/gm;127+ const reExport = /^export\s[^;]*?\sfrom\s+["']([^"']+)["']/gm;128+ const dynamicImport = /\bimport\(\s*["']([^"']+)["']\s*\)/g;
- scripts/lib/cli-external-packages.ts lines 135 to 137OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -103,0 +117,21 @@117+ *118+ * Bun-only entry points are exempt: they sit behind a runtime-conditional119+ * `import()` that Node never evaluates.120+ */121+export function findEsmImportsOfExternalPackages(source: string): ReadonlyArray<string> {122+ const specifiers = new Set<string>();123+ // `import x from`, `import "side-effect"`, and `export ... from` all load the124+ // module at evaluation time.125+ const staticImport = /^import\s[^;]*?\sfrom\s+["']([^"']+)["']/gm;126+ const sideEffectImport = /^import\s+["']([^"']+)["']/gm;127+ const reExport = /^export\s[^;]*?\sfrom\s+["']([^"']+)["']/gm;128+ const dynamicImport = /\bimport\(\s*["']([^"']+)["']\s*[,)]/g;129+ for (const pattern of [staticImport, sideEffectImport, reExport, dynamicImport]) {130+ for (const match of source.matchAll(pattern)) {131+ const specifier = match[1];132+ if (specifier === undefined) continue;133+ if (NodeModule.isBuiltin(specifier)) continue;134+ if (specifier.startsWith("./") || specifier.startsWith("../")) continue;135+ if (CLI_BUILD_ONLY_EXTERNAL_PREFIXES.some((prefix) => specifier.startsWith(prefix))) {136+ continue;137+ }
- @@ -103,0 +113,21 @@113+ * evaluation (static) or at first use (dynamic). External packages therefore114+ * have to be reached through `createRequire`, which reads the real filesystem115+ * in every runtime. The bundler cannot enforce this, so the check reads what it116+ * produced.117+ *118+ * Bun-only entry points are exempt: they sit behind a runtime-conditional119+ * `import()` that Node never evaluates.120+ */121+export function findEsmImportsOfExternalPackages(source: string): ReadonlyArray<string> {122+ const specifiers = new Set<string>();123+ // `import x from`, `import "side-effect"`, and `export ... from` all load the124+ // module at evaluation time. Dynamic `import()` is deferred, which is the125+ // only form under which the Bun-only entry points are acceptable: they sit126+ // behind a runtime check Node never passes. Statically imported they would127+ // fail at evaluation like any other file-backed package.128+ const evaluatedPatterns = [129+ /^import\s[^;]*?\sfrom\s+["']([^"']+)["']/gm,130+ /^import\s+["']([^"']+)["']/gm,131+ /^export\s[^;]*?\sfrom\s+["']([^"']+)["']/gm,132+ ];133+ const dynamicImport = /\bimport\(\s*["']([^"']+)["']\s*[,)]/g;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
juliusmarminge merged this06de59b
juliusmarminge closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline: unavailable · PR result:
8f2903f· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.