Repository navigation
[lexical-extension] Fix: use Array.from over iterable spread so extension editors build under loose-mode transpilation - #9134
Conversation
…sion editors build under loose-mode transpilation
`buildEditorFromExtensions` and `LexicalExtensionComposer` cannot build an
editor at all in Meta's www monorepo. Every attempt throws at construction:
createEditor: nodes[0] undefined is not a constructor that subclasses
LexicalNode from the lexical package used by this editor
at LexicalBuilder#constructEditor
at LexicalBuilder#buildEditor
at LexicalExtensionComposer
Even an extension with no nodes at all fails:
`buildEditorFromExtensions(defineExtension({name: '[root]'}))`.
The cause is downstream, not a bug in Lexical under native semantics: www
lowers iterable spread in **loose mode**, where `[...x]` compiles to
`[].concat(x)`. That is correct for arrays and silently wrong for every other
iterable, which gets wrapped rather than expanded. Measured in that
environment, with no Lexical involved:
[].concat(new Set([1,2,3])).length === 1 // native spread: 3
[].concat(new Map([[1,2],[3,4]])).length === 1 // native spread: 2
[].concat('abc').length === 1 // native spread: 3
[].concat([1,2,3]).length === 3 // arrays unaffected
`LexicalBuilder.buildCreateEditorArgs()` accumulates nodes into a `Set` and
then does `config.nodes = [...nodes]`, so under that lowering `config.nodes`
becomes `[Set]`. `createEditor` sees a non-function at `nodes[0]`, treats it
as a `{replace, with}` node-replacement descriptor, reads `.replace`, gets
`undefined`, and throws the error above.
`Array.from` is not rewritten by the transform and is equivalent under native
semantics, so this costs nothing and makes the extension packages robust for
consumers whose build lowers spread this way.
Three call sites in `@lexical/extension` spread a non-array iterable:
- `LexicalBuilder.ts` — `config.nodes = [...nodes]` where `nodes` is a `Set`.
This is the one that breaks every extension-built editor.
- `editorStateFamily.ts` — `[...selection._nodes]` where `_nodes` is a
`Set<NodeKey>`, so a serialized node selection would carry `[Set]` as its
keys.
- `editorStateFamily.ts` — `[...__slots]` where `__slots` is a
`Map<string, NodeKey>`, so a serialized node version would carry the Map
itself instead of its entries.
`NodeSelectionDataSelectedExtension.ts:92` also spreads, but both operands are
arrays, so it is unaffected and left alone.
I have only audited `@lexical/extension`, because that is where I have a
reproduction. Other packages contain spreads of possibly-non-array iterables
(by a rough count of the built bundles: `@lexical/html` 7, `@lexical/table` 4,
`lexical` 2) and may hold the same latent problem on paths a loose-mode
consumer has not exercised yet. Happy to follow up if you would like those
swept too.
Test Plan:
Verified against www's synced copy of the built bundles, which is the
environment that reproduces the bug.
Before: a jest probe in that environment calling
`buildEditorFromExtensions(defineExtension({name: '[root]'}))` throws
`nodes[0] undefined ...`. The same happens in a real browser rendering a
migrated editor — this was first caught by a human clicking through the page,
after I wrongly concluded from a jest-only repro that it would not affect
browsers.
After (this change, applied to `LexicalExtension.dev.js` and
`LexicalExtension.prod.js` in that checkout):
- an extension with no nodes builds an editor and registers the 6 core types
- an extension carrying a real product node list (14 classes) builds and
registers all of them:
`["artificial","autolink","chunk-container","chunk-header","code",
"code-highlight","heading","image","linebreak","link","list","listitem",
"paragraph","quote","root","tab","table","tablecell","tablerow","text"]`
A guard assertion in that probe confirms the loose lowering is still in effect
in the environment under test (`[...new Set([1,2,3])].length === 1`), so the
result is not an accident of a changed toolchain.
Not run: this repo's own `pnpm test` / `pnpm flow` (the sandbox used could not
install the pinned toolchain). `Array.from(set)` and `[...set]` are equivalent
under native semantics, so no behaviour change is expected for consumers who
do not lower spread; please let CI confirm.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Adding a browser confirmation to the test plan, since the original only had I loaded a real product editor (a Lexical view migrated from
So the fix is confirmed end to end in a browser, not only in the headless |
|
Naming the exact Babel setting, now that I have found it stated explicitly It is the
So To be explicit about scope: under that assumption Babel emits CI is green on this PR: integrity, unit (22.x and 24.x), browser, e2e canary, |
|
This seems like something we’re going to run into again unless it’s fixed on the meta side (why compile with this setting at all? Seems like solving a problem that hasn’t really existed for years) or transformed by the compiler when building a www release. Doesn’t seem like something we should try and maintain by hand. |
I do agree, last and cheap resort we should have some build/lint rules that enforce this practice. |
|
I'm fine if you want to merge as-is in the meantime to unblock, I just don't want to try and maintain this anachronistic restriction with continued review effort |
|
ill merge this first to unblock the lexical extension composer pilot on www. will explore a more scalable solution on the side |
buildEditorFromExtensionsandLexicalExtensionComposercannot build aneditor at all in Meta's www monorepo. Every attempt throws at construction:
Even an extension with no nodes at all fails:
buildEditorFromExtensions(defineExtension({name: '[root]'})).The cause is downstream, not a bug in Lexical under native semantics: www
lowers iterable spread in loose mode, where
[...x]compiles to[].concat(x). That is correct for arrays and silently wrong for every otheriterable, which gets wrapped rather than expanded. Measured in that
environment, with no Lexical involved:
LexicalBuilder.buildCreateEditorArgs()accumulates nodes into aSetandthen does
config.nodes = [...nodes], so under that loweringconfig.nodesbecomes
[Set].createEditorsees a non-function atnodes[0], treats itas a
{replace, with}node-replacement descriptor, reads.replace, getsundefined, and throws the error above.Array.fromis not rewritten by the transform and is equivalent under nativesemantics, so this costs nothing and makes the extension packages robust for
consumers whose build lowers spread this way.
Three call sites in
@lexical/extensionspread a non-array iterable:LexicalBuilder.ts—config.nodes = [...nodes]wherenodesis aSet.This is the one that breaks every extension-built editor.
editorStateFamily.ts—[...selection._nodes]where_nodesis aSet<NodeKey>, so a serialized node selection would carry[Set]as itskeys.
editorStateFamily.ts—[...__slots]where__slotsis aMap<string, NodeKey>, so a serialized node version would carry the Mapitself instead of its entries.
NodeSelectionDataSelectedExtension.ts:92also spreads, but both operands arearrays, so it is unaffected and left alone.
I have only audited
@lexical/extension, because that is where I have areproduction. Other packages contain spreads of possibly-non-array iterables
(by a rough count of the built bundles:
@lexical/html7,@lexical/table4,lexical2) and may hold the same latent problem on paths a loose-modeconsumer has not exercised yet. Happy to follow up if you would like those
swept too.
Test Plan:
Verified against www's synced copy of the built bundles, which is the
environment that reproduces the bug.
Before: a jest probe in that environment calling
buildEditorFromExtensions(defineExtension({name: '[root]'}))throwsnodes[0] undefined .... The same happens in a real browser rendering amigrated editor — this was first caught by a human clicking through the page,
after I wrongly concluded from a jest-only repro that it would not affect
browsers.
After (this change, applied to
LexicalExtension.dev.jsandLexicalExtension.prod.jsin that checkout):registers all of them:
["artificial","autolink","chunk-container","chunk-header","code", "code-highlight","heading","image","linebreak","link","list","listitem", "paragraph","quote","root","tab","table","tablecell","tablerow","text"]A guard assertion in that probe confirms the loose lowering is still in effect
in the environment under test (
[...new Set([1,2,3])].length === 1), so theresult is not an accident of a changed toolchain.
Not run: this repo's own
pnpm test/pnpm flow(the sandbox used could notinstall the pinned toolchain).
Array.from(set)and[...set]are equivalentunder native semantics, so no behaviour change is expected for consumers who
do not lower spread; please let CI confirm.