Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/modern-flowers-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"wrangler": patch
---

fix: allow Workflow bindings when calling getPlatformProxy()

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.
59 changes: 29 additions & 30 deletions fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Fetcher,
Hyperdrive,
KVNamespace,
Workflow,
} from "@cloudflare/workers-types";
import type { Unstable_DevWorker } from "wrangler";

Expand All @@ -28,16 +29,21 @@
MY_D1: D1Database;
MY_HYPERDRIVE: Hyperdrive;
ASSETS: Fetcher;
MY_WORKFLOW_INTERNAL: Workflow;
MY_WORKFLOW_EXTERNAL: Workflow;
};

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

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

beforeEach(() => {
// Hide stdout messages from the test logs
vi.spyOn(console, "log").mockImplementation(() => {});
vi.spyOn(console, "error").mockImplementation(() => {});
warn = vi.spyOn(console, "warn").mockImplementation(() => {});
});

describe("var bindings", () => {
Expand Down Expand Up @@ -221,41 +227,20 @@
});

describe("DO warnings", () => {
let warn = {} as MockInstance<typeof console.warn>;
beforeEach(() => {
warn = vi.spyOn(console, "warn").mockImplementation(() => {});
});
afterEach(() => {
warn.mockRestore();
});

it("warns about internal DOs and doesn't crash", async () => {
await getPlatformProxy<Env>({
configPath: path.join(__dirname, "..", "wrangler_internal_do.jsonc"),
});
expect(warn).toMatchInlineSnapshot(`
[MockFunction warn] {
"calls": [
[
"▲ [WARNING]  You have defined bindings to the following internal Durable Objects:

- {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"}
These will not work in local development, but they should work in production.

If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file.
For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings
expect(warn.mock.calls[0][0].replaceAll(/[\r\n]+/g, "\n"))
.toMatchInlineSnapshot(`

Check failure on line 235 in fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts

View workflow job for this annotation

GitHub Actions / Tests (Linux)

tests/get-platform-proxy.env.test.ts > getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash

Error: Snapshot `getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash 1` mismatched - Expected + Received "▲ [WARNING] You have defined bindings to the following internal Durable Objects: - {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"} These will not work in local development, but they should work in production. - + If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file. For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings " ❯ tests/get-platform-proxy.env.test.ts:235:6

Check failure on line 235 in fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts

View workflow job for this annotation

GitHub Actions / Tests (macOS)

tests/get-platform-proxy.env.test.ts > getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash

Error: Snapshot `getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash 1` mismatched - Expected + Received "▲ [WARNING] You have defined bindings to the following internal Durable Objects: - {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"} These will not work in local development, but they should work in production. - + If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file. For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings " ❯ tests/get-platform-proxy.env.test.ts:235:6

Check failure on line 235 in fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts

View workflow job for this annotation

GitHub Actions / Tests (Windows)

tests/get-platform-proxy.env.test.ts > getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash

Error: Snapshot `getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash 1` mismatched - Expected + Received "▲ [WARNING] You have defined bindings to the following internal Durable Objects: - {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"} These will not work in local development, but they should work in production. - + If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file. For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings " ❯ tests/get-platform-proxy.env.test.ts:235:6

Check failure on line 235 in fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts

View workflow job for this annotation

GitHub Actions / Tests (macOS)

tests/get-platform-proxy.env.test.ts > getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash

Error: Snapshot `getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash 1` mismatched - Expected + Received "▲ [WARNING] You have defined bindings to the following internal Durable Objects: - {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"} These will not work in local development, but they should work in production. - + If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file. For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings " ❯ tests/get-platform-proxy.env.test.ts:235:6

Check failure on line 235 in fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts

View workflow job for this annotation

GitHub Actions / Tests (Windows)

tests/get-platform-proxy.env.test.ts > getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash

Error: Snapshot `getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash 1` mismatched - Expected + Received "▲ [WARNING] You have defined bindings to the following internal Durable Objects: - {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"} These will not work in local development, but they should work in production. - + If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file. For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings " ❯ tests/get-platform-proxy.env.test.ts:235:6

