diff --git a/apps/t3/src/cli.test.ts b/apps/t3/src/cli.test.ts index 513436d4c8..fdf1f8bfcd 100644 --- a/apps/t3/src/cli.test.ts +++ b/apps/t3/src/cli.test.ts @@ -3,7 +3,7 @@ import os from "node:os"; import path from "node:path"; import { describe, expect, it } from "vitest"; -import { parseCliOptions, readCliVersion } from "./cli"; +import { formatStartupError, parseCliOptions, readCliVersion } from "./cli"; describe("parseCliOptions", () => { it("reads defaults from environment variables", () => { @@ -87,3 +87,23 @@ describe("readCliVersion", () => { expect(value).toBe("0.1.0"); }); }); + +describe("formatStartupError", () => { + const options = parseCliOptions([], {}, "/workspace"); + + it("returns helpful guidance for port conflicts", () => { + const message = formatStartupError({ code: "EADDRINUSE" }, options); + expect(message).toContain("Port already in use"); + expect(message).toContain("--backend-port"); + }); + + it("returns error message when available", () => { + const message = formatStartupError(new Error("boom"), options); + expect(message).toBe("boom"); + }); + + it("falls back to generic startup error text", () => { + const message = formatStartupError({}, options); + expect(message).toBe("Failed to start t3 runtime."); + }); +}); diff --git a/apps/t3/src/cli.ts b/apps/t3/src/cli.ts index 6888f72228..4e4361b3e6 100644 --- a/apps/t3/src/cli.ts +++ b/apps/t3/src/cli.ts @@ -42,6 +42,11 @@ interface CliOptions { showVersion: boolean; } +interface StartupErrorShape { + code?: unknown; + message?: unknown; +} + function readArgValue(args: string[], index: number, key: string): string { const value = args[index + 1]; if (!value || value.startsWith("-")) { @@ -51,6 +56,21 @@ function readArgValue(args: string[], index: number, key: string): string { return value; } +export function formatStartupError(error: unknown, options: CliOptions): string { + const candidate = error as StartupErrorShape; + const code = typeof candidate?.code === "string" ? candidate.code : undefined; + + if (code === "EADDRINUSE") { + return `Port already in use. Try --backend-port ${options.backendPort + 1} --web-port ${options.webPort + 1} or stop the conflicting process.`; + } + + if (error instanceof Error && error.message.length > 0) { + return error.message; + } + + return "Failed to start t3 runtime."; +} + export function parseCliOptions( argv: string[], env: NodeJS.ProcessEnv, @@ -349,9 +369,7 @@ async function main() { try { await runCli(options); } catch (error) { - process.stderr.write( - `${error instanceof Error ? error.message : "Failed to start t3 runtime."}\n`, - ); + process.stderr.write(`${formatStartupError(error, options)}\n`); process.exit(1); } }