[lexical-y] Bug Fix: Keep a remote read's commit from running node transforms when its text merges under the caret #9779
[vc]: #oDooFyKLO5vhSXF5MGA2EbKuV5byb2o4a3rvsgUnxpA=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJsZXhpY2FsIiwicHJvamVjdElkIjoicHJqX01SYU1PMzFlRklrWTYxU0JxcVBEUjJPank4cVAiLCJ2MCI6ZmFsc2UsInJvb3REaXJlY3RvcnkiOiJwYWNrYWdlcy9sZXhpY2FsLXdlYnNpdGUiLCJpbnNwZWN0b3JVcmwiOiJodHRwczovL3ZlcmNlbC5jb20vZmJvcGVuc291cmNlL2xleGljYWwvRUQxZnQ0YXN0MUY1aFVpVlBRSzNWS2I2dGh3UCIsInByZXZpZXdVcmwiOiJsZXhpY2FsLWdpdC16dXJmeXgteS1xdWlldC1zZWxlY3Rpb24tc2FtZS1vYmplY3QtZmJvcGVuc291cmNlLnZlcmNlbC5hcHAiLCJuZXh0Q29tbWl0U3RhdHVzIjoiREVQTE9ZRUQifSx7Im5hbWUiOiJsZXhpY2FsLXBsYXlncm91bmQiLCJwcm9qZWN0SWQiOiJwcmpfZWE4MnZMajY4NFh6U1JkZU83SlNkMFFOa2F3cyIsInYwIjpmYWxzZSwicm9vdERpcmVjdG9yeSI6InBhY2thZ2VzL2xleGljYWwtcGxheWdyb3VuZCIsImluc3BlY3RvclVybCI6Imh0dHBzOi8vdmVyY2VsLmNvbS9mYm9wZW5zb3VyY2UvbGV4aWNhbC1wbGF5Z3JvdW5kLzJHOFU2YlBOS3k4TGpTZlpjRW01dGkxd2hzQVIiLCJwcmV2aWV3VXJsIjoibGV4aWNhbC1wbGF5Z3JvdW5kLWdpdC16dXJmeXgteS1xdWlldC1zZWxlYy1lZjZiMjItZmJvcGVuc291cmNlLnZlcmNlbC5hcHAiLCJuZXh0Q29tbWl0U3RhdHVzIjoiREVQTE9ZRUQifV0sInJlcXVlc3RSZXZpZXdVcmwiOiJodHRwczovL3ZlcmNlbC5jb20vdmVyY2VsLWFnZW50L3JlcXVlc3QtcmV2aWV3P293bmVyPW1ldGEmcmVwbz1sZXhpY2FsJnByPTk3NzkifQ==
The latest updates on your projects. Learn more about Vercel for GitHub.
| Project | Deployment | Actions | Updated |
| :--- | :----- | :------ | :------ |
| <a href="https://vercel.com/fbopensource/lexical"><sup><img src="https://vercel.com/api/www/avatar?projectId=prj_MRaMO31eFIkY61SBqqPDR2Ojy8qP&teamId=team_QNIQyH5DJlqUjeCckcCcBEjE&s=32" width="16" height="16" align="middle" alt="" /></sup></a> lexical | !Ready Ready | Preview | <relative-time datetime="2026-11-04T06:08:21.568Z">Nov 4, 2026 6:08am UTC</relative-time> |
| <a href="https://vercel.com/fbopensource/lexical-playground"><sup><img src="https://vercel.com/api/www/avatar?projectId=prj_ea82vLj684XzSRdeO7JSd0QNkaws&teamId=team_QNIQyH5DJlqUjeCckcCcBEjE&s=32" width="16" height="16" align="middle" alt="" /></sup></a> lexical-playground | !Ready Ready | Preview | <relative-time datetime="2026-11-04T06:08:21.568Z">Nov 4, 2026 6:08am UTC</relative-time> |
<a href="https://vercel.com/vercel-agent/request-review?owner=meta&repo=lexical&pr=9779" rel="noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://agents-vade-review.vercel.sh/request-review-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://agents-vade-review.vercel.sh/request-review-light.svg"><img src="https://agents-vade-review.vercel.sh/request-review-light.svg" alt="Request Review"></picture></a>
AI review status for this pull request.
| Review status | Updated |
| :--- | :--- |
| 🟢 Reviewed (for maintainers only) | <relative-time datetime="2026-11-04T06:25:46Z">Nov 4, 2026, 6:25 AM UTC</relative-time> |
<!-- codebunny-status:1 r=318341089 p=9779 rv=2 -->
<details><summary><b>lexical</b></summary><p>
npm i https://pkg.pr.new/lexical@9779
</p></details>
<details><summary><b>@lexical/a11y</b></summary><p>
npm i https://pkg.pr.new/@lexical/a11y@9779
</p></details>
<details><summary><b>@lexical/clipboard</b></summary><p>
npm i https://pkg.pr.new/@lexical/clipboard@9779
</p></details>
<details><summary><b>@lexical/code</b></summary><p>
npm i https://pkg.pr.new/@lexical/code@9779
</p></details>
<details><summary><b>@lexical/code-core</b></summary><p>
npm i https://pkg.pr.new/@lexical/code-core@9779
</p></details>
<details><summary><b>@lexical/code-prism</b></summary><p>
npm i https://pkg.pr.new/@lexical/code-prism@9779
</p></details>
<details><summary><b>@lexical/code-shiki</b></summary><p>
npm i https://pkg.pr.new/@lexical/code-shiki@9779
</p></details>
<details><summary><b>@lexical/compiler</b></summary><p>
npm i https://pkg.pr.new/@lexical/compiler@9779
</p></details>
<details><summary><b>@lexical/devtools-core</b></summary><p>
npm i https://pkg.pr.new/@lexical/devtools-core@9779
</p></details>
<details><summary><b>@lexical/dragon</b></summary><p>
npm i https://pkg.pr.new/@lexical/dragon@9779
</p></details>
<details><summary><b>@lexical/eslint-plugin</b></summary><p>
npm i https://pkg.pr.new/@lexical/eslint-plugin@9779
</p></details>
<details><summary><b>@lexical/extension</b></summary><p>
npm i https://pkg.pr.new/@lexical/extension@9779
</p></details>
<details><summary><b>@lexical/fast-check</b></summary><p>
npm i https://pkg.pr.new/@lexical/fast-check@9779
</p></details>
<details><summary><b>@lexical/file</b></summary><p>
npm i https://pkg.pr.new/@lexical/file@9779
</p></details>
<details><summary><b>@lexical/hashtag</b></summary><p>
npm i https://pkg.pr.new/@lexical/hashtag@9779
</p></details>
<details><summary><b>@lexical/headless</b></summary><p>
npm i https://pkg.pr.new/@lexical/headless@9779
</p></details>
<details><summary><b>@lexical/history</b></summary><p>
npm i https://pkg.pr.new/@lexical/history@9779
</p></details>
<details><summary><b>@lexical/html</b></summary><p>
npm i https://pkg.pr.new/@lexical/html@9779
</p></details>
<details><summary><b>@lexical/internal</b></summary><p>
npm i https://pkg.pr.new/@lexical/internal@9779
</p></details>
<details><summary><b>@lexical/link</b></summary><p>
npm i https://pkg.pr.new/@lexical/link@9779
</p></details>
<details><summary><b>@lexical/list</b></summary><p>
npm i https://pkg.pr.new/@lexical/list@9779
</p></details>
<details><summary><b>@lexical/mark</b></summary><p>
npm i https://pkg.pr.new/@lexical/mark@9779
</p></details>
<details><summary><b>@lexical/markdown</b></summary><p>
npm i https://pkg.pr.new/@lexical/markdown@9779
</p></details>
<details><summary><b>@lexical/mdast</b></summary><p>
npm i https://pkg.pr.new/@lexical/mdast@9779
</p></details>
<details><summary><b>@lexical/offset</b></summary><p>
npm i https://pkg.pr.new/@lexical/offset@9779
</p></details>
<details><summary><b>@lexical/overflow</b></summary><p>
npm i https://pkg.pr.new/@lexical/overflow@9779
</p></details>
<details><summary><b>@lexical/plain-text</b></summary><p>
npm i https://pkg.pr.new/@lexical/plain-text@9779
</p></details>
<details><summary><b>@lexical/react</b></summary><p>
npm i https://pkg.pr.new/@lexical/react@9779
</p></details>
<details><summary><b>@lexical/rich-text</b></summary><p>
npm i https://pkg.pr.new/@lexical/rich-text@9779
</p></details>
<details><summary><b>@lexical/selection</b></summary><p>
npm i https://pkg.pr.new/@lexical/selection@9779
</p></details>
<details><summary><b>@lexical/table</b></summary><p>
npm i https://pkg.pr.new/@lexical/table@9779
</p></details>
<details><summary><b>@lexical/tailwind</b></summary><p>
npm i https://pkg.pr.new/@lexical/tailwind@9779
</p></details>
<details><summary><b>@lexical/text</b></summary><p>
npm i https://pkg.pr.new/@lexical/text@9779
</p></details>
<details><summary><b>@lexical/utils</b></summary><p>
npm i https://pkg.pr.new/@lexical/utils@9779
</p></details>
<details><summary><b>@lexical/y</b></summary><p>
npm i https://pkg.pr.new/@lexical/y@9779
</p></details>
<details><summary><b>@lexical/yjs</b></summary><p>
npm i https://pkg.pr.new/@lexical/yjs@9779
</p></details>
_commit: <a href="https://github.com/meta/lexical/runs/126420933844"><code>7660f65</code></a>_
Optional, non-blocking: consider adding a Verify: paragraph after this one. CONTRIBUTING.md (Changesets) and .changeset/README.md (Behavior changes) ask for one when a fix changes observable editing behavior. For example:
Verify: with the code highlighter registered, have a collaborator's change leave the caret in text that Lexical merges, and check that a code highlight outside a code block stays a code highlight and the editor equals a fresh read of its document.
This looks good to me. $quietSelection (packages/lexical-y/src/internal/Binding.ts) now stores the read's selection itself rather than a copy. That closes the gap where the text merge at the end of the update moved the caret after the copy was taken. The reasoning that the stored object cannot change after the commit also holds: committed selections are frozen in development, and every later update builds its own selection. The new regression test reproduces the race with two clients and an outside writer, fails without the fix, and checks the editor against a fresh read of its document.
Optional, non-blocking:
.changeset/lexical-y-read-merged-under-caret.md: consider adding a Verify: paragraph. CONTRIBUTING.md (Changesets) and .changeset/README.md (Behavior changes) ask for one when a fix changes observable editing behavior, and it lists the entry under "Behavior changes" in the changelog. For example: Verify: with the code highlighter registered, have a collaborator's change leave the caret in text that Lexical merges, and check that a code highlight outside a code block stays a code highlight and the editor equals a fresh read of its document.<!-- codebunny-pub:1 k=ce637dd772230b71768ee2d508a35f98b54e34d4ea5e79f1f3859beeada2ca1b -->
Added in 7660f65be.
This looks good to me.
$quietSelection in packages/lexical-y/src/internal/Binding.ts now stores the read's selection itself rather than a copy. That closes the gap where Lexical's end-of-update text merge moved the caret after the copy was taken, so the commit treated it as a selection change and ran every node transform over the content the read brought. The aliasing is bounded: committed selections are frozen in development, every later update builds its own selection, and the SELECTION_CHANGE_COMMAND the binding dispatches after the commit replaces the stored value with a copy again.
The new regression test sits in src/__tests__/unit/regressions/, as packages/lexical-y/AGENTS.md asks for a bug fix. It reproduces the race with two clients and an outside writer, fails 4 of its 6 cases without the fix, and checks the editor against a fresh read of its document as well as the exact expected structure. Thanks for adding the Verify: paragraph: the changeset now matches what CONTRIBUTING.md and .changeset/README.md ask of a fix that changes editing behavior.
No further suggestions.
<!-- codebunny-pub:1 k=965402d2060235545d9f319ea549f97c19569b2103111514e0480fa901f2f24c -->
zurfyx
Description
Every read of a remote change into the editor runs without node transforms,
and at its end the binding counts the read's selection as notified
(
$quietSelection), so that Lexical does not extend the read's commit withSELECTION_CHANGE_COMMAND(an extension runs every node transform over theupdate's changes, whatever the update's options).
The binding counted a copy of the selection. A read can leave two text nodes
side by side that Lexical's normalization merges at the end of the update
(they differ only in what the binding stores for them, such as their
segment), and the caret can be in the second one. The merge moves the
caret's points in place onto the merged node, so at commit the selection no
longer equalled the copy: Lexical extended the read's commit and ran the
transforms. With the code highlighter registered, a code highlight outside a
code block became plain text in that editor only. The commit is a read, so
the binding neither wrote that change nor read the content again: the editor
kept showing plain text while the document, every other client and a fresh
read showed the code highlight.
The binding now counts the selection itself as notified, so the commit
compares it with what the normalization leaves of it. Lexical gives each
later update a selection of its own, so the stored one does not change
after the read commits. Selection listeners still hear of the change from
the update the binding dispatches
SELECTION_CHANGE_COMMANDin after theread.
Found by randomized collaborative sessions (an author's undo read in a quote holding code
text typed concurrently); the cell reproduces it with two clients and an
outside writer.
Test plan
Before
ReadMergedUnderCaret.test.ts: A (no highlighter) stores a code highlightoutside code; B has a root element, the highlighter, and its caret in the
text next to it. An outside writer gives part of the text a segment of its
own and A removes the text B's caret is in.
After
Units,
LEXICAL_Y_GC=0and=1(vitest run --project unit packages/lexical-y):pnpm run ci-checkpasses (Found 0 errors);check-changesetspasses.Unchanged by this PR (same on main)
A read can still run node transforms when a
TextNode'sselectionTransformreplaces the read's selection after the binding counted it (with a new range, a node selection ornull), or changes it in place when the read left the selection equal to the notified one. Both happen on main too; this PR fixes the case where Lexical's own merge moves the caret.