Skip to content

fix: Allow internal module resolution to access builtin modules#6105

Closed
penalosa wants to merge 1 commit intomainfrom
penalosa/extension-workers-cloudflare-module
Closed

fix: Allow internal module resolution to access builtin modules#6105
penalosa wants to merge 1 commit intomainfrom
penalosa/extension-workers-cloudflare-module

Conversation

@penalosa
Copy link
Contributor

@penalosa penalosa commented Feb 18, 2026

When resolving modules with INTERNAL_ONLY option, also allow BUILTIN modules to be resolved as a fallback.

This enables extension modules (like miniflare's dispatch-namespace.worker.ts, which sparked this PR) to import public built-in modules like cloudflare:workers.

Alternative to cloudflare/capnweb#139

When resolving modules with INTERNAL_ONLY option (used by extension
modules and internal builtins), also allow BUILTIN modules to be
resolved as a fallback.

This enables extension modules (like miniflare's dispatch-namespace
worker) to import public built-in modules like cloudflare:workers.

Previously, extension modules could only import INTERNAL modules
(like cloudflare-internal:workers), which have a different export
structure than the public BUILTIN modules. This forced workarounds
in libraries like capnweb that needed to handle both module types.

The resolution order is preserved:
1. First try to find an INTERNAL module (preferred)
2. If not found, try to find a BUILTIN module (fallback)
3. Only if neither is found, return none
@penalosa penalosa marked this pull request as ready for review February 18, 2026 16:43
@penalosa penalosa requested review from a team as code owners February 18, 2026 16:43
penalosa added a commit to cloudflare/workers-sdk that referenced this pull request Feb 18, 2026
Extension workers can now import cloudflare:workers directly instead of
needing it rewritten to cloudflare-internal:workers.

This simplifies the build by removing:
- extensionWorkerPaths list
- rewriteCloudflareWorkersToInternalPlugin

Note: This change depends on cloudflare/workerd#6105 which allows
INTERNAL_ONLY module resolution to also access BUILTIN modules.
Copy link
Collaborator

@jasnell jasnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A key difference between BUILTIN and INTERNAL_ONLY is that BUILTIN modules can be overriden by the worker bundle...

const helloWorld :Workerd.Worker = (
  modules = [
    (name = "worker", esModule = embed "worker.js"),
    (name = "cloudflare:workers", esModule = "export default 1;")
  ],
  compatibilityDate = "2023-02-28",
);
import * as foo from 'cloudflare:workers';
console.log(foo);  // [Module: null prototype] { default: 1 }

We need to evaluate whether this change is correct. The INTERNAL_ONLY behavior here is quite intentional.

penalosa added a commit to cloudflare/workers-sdk that referenced this pull request Feb 18, 2026
Extension workers can now import cloudflare:workers directly instead of
needing it rewritten to cloudflare-internal:workers.

This simplifies the build by removing:
- extensionWorkerPaths list
- rewriteCloudflareWorkersToInternalPlugin

Note: This change depends on cloudflare/workerd#6105 which allows
INTERNAL_ONLY module resolution to also access BUILTIN modules.
@penalosa penalosa closed this Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants