Skip to content

[lexical-extension] Bug Fix: Use maybeFromEditor in getPeerDependencyFromEditor#8398

Merged
etrepum merged 1 commit intomainfrom
getPeerDependencyFromEditorfix
Apr 27, 2026
Merged

[lexical-extension] Bug Fix: Use maybeFromEditor in getPeerDependencyFromEditor#8398
etrepum merged 1 commit intomainfrom
getPeerDependencyFromEditorfix

Conversation

@potatowagon
Copy link
Copy Markdown
Contributor

Summary

Fixes a crash introduced by #8360, which moved code block escape logic into CodeNode.insertNewAfter() with a backward-compat fallback that calls getPeerDependencyFromEditor(). The fallback was intended to gracefully handle editors not built with buildEditorFromExtensions(), but getPeerDependencyFromEditor itself called LexicalBuilder.fromEditor(editor) — which throws when no builder is attached to the editor. This means the fallback path could never be reached, and pressing Enter inside a code block crashed the editor.

This affects every editor created with createEditor() directly, which includes all editors on www today.

Call chain before this fix

  1. User presses Enter in a code block
  2. Lexical calls CodeNode.insertNewAfter()
  3. insertNewAfter calls getPeerDependencyFromEditor(editor, 'LexicalCode') to check if CodeExtension is registered
  4. getPeerDependencyFromEditor calls LexicalBuilder.fromEditor(editor)
  5. fromEditor internally calls maybeFromEditor(editor) which returns undefined (no builder attached)
  6. fromEditor sees undefined and throws: "The given editor was not created with LexicalBuilder"
  7. insertNewAfter never gets to evaluate the ! check or run the fallback — it has already crashed

With this fix

  • Step 4 now calls LexicalBuilder.maybeFromEditor(editor) directly, which returns undefined instead of throwing
  • The new if (!builder) return undefined early-returns gracefully
  • Back in insertNewAfter, !undefined is truthy, so it enters the fallback branch and runs the old code block exit logic
  • No crash

This aligns with the function's own JSDoc, which documents the editor parameter as one that "may have been built using extension" and the return type as | undefined.

Test plan

  • Verified on www: the MLCShortcuts-e2e.js test that was failing now passes
  • TypeScript type check passes (pnpm run tsc)

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 27, 2026

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Apr 27, 2026 10:46am
lexical-playground Ready Ready Preview, Comment Apr 27, 2026 10:46am

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 Apr 27, 2026
@etrepum etrepum added this pull request to the merge queue Apr 27, 2026
Merged via the queue into main with commit 59e77e2 Apr 27, 2026
20 checks passed
@etrepum etrepum mentioned this pull request Apr 27, 2026
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.

2 participants