Skip to content

wp-build: Fix circular imports in CommonJS output - #86346

Closed
Xmerlin7 wants to merge 3 commits into
WordPress:trunkfrom
Xmerlin7:fix/wp-build-cjs-circular-imports
Closed

Xmerlin7 wants to merge 3 commits into
WordPress:trunkfrom
Xmerlin7:fix/wp-build-cjs-circular-imports

Conversation

@Xmerlin7

@Xmerlin7 Xmerlin7 commented Nov 5, 2026 •

Copy link
Copy Markdown

What?

Closes #86328.

Fixes circular imports in wp-build's CommonJS output, both when an export is read after the cycle has loaded (apiFetch with per_page=-1) and when it is read while the other module is still loading (the memo(undefined) warnings in @wordpress/block-editor).

Why?

Since the move to Rolldown (#85476), the CommonJS output differs from the esbuild output in two ways that break circular imports:

  1. Rolldown's __toESM helper copies a module's exports when it is required. In a cycle the module has not assigned its exports yet, so the copy never gets them. fetch-all-middleware.cjs keeps a copy of index.cjs without default, so per_page=-1 requests fail with (0 , ___index_ts.default) is not a function, and children.fromDOM in @wordpress/blocks silently returns [].
  2. Rolldown assigns each module's exports at the end of the file, after its require() calls. A module that reads an export while the other module is still loading gets undefined, even when the export is a function declaration. That is why block-preview/auto and inner-blocks pass undefined to memo for BlockList. esbuild defined the exports as getters before the require() calls.

How?

Two renderChunk plugins in the CommonJS transpile build:

  • es-module-interop already rewrites Rolldown's __toESM helper. It now returns modules flagged __esModule as they are instead of copying them, so imports see exports assigned later. Such modules still get no synthetic default. Modules not flagged __esModule and isNodeMode calls go through the original helper.
  • exports-first is new. It parses the chunk with rolldown/parseAst, finds the exports.name = localName; assignments at the end of the file, and defines them as getters right after the __esModule header, before any require(). Function declarations are hoisted, so their getters return them at once. Other bindings return their value once it is assigned.

Both plugins throw on output they don't expect, so a change in Rolldown's output fails the build instead of producing broken code. Neither moves the code that the source map covers: the getters go on the header's line, and the original assignments are replaced with spaces.

Each fix covers a case the other cannot. The first is still needed for re-exports such as export { default } from './x', because Rolldown defines those after the require() they read from. The tests cover each case.

Like the esbuild output, exports are now non-writable getters. Code that assigned to a CommonJS export, such as some test mocks, would fail with this output, as it did with esbuild. Rolldown already defined re-exports this way.

Testing Instructions

  1. Run npm run test:unit -- packages/wp-build/lib/test/es-module-interop.test.js. The test runs wp-build on a fixture with circular imports and executes the generated .cjs, so it does not go through the Vitest aliases to package sources. On trunk, the three circular tests fail. Without the __toESM change, the re-exported default test fails. Without exports-first, the read-during-load test fails.
  2. Run npm run build, then in Node require( './packages/block-editor/build/index.cjs' ). The two memo: The first argument must be a component warnings are gone (this needs a DOM, for example jsdom).
  3. In Node, require( './packages/api-fetch/build/index.cjs' ).default, set a fetch handler with setFetchHandler, register fetchAllMiddleware, and request /wp/v2/posts?per_page=-1. It resolves instead of throwing.

I also transpiled all 127 packages with trunk and with this change and compared:

  • Loading each package's CommonJS entry in Node (jsdom for browser packages) gives the same exports with the same types for all 91 packages that load. None throws.
  • In all 4,231 source maps, each of the 849,750 mapped source positions points at the same generated code as on trunk.
  • No generated code assigns to exports.name outside the replaced statements.
  • The full transpile takes about 60s instead of 56s locally.
  • api-fetch with per_page=-1 and children.fromDOM work. I could not reproduce the ToggleGroupControl case in my setup, and did not render the edit-site navigation menu screen. Both are the delayed-read case covered by the first fix.

Testing Instructions for Keyboard

Not applicable, no UI changes.

Use of AI Tools

This PR was written with Claude Code (Claude Opus 5.5). I reviewed the change and the verification described above.

xmerlin7 added 2 commits November 5, 2026 07:31
Rolldown's __toESM helper copies a module's exports when it is required.
In a circular import the module has not assigned its exports yet, so the
copy never gets them. Return modules flagged __esModule as they are.

Part of WordPress#86328.
@github-actions github-actions Bot added the [Package] wp-build /packages/wp-build label Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Xmerlin7! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @darageelys40@example.com, @Xmerlin7.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: darageelys40@example.com, Xmerlin7.

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 5, 2026
Rolldown assigns exports at the end of each CommonJS file, after its
require() calls, so a module that reads an export during a circular
import gets undefined. Define the exports as getters at the top of the
file, like the esbuild output did.

Fixes the memo(undefined) warnings in block-editor (WordPress#86328).
@Xmerlin7 Xmerlin7 changed the title wp-build: Keep CommonJS imports live across circular imports wp-build: Fix circular imports in CommonJS output Nov 5, 2026
@Xmerlin7
Xmerlin7 marked this pull request as ready for review November 5, 2026 06:00
@Xmerlin7

Xmerlin7 commented Nov 5, 2026

Copy link
Copy Markdown
Author

@Mamaduka @mirka
Could a maintainer add the [Type] Bug label? The "Check the type label" check needs it, and I don't have permission to add labels. Thanks!

@Mamaduka Mamaduka added the [Type] Bug An existing feature does not function as intended label Nov 5, 2026
@Mamaduka

Mamaduka commented Nov 5, 2026

Copy link
Copy Markdown
Member

Sure, but I don't think this is the best solution. I know folks are working to fix the circular import causes (#86323.

cc @jsnajdr

@Xmerlin7

Xmerlin7 commented Nov 5, 2026

Copy link
Copy Markdown
Author

Sure, but I don't think this is the best solution. I know folks are working to fix the circular import causes (#86323.

cc @jsnajdr

Thanks, fixing the cycles themselves makes sense, and #86322 and #86323 are good either way.

Two reasons I'd still consider a build fix:

  • wp-build is also used by plugins outside Gutenberg, and source fixes here don't help their circular imports.
  • The api-fetch reproduction (the published 8.1.0 is broken), blocks/children and edit-site cycles have no fix yet, and new cycles will keep appearing. The ESM output and the earlier esbuild output both handled them.

If you'd rather fix this in the source, I'm happy to close this and send PRs removing the remaining cycles instead. @jsnajdr, which would you prefer?

@mirka

mirka commented Nov 5, 2026

Copy link
Copy Markdown
Member

I think the initial wording of my issue implied that it should be fixed in wp-build, so I have corrected that. What clearly should be fixed is the circular imports in the packages. Preserving the previous behavior of wp-build in addition to that is only appropriate once we've established a good reason to do so, which I don't think we have yet.

@Xmerlin7 Xmerlin7 closed this Nov 5, 2026
@Xmerlin7

Xmerlin7 commented Nov 5, 2026

Copy link
Copy Markdown
Author

@Mamaduka I really wanna contribute to Wordpress, any advice or 'good first-issue' assign to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] wp-build /packages/wp-build [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Circular imports fail in CommonJS packages after the Rolldown migration

3 participants