From c31fa98e613adc51aa7d16201260cd689b9c72ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Flor=C3=AAncio?= Date: Mon, 20 Apr 2026 22:34:15 +0100 Subject: [PATCH] fix(cli): detect standalone server.js at non-default path (#660) --- apps/cli/src/start.test.ts | 74 ++++++++++++++++++++++++++++++++++++++ apps/cli/src/start.ts | 66 ++++++++++++++++++++++++++++------ 2 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 apps/cli/src/start.test.ts diff --git a/apps/cli/src/start.test.ts b/apps/cli/src/start.test.ts new file mode 100644 index 0000000000..4535ce16ea --- /dev/null +++ b/apps/cli/src/start.test.ts @@ -0,0 +1,74 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; + +import { resolveStandaloneServerPath } from "./start"; + +function makeRepo(): string { + return fs.mkdtempSync(path.join(os.tmpdir(), "kandev-start-test-")); +} + +function writeFile(filePath: string, contents = "") { + fs.mkdirSync(path.dirname(filePath), { recursive: true }); + fs.writeFileSync(filePath, contents); +} + +describe("resolveStandaloneServerPath", () => { + let repoRoot: string; + + beforeEach(() => { + repoRoot = makeRepo(); + }); + + afterEach(() => { + fs.rmSync(repoRoot, { recursive: true, force: true }); + }); + + it("returns the expected path when .next/standalone/web/server.js exists", () => { + const expected = path.join(repoRoot, "apps", "web", ".next", "standalone", "web", "server.js"); + writeFile(expected); + + expect(resolveStandaloneServerPath(repoRoot)).toBe(expected); + }); + + it("finds server.js at a deeper path (Turbopack root mismatch)", () => { + const nested = path.join( + repoRoot, + "apps", + "web", + ".next", + "standalone", + "Users", + "alice", + "projects", + "kandev", + "apps", + "web", + "server.js", + ); + writeFile(nested); + + expect(resolveStandaloneServerPath(repoRoot)).toBe(nested); + }); + + it("returns null when .next/standalone/ does not exist", () => { + expect(resolveStandaloneServerPath(repoRoot)).toBeNull(); + }); + + it("returns null when standalone/ exists but has no web/server.js", () => { + const standaloneDir = path.join(repoRoot, "apps", "web", ".next", "standalone"); + fs.mkdirSync(standaloneDir, { recursive: true }); + // Stray server.js that is NOT inside a `web/` directory. + writeFile(path.join(standaloneDir, "node_modules", "next", "server.js")); + + expect(resolveStandaloneServerPath(repoRoot)).toBeNull(); + }); + + it("ignores server.js inside node_modules/web/", () => { + const standaloneDir = path.join(repoRoot, "apps", "web", ".next", "standalone"); + writeFile(path.join(standaloneDir, "node_modules", "web", "server.js")); + + expect(resolveStandaloneServerPath(repoRoot)).toBeNull(); + }); +}); diff --git a/apps/cli/src/start.ts b/apps/cli/src/start.ts index bc1d4e82c6..47b37af4b7 100644 --- a/apps/cli/src/start.ts +++ b/apps/cli/src/start.ts @@ -28,6 +28,49 @@ import { } from "./shared"; import { launchWebApp, openBrowser } from "./web"; +/** + * Locates the standalone Next.js `server.js` inside `apps/web/.next/standalone/`. + * + * Normally this sits at `.next/standalone/web/server.js`, but Turbopack may + * place it under a deeper path (e.g. `.next/standalone/Users/.../web/server.js`) + * when it detects the wrong project root (typically caused by a stray + * `package-lock.json` in a parent directory). We look for any `web/server.js` + * below the standalone directory so `kandev start` keeps working. + * + * @returns absolute path to `server.js`, or null if it cannot be found. + */ +export function resolveStandaloneServerPath(repoRoot: string): string | null { + const standaloneDir = path.join(repoRoot, "apps", "web", ".next", "standalone"); + const expected = path.join(standaloneDir, "web", "server.js"); + if (fs.existsSync(expected)) return expected; + if (!fs.existsSync(standaloneDir)) return null; + + return findWebServerJs(standaloneDir); +} + +// Walks `dir` manually instead of relying on `Dirent.parentPath` (Node 20.12+), +// so the CLI keeps working on older Node 20.x runtimes installed via npx. +function findWebServerJs(dir: string): string | null { + const stack: string[] = [dir]; + while (stack.length > 0) { + const current = stack.pop() as string; + let entries: fs.Dirent[]; + try { + entries = fs.readdirSync(current, { withFileTypes: true }); + } catch { + continue; + } + for (const entry of entries) { + if (entry.isDirectory() && entry.name !== "node_modules") { + stack.push(path.join(current, entry.name)); + } else if (entry.isFile() && entry.name === "server.js" && path.basename(current) === "web") { + return path.join(current, entry.name); + } + } + } + return null; +} + export type StartOptions = { /** Path to the repository root directory */ repoRoot: string; @@ -69,17 +112,18 @@ export async function runStart({ } // Check for standalone build (Next.js standalone output) - const webServerPath = path.join( - repoRoot, - "apps", - "web", - ".next", - "standalone", - "web", - "server.js", - ); - if (!fs.existsSync(webServerPath)) { - throw new Error("Web standalone build not found. Run `make build` first."); + const webServerPath = resolveStandaloneServerPath(repoRoot); + if (!webServerPath) { + const standaloneDir = path.join(repoRoot, "apps", "web", ".next", "standalone"); + if (!fs.existsSync(standaloneDir)) { + throw new Error("Web standalone build not found. Run `make build` first."); + } + throw new Error( + `Web standalone build is missing server.js under ${standaloneDir}. ` + + "This can happen when Next.js/Turbopack detects a different project root " + + "(e.g. a stray package-lock.json in a parent directory). " + + "Remove the stray lockfile and re-run `make build`.", + ); } const webStandaloneDir = path.dirname(webServerPath); const webStaticDir = path.join(repoRoot, "apps", "web", ".next", "static");