diff --git a/packages/cli/src/commands/preview.test.ts b/packages/cli/src/commands/preview.test.ts index 2f8d56b19a..862465622c 100644 --- a/packages/cli/src/commands/preview.test.ts +++ b/packages/cli/src/commands/preview.test.ts @@ -22,6 +22,16 @@ import { waitForStudioChildClose, } from "./preview.js"; +const lint = vi.hoisted(() => ({ inProcess: vi.fn(), worker: vi.fn() })); +vi.mock("../utils/lintProject.js", async (importOriginal) => ({ + ...(await importOriginal()), + lintProject: lint.inProcess, +})); +vi.mock("../utils/cancellableProcess.js", async (importOriginal) => ({ + ...(await importOriginal()), + runRenderSetupWorker: lint.worker, +})); + const tempDirs: string[] = []; afterEach(() => { @@ -398,6 +408,40 @@ describe("prebuildPreview", () => { }); }); +describe("startup lint", () => { + const neverSettles = () => new Promise(() => {}); + + it("never lints for a --json start", async () => { + lint.inProcess.mockImplementation(neverSettles); + lint.worker.mockImplementation(neverSettles); + const dir = tempProject(); + writeFileSync(join(dir, "index.html"), ""); + vi.spyOn(console, "log").mockImplementation(() => {}); + + await runCommand(previewCommand, { + rawArgs: [dir, "--background", "--json", "--user-data-dir", join(dir, "profile")], + }); + + expect(lint.inProcess).not.toHaveBeenCalled(); + expect(lint.worker).not.toHaveBeenCalled(); + }); + + it("does not hold a start behind a lint that has not finished", async () => { + lint.inProcess.mockImplementation(neverSettles); + lint.worker.mockImplementation(neverSettles); + const dir = tempProject(); + writeFileSync(join(dir, "index.html"), ""); + const error = vi.spyOn(clack.log, "error").mockImplementation(() => {}); + + await runCommand(previewCommand, { + rawArgs: [dir, "--background", "--user-data-dir", join(dir, "profile")], + }); + + expect(error).toHaveBeenCalledWith(expect.stringContaining("--user-data-dir")); + expect(lint.inProcess).not.toHaveBeenCalled(); + }, 5_000); +}); + describe("foreground preview JSON", () => { it("emits the same ready session contract before remaining attached", () => { const dir = tempProject(); diff --git a/packages/cli/src/commands/preview.ts b/packages/cli/src/commands/preview.ts index 2238b7408d..cff430b86e 100644 --- a/packages/cli/src/commands/preview.ts +++ b/packages/cli/src/commands/preview.ts @@ -48,7 +48,8 @@ import { parseRemoteDebuggingPort, validateRemoteDebuggingPortDeps, } from "../utils/openBrowser.js"; -import { lintProject } from "../utils/lintProject.js"; +import type { ProjectLintResult } from "../utils/lintProject.js"; +import { runRenderSetupWorker } from "../utils/cancellableProcess.js"; import { formatLintStartupMessage } from "../utils/lintFormat.js"; import { activeServerOnPort, @@ -416,19 +417,6 @@ export default defineCommand({ const dir = project.dir; const projectName = isImplicitCwd ? basename(process.env.PWD ?? dir) : project.name; - // Lint before starting — surface issues for the agent to fix. - const lintResult = await lintProject(dir); - if (!args.json && (lintResult.totalErrors > 0 || lintResult.totalWarnings > 0)) { - console.log(); - const verbose = Boolean(args["lint-verbose"]); - for (const line of formatLintStartupMessage( - lintResult, - verbose ? { kind: "verbose" } : { kind: "summary", pointer: "studio" }, - )) - console.log(line); - console.log(); - } - // Validation: --user-data-dir requires --browser-path if (args["user-data-dir"] && !args["browser-path"]) { reportPreviewFailure( @@ -492,6 +480,10 @@ export default defineCommand({ ); } + // Lint surfaces issues for the agent reading this terminal. It runs in the CLI worker while + // Studio starts, so it never delays the Studio opening; --json never reads it. + const startupLint = args.json ? null : printStartupLint(dir, Boolean(args["lint-verbose"])); + const launchMode = previewLaunchMode({ background: Boolean(args.background), foreground: Boolean(args.foreground), @@ -555,6 +547,7 @@ export default defineCommand({ remoteDebuggingPort, browserNoGpu, }); + await startupLint; return; } @@ -605,6 +598,26 @@ export default defineCommand({ }, }); +async function printStartupLint(dir: string, verbose: boolean): Promise { + try { + const lintResult = await runRenderSetupWorker( + "lint", + { projectDir: dir }, + { maxBufferBytes: 8 * 1024 * 1024 }, + ); + if (lintResult.totalErrors === 0 && lintResult.totalWarnings === 0) return; + console.log(); + for (const line of formatLintStartupMessage( + lintResult, + verbose ? { kind: "verbose" } : { kind: "summary", pointer: "studio" }, + )) + console.log(line); + console.log(); + } catch (error) { + clack.log.warn(`Lint did not finish: ${errorMessage(error)}`); + } +} + export type PreviewLaunchMode = "background" | "dev" | "local" | "embedded"; export function previewLaunchMode(options: {