Skip to content

Commit 2e7a135

Browse files
committed
fix(cli): honor NODE_ENV=production again
1 parent b1e440c commit 2e7a135

4 files changed

Lines changed: 77 additions & 3 deletions

File tree

‎src/cli/main.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,19 @@ function parseArgs(args: string[]): CLIOptions {
136136
positionals.shift();
137137
}
138138

139+
// `--prod` wins; otherwise fall back to the documented `NODE_ENV` control
140+
// (see the ENVIRONMENT section of usage.ts). The fallback was dropped by the
141+
// CLI file-split refactor (#175), silently turning `NODE_ENV=production` into
142+
// a dead option and leaking stack traces from the dev error page. Resolved
143+
// once here so serve and fetch modes agree. `node:util.parseArgs` rejects
144+
// `--prod=false` for a boolean option, so `values.prod` is `true | undefined`
145+
// and `??` is the correct operator.
146+
const prod = values.prod ?? process.env.NODE_ENV === "production";
147+
139148
if (mode === "fetch") {
140149
const method = values.method || values.request;
141150
const url = values.url || positionals[0] || "/";
142-
return { mode, ...values, url, method };
151+
return { mode, ...values, prod, url, method };
143152
}
144153

145154
// Serve mode: allow entry or dir as a positional argument
@@ -159,7 +168,7 @@ function parseArgs(args: string[]): CLIOptions {
159168
}
160169
}
161170

162-
return { mode, ...values };
171+
return { mode, ...values, prod };
163172
}
164173

165174
async function startServer(cliOpts: CLIOptions) {

‎src/cli/serve.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ function renderError(
112112
): Response {
113113
const safeTitle = escapeHtml(title);
114114
let html = `<!DOCTYPE html><html><head><title>${safeTitle}</title></head><body>`;
115-
if (cliOpts.prod) {
115+
// Never send a stack trace to a remote client when the
116+
// process believes it is in production, even if `prod` resolution regresses again.
117+
// `cliServe` sets NODE_ENV before any request is handled, so it is always set here.
118+
const safeMode = cliOpts.prod || process.env.NODE_ENV === "production";
119+
if (safeMode) {
116120
html += `<h1>${safeTitle}</h1><p>Something went wrong while processing your request.</p>`;
117121
} else {
118122
html += /* html */ `

‎test/cli.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,62 @@ describe("cli", () => {
174174
expect(exitCode).not.toBe(0);
175175
expect(stderr).toMatch(/static/i);
176176
});
177+
178+
it("NODE_ENV=production (no --prod) suppresses the stack-trace error page", async () => {
179+
const port = await getRandomPort("localhost");
180+
const child = runCli(["--entry", entryFile, "--port", String(port)], {
181+
env: { NODE_ENV: "production" },
182+
});
183+
let result: any;
184+
try {
185+
await waitForPort(port, { host: "localhost", delay: 50, retries: 100 });
186+
const res = await fetch(`http://localhost:${port}/boom`);
187+
expect(res.status).toBe(500);
188+
const body = await res.text();
189+
expect(body).toContain("Something went wrong");
190+
expect(body).not.toContain("BOOM_SECRET");
191+
expect(body).not.toMatch(/\n\s+at\s/); // no stack-trace frames
192+
} finally {
193+
child.kill("SIGTERM");
194+
result = await child.catch((error: any) => error);
195+
}
196+
// NODE_ENV also drives the rest of prod mode (banner, .env.production, no watch)
197+
expect(result.stdout).toContain("prod");
198+
});
199+
200+
it("--prod suppresses the stack-trace error page", async () => {
201+
const port = await getRandomPort("localhost");
202+
const child = runCli(["--prod", "--entry", entryFile, "--port", String(port)]);
203+
try {
204+
await waitForPort(port, { host: "localhost", delay: 50, retries: 100 });
205+
const res = await fetch(`http://localhost:${port}/boom`);
206+
expect(res.status).toBe(500);
207+
const body = await res.text();
208+
expect(body).toContain("Something went wrong");
209+
expect(body).not.toContain("BOOM_SECRET");
210+
expect(body).not.toMatch(/\n\s+at\s/); // no stack-trace frames
211+
} finally {
212+
child.kill("SIGTERM");
213+
await child.catch(() => {});
214+
}
215+
});
216+
217+
it("dev mode still shows the stack trace", async () => {
218+
const port = await getRandomPort("localhost");
219+
const child = runCli(["--entry", entryFile, "--port", String(port)], {
220+
env: { NODE_ENV: "development" },
221+
});
222+
try {
223+
await waitForPort(port, { host: "localhost", delay: 50, retries: 100 });
224+
const res = await fetch(`http://localhost:${port}/boom`);
225+
expect(res.status).toBe(500);
226+
const body = await res.text();
227+
expect(body).toContain("BOOM_SECRET");
228+
} finally {
229+
child.kill("SIGTERM");
230+
await child.catch(() => {});
231+
}
232+
});
177233
});
178234

179235
describe("errors", () => {

‎test/fixtures/cli/server.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export const fetch = async (req: Request): Promise<Response> => {
77
if (url.pathname === "/echo") {
88
return new Response(await req.text());
99
}
10+
if (url.pathname === "/boom") {
11+
// Used to assert that the verbose error page (which embeds the stack)
12+
// is only rendered in dev mode.
13+
throw new Error("BOOM_SECRET");
14+
}
1015
if (url.pathname === "/env") {
1116
return new Response(process.env.CLI_TEST_VAR || "");
1217
}

0 commit comments

Comments
 (0)