-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[vitest-pool-workers] Fix dynamic import() in entrypoint and DO handlers #13056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df244ce
[vitest-pool-workers] Fix dynamic import() in entrypoint and DO handlers
penalosa 0005938
format: run oxfmt on dynamic-import fixture
penalosa e17979c
fix: fix TS errors in dynamic-import fixture
penalosa 6faae10
fix: fix TS errors in dynamic-import fixture
penalosa 1ab29ab
fix review: remove unused imports, fix comment ref, add transport pat…
penalosa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@cloudflare/vitest-pool-workers": patch | ||
| --- | ||
|
|
||
| fix: Support dynamic `import()` inside entrypoint and Durable Object handlers | ||
|
|
||
| Previously, calling `exports.default.fetch()` or `SELF.fetch()` on a worker whose handler used a dynamic `import()` would hang and fail with "Cannot perform I/O on behalf of a different Durable Object". This happened because the module runner's transport — which communicates over a WebSocket owned by the runner Durable Object — was invoked from a different DO context. | ||
|
|
||
| The fix patches the module runner's transport via the `onModuleRunner` hook so that all `invoke()` calls are routed through the runner DO's I/O context, regardless of where the `import()` originates. |
3 changes: 3 additions & 0 deletions
3
fixtures/vitest-pool-workers-examples/dynamic-import/src/greeting.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export function greet(name: string): string { | ||
| return `Hello, ${name}!`; | ||
| } |
20 changes: 20 additions & 0 deletions
20
fixtures/vitest-pool-workers-examples/dynamic-import/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Durable Object that uses dynamic import() in fetch handler. | ||
| // Regression test for https://github.com/cloudflare/workers-sdk/issues/5387 | ||
| export class GreeterDO implements DurableObject { | ||
| constructor(readonly state: DurableObjectState) {} | ||
| async fetch(request: Request): Promise<Response> { | ||
| const { greet } = await import("./greeting"); | ||
| return new Response(greet("DO")); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request: Request, _env: unknown, _ctx: ExecutionContext): Promise<Response> { | ||
| // Dynamic import inside a fetch handler — this is the pattern that | ||
| // triggers the cross-DO I/O violation in vitest-pool-workers 0.13.x | ||
| // when called via `exports.default.fetch()` in tests. | ||
| // See: https://github.com/cloudflare/workers-sdk/issues/12924 | ||
| const { greet } = await import("./greeting"); | ||
| return new Response(greet("World")); | ||
| }, | ||
| } satisfies ExportedHandler; |
4 changes: 4 additions & 0 deletions
4
fixtures/vitest-pool-workers-examples/dynamic-import/src/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": "../../tsconfig.workerd.json", | ||
| "include": ["./**/*.ts"] | ||
| } |
6 changes: 6 additions & 0 deletions
6
fixtures/vitest-pool-workers-examples/dynamic-import/src/worker-configuration.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| declare namespace Cloudflare { | ||
| interface GlobalProps { | ||
| mainModule: typeof import("./index"); | ||
| } | ||
| } | ||
| interface Env extends Cloudflare.Env {} |
29 changes: 29 additions & 0 deletions
29
fixtures/vitest-pool-workers-examples/dynamic-import/test/dynamic-import.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { env, runDurableObjectAlarm, runInDurableObject } from "cloudflare:test"; | ||
| import { exports } from "cloudflare:workers"; | ||
| import { it } from "vitest"; | ||
|
|
||
| // Regression test for https://github.com/cloudflare/workers-sdk/issues/12924 | ||
| // | ||
| // Calling exports.default.fetch() on a worker whose fetch handler uses a | ||
| // dynamic import() would hang with "Cannot perform I/O on behalf of a | ||
| // different Durable Object". Pre-loading the module (e.g. via a static | ||
| // import of the worker) masks the bug by caching the module. | ||
| it("exports.default.fetch() with dynamic import()", async ({ expect }) => { | ||
| const response = await exports.default.fetch( | ||
| new Request("https://example.com/") | ||
| ); | ||
| expect(response.status).toBe(200); | ||
| expect(await response.text()).toBe("Hello, World!"); | ||
| }); | ||
|
|
||
| // Regression test for https://github.com/cloudflare/workers-sdk/issues/5387 | ||
| // | ||
| // Dynamic import() inside a Durable Object fetch handler has the same | ||
| // cross-context I/O violation when the module isn't already cached. | ||
| it("Durable Object fetch with dynamic import()", async ({ expect }) => { | ||
| const id = env.GREETER.idFromName("test"); | ||
| const stub = env.GREETER.get(id); | ||
| const response = await stub.fetch(new Request("https://example.com/")); | ||
| expect(response.status).toBe(200); | ||
| expect(await response.text()).toBe("Hello, DO!"); | ||
| }); | ||
4 changes: 4 additions & 0 deletions
4
fixtures/vitest-pool-workers-examples/dynamic-import/test/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": "../../tsconfig.workerd-test.json", | ||
| "include": ["./**/*.ts", "../src/**/*.ts"] | ||
| } |
4 changes: 4 additions & 0 deletions
4
fixtures/vitest-pool-workers-examples/dynamic-import/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": "../tsconfig.node.json", | ||
| "include": ["./*.ts"] | ||
| } |
14 changes: 14 additions & 0 deletions
14
fixtures/vitest-pool-workers-examples/dynamic-import/vitest.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { cloudflareTest } from "@cloudflare/vitest-pool-workers"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflareTest({ | ||
| wrangler: { | ||
| configPath: "./wrangler.jsonc", | ||
| }, | ||
| }), | ||
| ], | ||
|
|
||
| test: {}, | ||
| }); |
9 changes: 9 additions & 0 deletions
9
fixtures/vitest-pool-workers-examples/dynamic-import/wrangler.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "dynamic-import", | ||
| "main": "src/index.ts", | ||
| // don't provide compatibility_date so that vitest will infer the latest one | ||
| "durable_objects": { | ||
| "bindings": [{ "name": "GREETER", "class_name": "GreeterDO" }], | ||
| }, | ||
| "migrations": [{ "tag": "v1", "new_classes": ["GreeterDO"] }], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.