Skip to content

Commit bfb791e

Browse files
authored
add debug logs for workerd (#9640)
* debug logs for workerd * test * pr feedback * fix tests
1 parent d5edf52 commit bfb791e

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

.changeset/hot-lamps-laugh.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"miniflare": minor
3+
---
4+
5+
Add ability to dump workerd config into a file for debugging.
6+
7+
You can enable this by setting `MINIFLARE_WORKERD_CONFIG_DEBUG` to a file path where you want the config to be written.

packages/miniflare/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,3 +911,5 @@ For example:
911911
```shell
912912
$ export MINIFLARE_WORKERD_PATH="<WORKERD_REPO_DIR>/bazel-bin/src/workerd/server/workerd"
913913
```
914+
915+
For debugging purposes, you can also set `MINIFLARE_WORKERD_CONFIG_DEBUG=<file_path>` which will dump the workerd config to the specified file path.

packages/miniflare/src/runtime/config/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { writeFileSync } from "fs";
12
import { Data, List, Message, Struct } from "capnp-es";
23
import { Config as CapnpConfig } from "./generated";
34
import { Config, kVoid } from "./workerd";
@@ -47,6 +48,10 @@ function encodeCapnpStruct(obj: any, struct: Struct) {
4748
}
4849

4950
export function serializeConfig(config: Config): Buffer {
51+
const debugPath = process.env.MINIFLARE_WORKERD_CONFIG_DEBUG;
52+
if (debugPath) {
53+
writeFileSync(debugPath, JSON.stringify(config, null, 2));
54+
}
5055
const message = new Message();
5156
const struct = message.initRoot(CapnpConfig);
5257
encodeCapnpStruct(config, struct);

packages/miniflare/test/index.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,3 +3327,62 @@ test("Miniflare: custom Node outbound service", async (t) => {
33273327
`Response from custom Node outbound service. The value of "custom-header" is "foo".`
33283328
);
33293329
});
3330+
3331+
test("Miniflare: MINIFLARE_WORKERD_CONFIG_DEBUG controls workerd config file creation", async (t) => {
3332+
const originalEnv = process.env.MINIFLARE_WORKERD_CONFIG_DEBUG;
3333+
const configFilePath = "workerd-config.json";
3334+
3335+
// Clean up any existing config file
3336+
if (existsSync(configFilePath)) {
3337+
await fs.unlink(configFilePath);
3338+
}
3339+
3340+
t.teardown(async () => {
3341+
if (originalEnv === undefined) {
3342+
delete process.env.MINIFLARE_WORKERD_CONFIG_DEBUG;
3343+
} else {
3344+
process.env.MINIFLARE_WORKERD_CONFIG_DEBUG = originalEnv;
3345+
}
3346+
if (existsSync(configFilePath)) {
3347+
await fs.unlink(configFilePath);
3348+
}
3349+
});
3350+
3351+
// ensure the config file is not created without the flag
3352+
delete process.env.MINIFLARE_WORKERD_CONFIG_DEBUG;
3353+
let mf = new Miniflare({
3354+
modules: true,
3355+
script: `export default {
3356+
fetch() {
3357+
return new Response("Hello World");
3358+
}
3359+
}`,
3360+
});
3361+
// Trigger workerd config serialization by dispatching a request
3362+
let response = await mf.dispatchFetch("http://localhost");
3363+
// seems like miniflare doesn't like it if you don't read the response
3364+
await response.text();
3365+
t.false(
3366+
existsSync(configFilePath),
3367+
"config file should not be created when MINIFLARE_WORKERD_CONFIG_DEBUG is not set"
3368+
);
3369+
await mf.dispose();
3370+
3371+
// ensure the config file is created with the flag
3372+
process.env.MINIFLARE_WORKERD_CONFIG_DEBUG = configFilePath;
3373+
mf = new Miniflare({
3374+
modules: true,
3375+
script: `export default {
3376+
fetch() {
3377+
return new Response("Hello World");
3378+
}
3379+
}`,
3380+
});
3381+
response = await mf.dispatchFetch("http://localhost");
3382+
await response.text();
3383+
t.true(
3384+
existsSync(configFilePath),
3385+
"workerd-config.json should be created when MINIFLARE_WORKERD_CONFIG_DEBUG=true"
3386+
);
3387+
await mf.dispose();
3388+
});

packages/miniflare/turbo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"test:ci": {
1010
"dependsOn": ["build"],
11-
"env": ["MINIFLARE_WORKERD_PATH"]
11+
"env": ["MINIFLARE_WORKERD_PATH", "MINIFLARE_WORKERD_CONFIG_DEBUG"]
1212
}
1313
}
1414
}

packages/miniflare/types/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ declare namespace NodeJS {
33
NODE_ENV?: string;
44
NODE_EXTRA_CA_CERTS?: string;
55
MINIFLARE_WORKERD_PATH?: string;
6+
MINIFLARE_WORKERD_CONFIG_DEBUG?: string;
67
MINIFLARE_ASSERT_BODIES_CONSUMED?: "true";
78
}
89
}

0 commit comments

Comments
 (0)