Skip to content

Commit 9fd7ed3

Browse files
committed
fix: ensure console.log()s during startup are displayed
As of #4072, whenever Wrangler connected to the V8 inspector, it would clear V8's internal buffer of log messages. This ensured only messages logged while the inspector was connected would be displayed. Unfortunately, we only connect to the inspector once the Worker has reported it's ready to receive connections. This meant any `console.log()`s during Worker startup wouldn't be displayed. This change switches to clearing V8's buffer whenever we _disconnect_ from the inspector instead, ensuring startup logs are shown.
1 parent a1f212e commit 9fd7ed3

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

.changeset/serious-beans-trade.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: ensure `console.log()`s during startup are displayed
6+
7+
Previously, whenever Wrangler connected to the V8 inspector, it would clear V8's
8+
internal buffer of log messages. This ensured only messages logged while the
9+
inspector was connected would be displayed. Unfortunately, we only connect to
10+
the inspector once the Worker has reported it's ready to receive connections.
11+
This meant any `console.log()`s during Worker startup wouldn't be displayed.
12+
13+
This change switches to clearing V8's buffer whenever we _disconnect_ from the
14+
inspector instead, ensuring startup logs are shown. In particular, this should
15+
[fix Remix's HMR](https://github.com/remix-run/remix/issues/7616), which relies
16+
on startup logs to know when the Worker is ready.

fixtures/shared/src/run-wrangler-long-lived.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async function runLongLivedWrangler(command: string[], cwd: string) {
5757
const chunks: Buffer[] = [];
5858
wranglerProcess.stdout?.on("data", (chunk) => chunks.push(chunk));
5959
wranglerProcess.stderr?.on("data", (chunk) => chunks.push(chunk));
60+
const getOutput = () => Buffer.concat(chunks).toString();
6061

6162
const timeoutHandle = setTimeout(() => {
6263
if (settledReadyPromise) return;
@@ -65,7 +66,7 @@ async function runLongLivedWrangler(command: string[], cwd: string) {
6566
const message = [
6667
"Timed out starting long-lived Wrangler:",
6768
separator,
68-
Buffer.concat(chunks).toString(),
69+
getOutput(),
6970
separator,
7071
].join("\n");
7172
rejectReadyPromise(new Error(message));
@@ -85,5 +86,5 @@ async function runLongLivedWrangler(command: string[], cwd: string) {
8586
}
8687

8788
const { ip, port } = await ready;
88-
return { ip, port, stop };
89+
return { ip, port, stop, getOutput };
8990
}

fixtures/worker-app/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { now } from "./dep";
22
import { randomBytes } from "isomorphic-random-example";
33

4+
console.log("startup log");
5+
46
/** @param {Uint8Array} array */
57
function hexEncode(array) {
68
return Array.from(array)
@@ -10,6 +12,8 @@ function hexEncode(array) {
1012

1113
export default {
1214
async fetch(request) {
15+
console.log("request log");
16+
1317
const { pathname } = new URL(request.url);
1418
if (pathname === "/random") return new Response(hexEncode(randomBytes(8)));
1519

fixtures/worker-app/tests/index.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import { describe, it, beforeAll, afterAll } from "vitest";
44
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";
55

66
describe("'wrangler dev' correctly renders pages", () => {
7-
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;
7+
let ip: string,
8+
port: number,
9+
stop: (() => Promise<unknown>) | undefined,
10+
getOutput: () => string;
811

912
beforeAll(async () => {
10-
({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [
11-
"--local",
12-
"--port=0",
13-
]));
13+
({ ip, port, stop, getOutput } = await runWranglerDev(
14+
resolve(__dirname, ".."),
15+
["--local", "--port=0"]
16+
));
1417
});
1518

1619
afterAll(async () => {
@@ -21,6 +24,11 @@ describe("'wrangler dev' correctly renders pages", () => {
2124
const response = await fetch(`http://${ip}:${port}/`);
2225
const text = await response.text();
2326
expect(text).toContain(`http://${ip}:${port}/`);
27+
28+
// Ensure `console.log()`s from startup and requests are shown
29+
const output = getOutput();
30+
expect(output).toContain("startup log");
31+
expect(output).toContain("request log");
2432
});
2533

2634
it("uses `workerd` condition when bundling", async ({ expect }) => {

packages/wrangler/src/dev/inspect.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ export default function useInspector(props: InspectorProps) {
342342
*/
343343
function close(): void {
344344
if (!isClosed()) {
345+
send({
346+
method: "Runtime.discardConsoleEntries",
347+
id: messageCounterRef.current++,
348+
});
345349
try {
346350
ws.close();
347351
} catch (err) {
@@ -378,10 +382,6 @@ export default function useInspector(props: InspectorProps) {
378382
}
379383

380384
ws.addEventListener("open", () => {
381-
send({
382-
method: "Runtime.discardConsoleEntries",
383-
id: messageCounterRef.current++,
384-
});
385385
send({ method: "Runtime.enable", id: messageCounterRef.current++ });
386386
// TODO: This doesn't actually work. Must fix.
387387
send({ method: "Network.enable", id: messageCounterRef.current++ });

0 commit comments

Comments
 (0)