Skip to content

[lexical-extension] Fix: use Array.from over iterable spread so extension editors build under loose-mode transpilation - #9134

Merged
potatowagon merged 1 commit into
meta:mainfrom
potatowagon:builder-nodes-array-from
Sep 9, 2026
Merged

potatowagon merged 1 commit into
meta:mainfrom
potatowagon:builder-nodes-array-from

Conversation

@potatowagon

Copy link
Copy Markdown
Contributor

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.

…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.
@vercel

vercel Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 8, 2026 9:55am UTC
lexical-playground Ready Ready Preview Sep 8, 2026 9:55am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 8, 2026
@potatowagon

potatowagon commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Adding a browser confirmation to the test plan, since the original only had
headless evidence.

I loaded a real product editor (a Lexical view migrated from
LexicalComposer to LexicalExtensionComposer) in Chrome, served from a www
sandbox with these two Array.from changes applied to the built
@lexical/extension bundles.

  • Before: the page failed to render, with createEditor: nodes[0] undefined is not a constructor that subclasses LexicalNode thrown from
    LexicalBuilder#constructEditor via LexicalExtensionComposer, exactly as
    in the description.
  • After: the editor renders and the document displays normally.

So the fix is confirmed end to end in a browser, not only in the headless
probe.

@potatowagon

Copy link
Copy Markdown
Contributor Author

Naming the exact Babel setting, now that I have found it stated explicitly
rather than inferred from behaviour.

It is the iterableIsArray assumption, applied codebase-wide in www. An
unrelated team hit the same thing and left this note in their own test file:

// Array.from, not array spread: this codebase's Babel assumes
// `iterableIsArray`, so `[...aSet]` yields `[aSet]` rather than iterating.

So Array.from is already the established local idiom for this, arrived at
independently — which is some reassurance that this PR is the right shape and
not a workaround peculiar to my reproduction.

To be explicit about scope: under that assumption Babel emits [].concat(x)
for [...x], which is correct for arrays and wrong for every other iterable.
Consumers on standard Babel, native ESM, or any build that does not set the
assumption get the spec-correct iterator protocol and are unaffected — so this
is a robustness change for downstream builds, not a correctness bug in Lexical
under native semantics.

CI is green on this PR: integrity, unit (22.x and 24.x), browser, e2e canary,
dependency-check, and both Vercel deploys.

@etrepum

etrepum commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

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.

@zurfyx

zurfyx commented Sep 8, 2026

Copy link
Copy Markdown
Member

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.

@etrepum

etrepum commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

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

@potatowagon
potatowagon added this pull request to the merge queue Sep 9, 2026
@potatowagon

Copy link
Copy Markdown
Contributor Author

ill merge this first to unblock the lexical extension composer pilot on www. will explore a more scalable solution on the side

Merged via the queue into meta:main with commit e0e53e1 Sep 9, 2026
46 checks passed
@etrepum etrepum mentioned this pull request Sep 17, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 0484b538 Deployed Sep 8, 2026 by vercel[bot]
Preview – lexical-playground — 0484b538 Deployed Sep 8, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants