Skip to content

Commit 8d07576

Browse files
fix: allow Workflow bindings when calling getPlatformProxy() (#10112)
Workflow bindings are not supported in practice when using `getPlatformProxy()`. But their existence in a Wrangler config file should not prevent other bindings from working. Previously, calling `getPlatformProxy()` would crash if there were any Workflow bindings defined. Now, instead, you get a warning telling you that these bindings are not available. Co-authored-by: Peter Bacon Darwin <[email protected]>
1 parent 8e05701 commit 8d07576

File tree

4 files changed

+73
-31
lines changed

4 files changed

+73
-31
lines changed

.changeset/modern-flowers-care.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: allow Workflow bindings when calling getPlatformProxy()
6+
7+
Workflow bindings are not supported in practice when using `getPlatformProxy()`.
8+
But their existence in a Wrangler config file should not prevent other bindings from working.
9+
Previously, calling `getPlatformProxy()` would crash if there were any Workflow bindings defined.
10+
Now, instead, you get a warning telling you that these bindings are not available.

fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
Hyperdrive,
1717
ImagesBinding,
1818
KVNamespace,
19+
Workflow,
1920
} from "@cloudflare/workers-types";
2021
import type { Unstable_DevWorker } from "wrangler";
2122

@@ -31,16 +32,21 @@ type Env = {
3132
MY_HYPERDRIVE: Hyperdrive;
3233
ASSETS: Fetcher;
3334
IMAGES: ImagesBinding;
35+
MY_WORKFLOW_INTERNAL: Workflow;
36+
MY_WORKFLOW_EXTERNAL: Workflow;
3437
};
3538

3639
const wranglerConfigFilePath = path.join(__dirname, "..", "wrangler.jsonc");
3740

3841
describe("getPlatformProxy - env", () => {
3942
let devWorkers: Unstable_DevWorker[];
43+
let warn = {} as MockInstance<typeof console.warn>;
4044

4145
beforeEach(() => {
4246
// Hide stdout messages from the test logs
4347
vi.spyOn(console, "log").mockImplementation(() => {});
48+
vi.spyOn(console, "error").mockImplementation(() => {});
49+
warn = vi.spyOn(console, "warn").mockImplementation(() => {});
4450
});
4551

4652
describe("var bindings", () => {
@@ -261,41 +267,20 @@ describe("getPlatformProxy - env", () => {
261267
});
262268

263269
describe("DO warnings", () => {
264-
let warn = {} as MockInstance<typeof console.warn>;
265-
beforeEach(() => {
266-
warn = vi.spyOn(console, "warn").mockImplementation(() => {});
267-
});
268-
afterEach(() => {
269-
warn.mockRestore();
270-
});
271-
272270
it("warns about internal DOs and doesn't crash", async () => {
273271
await getPlatformProxy<Env>({
274272
configPath: path.join(__dirname, "..", "wrangler_internal_do.jsonc"),
275273
});
276-
expect(warn).toMatchInlineSnapshot(`
277-
[MockFunction warn] {
278-
"calls": [
279-
[
280-
"▲ [WARNING]  You have defined bindings to the following internal Durable Objects:
281-
282-
- {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"}
283-
These will not work in local development, but they should work in production.
284-
285-
If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file.
286-
For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings
287-
288-
",
289-
],
290-
],
291-
"results": [
292-
{
293-
"type": "return",
294-
"value": undefined,
295-
},
296-
],
297-
}
298-
`);
274+
expect(warn.mock.calls[0][0].replaceAll(/[\r\n]+/g, "\n"))
275+
.toMatchInlineSnapshot(`
276+
"▲ [WARNING]  You have defined bindings to the following internal Durable Objects:
277+
- {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"}
278+
These will not work in local development, but they should work in production.
279+
280+
If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file.
281+
For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings
282+
"
283+
`);
299284
});
300285

301286
it("doesn't warn about external DOs and doesn't crash", async () => {
@@ -304,6 +289,20 @@ describe("getPlatformProxy - env", () => {
304289
});
305290
expect(warn).not.toHaveBeenCalled();
306291
});
292+
293+
it("warns about Workflows and doesn't crash", async () => {
294+
await getPlatformProxy<Env>({
295+
configPath: path.join(__dirname, "..", "wrangler_workflow.jsonc"),
296+
});
297+
expect(warn.mock.calls[0][0].replaceAll(/[\r\n]+/g, "\n"))
298+
.toMatchInlineSnapshot(`
299+
"▲ [WARNING]  You have defined bindings to the following Workflows:
300+
- {"binding":"MY_WORKFLOW_INTERNAL","name":"my-workflow-internal","class_name":"MyWorkflowInternal"}
301+
- {"binding":"MY_WORKFLOW_EXTERNAL","name":"my-workflow-external","class_name":"MyWorkflowExternal","script_name":"OtherWorker"}
302+
These are not available in local development, so you will not be able to bind to them when testing locally, but they should work in production.
303+
"
304+
`);
305+
});
307306
});
308307

309308
describe("with a target environment", () => {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "workflow-test",
3+
"main": "src/index.ts",
4+
// An internal workflow is indicated by the binding not specifying a script name.
5+
// This implies the workflow is exported alongside this worker in `index.ts`, which it isn't actually.
6+
// However we don't care about this here because `getPlatformProxy()` will discard all user code anyway.
7+
// We are simply making sure the warning shows up.
8+
"compatibility_date": "2023-11-21",
9+
"workflows": [
10+
{
11+
"binding": "MY_WORKFLOW_INTERNAL",
12+
"name": "my-workflow-internal",
13+
"class_name": "MyWorkflowInternal",
14+
},
15+
{
16+
"binding": "MY_WORKFLOW_EXTERNAL",
17+
"name": "my-workflow-external",
18+
"class_name": "MyWorkflowExternal",
19+
"script_name": "OtherWorker",
20+
},
21+
],
22+
}

packages/wrangler/src/api/integrations/platform/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,17 @@ async function getMiniflareOptionsFromConfig(args: {
226226
}
227227
}
228228

229+
if (config.workflows?.length > 0) {
230+
logger.warn(dedent`
231+
You have defined bindings to the following Workflows:
232+
${config.workflows.map((b) => `- ${JSON.stringify(b)}`).join("\n")}
233+
These are not available in local development, so you will not be able to bind to them when testing locally, but they should work in production.
234+
`);
235+
236+
// Remove workflows from bindings to prevent Miniflare from complaining
237+
bindings.workflows = [];
238+
}
239+
229240
const { bindingOptions, externalWorkers } = buildMiniflareBindingOptions(
230241
{
231242
name: config.name,

0 commit comments

Comments
 (0)