Fix: avoid infinite transform loop in Shiki/Prism highlight toggle#8607
Fix: avoid infinite transform loop in Shiki/Prism highlight toggle#8607messiawrq-design wants to merge 1 commit into
Conversation
|
Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
I signed it! |
|
Review from potatowagon's AI reviewer: Thanks for the fix! The logic here is correct — using strict inequality checks to avoid the infinite transform loop is the right approach. However, I'd recommend looking at #8606 by @levensta which addresses the same infinite loop issue with a slightly cleaner approach (setting the unsupported flag only once). Between the two PRs: Concerns with this PR:
What I verified: The code logic is sound — the strict inequality checks prevent re-triggering transforms when the state is already set. No regressions to existing behavior. Recommendation: Unless there's a reason to prefer this approach over #8606, I'd suggest the maintainers go with #8606 which doesn't have the CLA/cleanup issues. |
|
The solution in #8606 is better so this PR will be closed when that PR is merged |
potatowagon
left a comment
There was a problem hiding this comment.
Reviewed by Navi (Tater Thoughts Bobblehead) on behalf of @potatowagon.
Concern
What this does: Fixes an infinite transform loop in the Shiki/Prism code highlighters. The bug: getIsSyntaxHighlightSupported() can return null (not just true/false), so !node.getIsSyntaxHighlightSupported() evaluates to true when the value is null, triggering setIsSyntaxHighlightSupported(true), which marks the node dirty, re-triggers the transform, and loops forever. The fix uses strict equality (!== true, !== false) to correctly handle the null/undefined initial state without triggering redundant mutations.
What I checked:
- ✅ Logic correctness: Using strict inequality (
!== true/!== false) correctly handles the three-state scenario (null | true | false) — only calls the setter when the value actually needs to change. - ✅ Applied consistently across all 3 files (CodeHighlighterPrism.ts, CodeHighlighterShiki.ts, FacadeShiki.ts).
- ✅ www compat: No public API changes. Internal behavior fix only.
Issues:
- ❌ CLA check failing — must be signed before merge.
⚠️ Includescode.patchfile — appears to be a stale patch file committed to the repo root. This should be removed before merge or moved to a documentation location.
Note: This PR overlaps with #8606 (same area, Shiki-only). If both land, there may be a merge conflict. #8606 takes a different approach (reordering the loadCodeLanguage call). The approaches are complementary — this PR's strict equality check is more defensive and covers both Prism and Shiki.
Description
Closes #8605
Fixes an issue where changing syntax highlighters (e.g. from Prism to Shiki) on a Code block inside the Playground causes an infinite transform error (
errorOnInfiniteTransforms).The root cause was that
getIsSyntaxHighlightSupported()returnsundefinedinitially or during switching phases when nodes are parsed, which made the!negative condition evaluate to true, thus endlessly re-triggering$codeNodeTransformby callingsetIsSyntaxHighlightSupported(true/false).Implementation Details:
CodeHighlighterPrism.tsto strictly check!== true/!== falsebefore mutatingisSyntaxHighlightSupported.CodeHighlighterShiki.tsandFacadeShiki.ts.pnpm buildandpnpm test.Bounty Payout Note:
If this contribution is eligible for an open-source bounty or reward program under Meta/Lexical initiatives, please route the bounty allocation to the following Solana address:
6sF8p22Gg83NKTJ6dvya7Srv4USCniZnP47DwQwK7Mtp