Repository navigation
Components: Key useBaseControlProps ids on the hook, not BaseControl - #86322
Conversation
The hook imported BaseControl from the package entry that re-exports the hook. In the CommonJS build that circular import resolves to undefined, so every control built on useBaseControlProps, ToggleGroupControl among them, crashed with "Invalid value used as weak map key". The ESM build was unaffected.
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: 0 B Total Size: 8.61 MB
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in
|
Co-authored-by: retrofox <77539+retrofox@users.noreply.github.com>
mirka
left a comment
There was a problem hiding this comment.
Thanks for reporting 🙏 This looks like a regression from #85476. I opened a broader issue at #86328.
The testing steps don't run as written. There's no build:packages script, and require( 'react' ) fails from the repo root. But more importantly the crash also only seems to happen when no id is passed, which is worth noting.
Co-authored-by: Lena Morita <lena@jaguchi.com>
Co-authored-by: Lena Morita <lena@jaguchi.com>
|
|
||
| ### Bug Fixes | ||
|
|
||
| - `useBaseControlProps`: Fix a crash in the CommonJS build when no `id` is passed, which also affected controls built on the hook such as `ToggleGroupControl` ([#86322](https://github.com/WordPress/gutenberg/pull/86322)). |
There was a problem hiding this comment.
I would word this differently:
useBaseControlProps: fix instance ID generation crash in CommonJS build by avoiding a circular import ofBaseControl.
Current Claude models have a weird and incomprehensible writing style when it comes to code comments and changelog entries. I roll my eyes over them all the time 🙂 Hope it gets better soon.
There was a problem hiding this comment.
Thanks for feedback. Updated.
👍. Added the issue to the PR desc
Updated |
Fix instance ID generation crash in CommonJS build by avoiding a circular import of BaseControl.
…ordPress#86322) * Components: Key useBaseControlProps ids on the hook, not BaseControl The hook imported BaseControl from the package entry that re-exports the hook. In the CommonJS build that circular import resolves to undefined, so every control built on useBaseControlProps, ToggleGroupControl among them, crashed with "Invalid value used as weak map key". The ESM build was unaffected. * Add PR link to components changelog entry Co-authored-by: retrofox <77539+retrofox@users.noreply.github.com> * Update packages/components/src/base-control/hooks.ts Co-authored-by: Lena Morita <lena@jaguchi.com> * Update packages/components/CHANGELOG.md Co-authored-by: Lena Morita <lena@jaguchi.com> * Fix crash in useBaseControlProps for CommonJS build Fix instance ID generation crash in CommonJS build by avoiding a circular import of BaseControl. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: retrofox <77539+retrofox@users.noreply.github.com> Co-authored-by: Lena Morita <lena@jaguchi.com>
What?
useBaseControlPropskeys the generated id on the hook itself instead of onBaseControl, andhooks.tsno longer importsBaseControlat all.Why?
hooks.tsimportedBaseControlfrom./index, the module that re-exports the hook, so the two files import each other. The ESM build handles that cycle through live bindings. The CommonJS build does not:build/base-control/hooks.cjsrequires./index.cjsand wraps the result with__toESM()whileindex.cjsis still loading, so itsdefaultis copied asundefined.Every CommonJS consumer rendering a control built on the hook without an
id, such asToggleGroupControland the DataViews view options that use it, then callsuseInstanceId( undefined )and crashes withTypeError: Invalid value used as weak map key. With anidthe hook returns it before touching its map, so those renders survive.See #86328 for the Rolldown regression behind the CommonJS output.
How?
useInstanceIdonly needs a stable object to key its counter on, so the hook function serves as well as the component. Thewp-components-base-controlprefix is unchanged, so the ids keep their shape. Dropping the import also removes the cycle itself.Testing Instructions
Build the packages, then render the control from the CommonJS build in plain Node, from the package directory so
reactresolves:On trunk, this throws
TypeError: Invalid value used as weak map key. With this change, it prints the rendered markup. Passing anidto the control makes trunk render too, which is why the snippet leaves it out.The unit tests for the control cover the generated ids: