From 5e60973ebfa641e98a3be41a9927be73ec809199 Mon Sep 17 00:00:00 2001 From: Florian Schmidt Date: Mon, 16 Nov 2026 10:56:19 +0100 Subject: [PATCH] fix(server): start the production bundle again (#7) The bundled server crashed at startup with "Named export 'utils' not found": ssh2 is CommonJS and Node's ESM loader does not expose `utils` as a named export. Vitest resolves the module differently, so the unit tests passed. Import ssh2 through its default export instead. New test builds the bundle with the package's own esbuild command and starts it with Node until /api/health answers. --- .gitignore | 1 + apps/server/src/hosts/credentials.ts | 4 +- apps/server/src/hosts/index.ts | 4 +- apps/server/test/bundle.test.ts | 57 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 apps/server/test/bundle.test.ts diff --git a/.gitignore b/.gitignore index 47fcec4..721d496 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ coverage/ .env.* # runtime data when running from the repo data/ +apps/server/.bundle-test-*/ diff --git a/apps/server/src/hosts/credentials.ts b/apps/server/src/hosts/credentials.ts index 08f3fb4..98479aa 100644 --- a/apps/server/src/hosts/credentials.ts +++ b/apps/server/src/hosts/credentials.ts @@ -14,7 +14,9 @@ import { realpath, } from "node:fs/promises"; import path from "node:path"; -import { utils } from "ssh2"; +// ssh2 is CommonJS: Node's ESM loader cannot see `utils` as a named export (see test/bundle.test.ts). +import ssh2 from "ssh2"; +const { utils } = ssh2; import type { HostCredentialInput, HostCredentialMetadata, diff --git a/apps/server/src/hosts/index.ts b/apps/server/src/hosts/index.ts index 352284e..448015d 100644 --- a/apps/server/src/hosts/index.ts +++ b/apps/server/src/hosts/index.ts @@ -2,7 +2,9 @@ import { readFile, writeFile, rename, rm } from "node:fs/promises"; import { randomUUID } from "node:crypto"; import path from "node:path"; import { parseDocument, isSeq, isMap } from "yaml"; -import { utils } from "ssh2"; +// ssh2 is CommonJS: Node's ESM loader cannot see `utils` as a named export (see test/bundle.test.ts). +import ssh2 from "ssh2"; +const { utils } = ssh2; import { shellQuote, type HostActionRequest, diff --git a/apps/server/test/bundle.test.ts b/apps/server/test/bundle.test.ts new file mode 100644 index 0000000..88a26f4 --- /dev/null +++ b/apps/server/test/bundle.test.ts @@ -0,0 +1,57 @@ +import { execFile, spawn } from "node:child_process"; +import { mkdtemp, readFile, rm } from "node:fs/promises"; +import { createServer } from "node:net"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { promisify } from "node:util"; +import { expect, it } from "vitest"; + +// Vitest resolves CommonJS packages differently than Node's ESM loader, so only the +// real production bundle shows whether externals like ssh2 import correctly. +const serverDir = path.resolve(__dirname, ".."); + +async function freePort() { + const probe = createServer(); + await new Promise(resolve => probe.listen(0, "127.0.0.1", resolve)); + const { port } = probe.address() as { port: number }; + await new Promise(resolve => probe.close(resolve)); + return port; +} + +it("starts the production bundle with Node and answers the health check", async () => { + const outdir = await mkdtemp(path.join(serverDir, ".bundle-test-")); + const work = await mkdtemp(path.join(tmpdir(), "shellbook-bundle-")); + try { + const { scripts } = JSON.parse(await readFile(path.join(serverDir, "package.json"), "utf8")) as { scripts: { build: string } }; + const args = scripts.build.split(" ").slice(1).map(arg => arg.startsWith("--outdir=") ? `--outdir=${outdir}` : arg); + await promisify(execFile)(path.join(serverDir, "node_modules", ".bin", "esbuild"), args, { cwd: serverDir, shell: process.platform === "win32" }); + const port = await freePort(); + const child = spawn(process.execPath, [path.join(outdir, "main.js")], { + cwd: serverDir, + env: { ...process.env, SHELLBOOK_HOST: "127.0.0.1", SHELLBOOK_PORT: String(port), SHELLBOOK_WORKSPACE: path.join(work, "workspace"), SHELLBOOK_DATA_DIR: path.join(work, "data"), SHELLBOOK_SSH_KEY: path.join(work, "data", "id_ed25519") }, + stdio: ["ignore", "ignore", "pipe"], + }); + let stderr = ""; + child.stderr.on("data", chunk => { stderr += chunk; }); + const exited = new Promise(resolve => child.on("exit", resolve)); + try { + const healthy = await Promise.race([ + (async () => { + for (let attempt = 0; attempt < 100; attempt++) { + try { if ((await fetch(`http://127.0.0.1:${port}/api/health`)).ok) return true; } catch { /* not listening yet */ } + await new Promise(r => setTimeout(r, 100)); + } + return false; + })(), + exited.then(code => { throw new Error(`Server exited with ${code}: ${stderr}`); }), + ]); + expect(healthy).toBe(true); + } finally { + child.kill(); + await exited; + } + } finally { + await rm(outdir, { recursive: true, force: true }); + await rm(work, { recursive: true, force: true }); + } +}, 60_000);