Skip to content

Commit d0d62e6

Browse files
make that unstable_startWorker can correctly throw configuration errors (#9124)
1 parent 5b0c9c9 commit d0d62e6

File tree

6 files changed

+120
-4
lines changed

6 files changed

+120
-4
lines changed

.changeset/metal-clubs-vanish.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
make that `unstable_startWorker` can correctly throw configuration errors
6+
7+
make sure that `unstable_startWorker` can throw configuration related errors when:
8+
9+
- the utility is called
10+
- the worker's `setConfig` is called with the `throwErrors` argument set to `true`
11+
12+
additionally when an error is thrown when `unstable_startWorker` is called make sure
13+
that the worker is properly disposed (since, given the fact that it is not returned
14+
by the utility the utility's caller wouldn't have any way to dispose it themselves)

fixtures/start-worker-node-test/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@cloudflare/workers-tsconfig": "workspace:*",
1313
"@cloudflare/workers-types": "^4.20250507.0",
1414
"@types/is-even": "^1.0.2",
15+
"get-port": "^7.1.0",
1516
"is-even": "^1.0.0",
1617
"miniflare": "workspace:*",
1718
"wrangler": "workspace:*"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import assert from "node:assert";
2+
import test, { describe } from "node:test";
3+
import { setTimeout } from "node:timers/promises";
4+
import getPort from "get-port";
5+
import { unstable_startWorker } from "wrangler";
6+
7+
describe("startWorker - configuration errors", () => {
8+
test("providing an incorrect entrypoint to startWorker", async () => {
9+
await assert.rejects(
10+
unstable_startWorker({
11+
entrypoint: "not a real entrypoint",
12+
}),
13+
(err) => {
14+
assert(err instanceof Error);
15+
assert.match(
16+
err.message,
17+
/he entry-point file at "not a real entrypoint" was not found./
18+
);
19+
return true;
20+
}
21+
);
22+
});
23+
24+
test("providing a non existing config file to startWorker", async () => {
25+
await assert.rejects(
26+
unstable_startWorker({ config: "non-existing-config" }),
27+
(err) => {
28+
assert(err instanceof Error);
29+
assert.match(
30+
err.message,
31+
/Missing entry-point to Worker script or to assets directory/
32+
);
33+
return true;
34+
}
35+
);
36+
});
37+
38+
test("providing an incorrect config to setConfig", async () => {
39+
const worker = await unstable_startWorker({
40+
config: "wrangler.json",
41+
dev: {
42+
server: {
43+
port: await getPort(),
44+
},
45+
inspector: { port: await getPort() },
46+
},
47+
});
48+
49+
await assert.rejects(
50+
worker.setConfig({ config: "non-existing-config" }, true),
51+
(err) => {
52+
assert(err instanceof Error);
53+
assert.match(
54+
err.message,
55+
/Missing entry-point to Worker script or to assets directory/
56+
);
57+
return true;
58+
}
59+
);
60+
61+
// TODO: worker.dispose() should itself await worker.ready
62+
await worker.ready;
63+
await worker.dispose();
64+
});
65+
66+
test("providing an incorrect entrypoint to setConfig", async () => {
67+
const worker = await unstable_startWorker({
68+
config: "wrangler.json",
69+
dev: {
70+
server: {
71+
port: await getPort(),
72+
},
73+
inspector: { port: await getPort() },
74+
},
75+
});
76+
77+
await assert.rejects(
78+
worker.setConfig({ entrypoint: "not a real entrypoint" }, true),
79+
(err) => {
80+
assert(err instanceof Error);
81+
assert.strictEqual(
82+
err.message,
83+
'The entry-point file at "not a real entrypoint" was not found.'
84+
);
85+
return true;
86+
}
87+
);
88+
89+
// TODO: worker.dispose() should itself await worker.ready
90+
await worker.ready;
91+
await worker.dispose();
92+
});
93+
});

fixtures/start-worker-node-test/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
33
"compilerOptions": {
4-
"types": ["@cloudflare/workers-types"],
4+
"types": ["node", "@cloudflare/workers-types"],
55
"checkJs": true
66
},
77
"include": ["**/*.js", "**/*.ts"]

packages/wrangler/src/api/startDevWorker/DevEnv.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export class DevEnv extends EventEmitter {
2020
async startWorker(options: StartDevWorkerInput): Promise<Worker> {
2121
const worker = createWorkerObject(this);
2222

23-
await this.config.set(options);
23+
try {
24+
await this.config.set(options, true);
25+
} catch (e) {
26+
await worker.dispose();
27+
throw e;
28+
}
2429

2530
return worker;
2631
}
@@ -148,8 +153,8 @@ function createWorkerObject(devEnv: DevEnv): Worker {
148153
assert(devEnv.config.latestConfig);
149154
return devEnv.config.latestConfig;
150155
},
151-
setConfig(config) {
152-
return devEnv.config.set(config);
156+
async setConfig(config, throwErrors) {
157+
return devEnv.config.set(config, throwErrors);
153158
},
154159
patchConfig(config) {
155160
return devEnv.config.patch(config);

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)