Check failure on line 235 in fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts

View workflow job for this annotation

GitHub Actions / Tests (Linux)

tests/get-platform-proxy.env.test.ts > getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash

Error: Snapshot `getPlatformProxy - env > DO warnings > warns about internal DOs and doesn't crash 1` mismatched - Expected + Received "▲ [WARNING] You have defined bindings to the following internal Durable Objects: - {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"} These will not work in local development, but they should work in production. - + If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file. For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings " ❯ tests/get-platform-proxy.env.test.ts:235:6
"▲ [WARNING]  You have defined bindings to the following internal Durable Objects:
- {"class_name":"MyDurableObject","name":"MY_DURABLE_OBJECT"}
These will not work in local development, but they should work in production.

",
],
],
"results": [
{
"type": "return",
"value": undefined,
},
],
}
`);
If you want to develop these locally, you can define your DO in a separate Worker, with a separate configuration file.
For detailed instructions, refer to the Durable Objects section here: https://developers.cloudflare.com/workers/wrangler/api#supported-bindings
"
`);
});

it("doesn't warn about external DOs and doesn't crash", async () => {
Expand All @@ -264,6 +249,20 @@
});
expect(warn).not.toHaveBeenCalled();
});

it("warns about Workflows and doesn't crash", async () => {
await getPlatformProxy<Env>({
configPath: path.join(__dirname, "..", "wrangler_workflow.jsonc"),
});
expect(warn.mock.calls[0][0].replaceAll(/[\r\n]+/g, "\n"))
.toMatchInlineSnapshot(`
"▲ [WARNING]  You have defined bindings to the following Workflows:
- {"binding":"MY_WORKFLOW_INTERNAL","name":"my-workflow-internal","class_name":"MyWorkflowInternal"}
- {"binding":"MY_WORKFLOW_EXTERNAL","name":"my-workflow-external","class_name":"MyWorkflowExternal","script_name":"OtherWorker"}
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.
"
`);
});
});

describe("with a target environment", () => {
Expand Down
1 change: 1 addition & 0 deletions fixtures/get-platform-proxy/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export default defineConfig({
hookTimeout: 25_000,
teardownTimeout: 25_000,
useAtomics: true,
restoreMocks: true,
},
});
22 changes: 22 additions & 0 deletions fixtures/get-platform-proxy/wrangler_workflow.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "workflow-test",
"main": "src/index.ts",
// An internal workflow is indicated by the binding not specifying a script name.
// This implies the workflow is exported alongside this worker in `index.ts`, which it isn't actually.
// However we don't care about this here because `getPlatformProxy()` will discard all user code anyway.
// We are simply making sure the warning shows up.
"compatibility_date": "2023-11-21",
"workflows": [
{
"binding": "MY_WORKFLOW_INTERNAL",
"name": "my-workflow-internal",
"class_name": "MyWorkflowInternal",
},
{
"binding": "MY_WORKFLOW_EXTERNAL",
"name": "my-workflow-external",
"class_name": "MyWorkflowExternal",
"script_name": "OtherWorker",
},
],
}
11 changes: 11 additions & 0 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ async function getMiniflareOptionsFromConfig(
durableObjects: rawConfig["durable_objects"],
});

if (rawConfig.workflows?.length > 0) {
logger.warn(dedent`
You have defined bindings to the following Workflows:
${rawConfig.workflows.map((b) => `- ${JSON.stringify(b)}`).join("\n")}
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.
`);

// Remove workflows from bindings to prevent Miniflare from complaining
bindings.workflows = [];
}

const { bindingOptions, externalWorkers } = buildMiniflareBindingOptions({
name: rawConfig.name,
bindings,
Expand Down
Loading