Skip to content

markdown: Fix crashes on tab-indented code blocks - #65398

Merged
SomeoneToIgnore merged 1 commit into
mainfrom
kb/fix-markdown-panic
Oct 9, 2026
Merged

SomeoneToIgnore merged 1 commit into
mainfrom
kb/fix-markdown-panic

Conversation

@SomeoneToIgnore

Copy link
Copy Markdown
Contributor

Fixes #65320

Key cached highlights by typed event indices instead of source offsets, which can collide during tab expansion. Add a layout regression test covering the crash and related edge cases.

Release Notes:

  • Fixed crashes on tab-indented code blocks

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Oct 9, 2026
@zed-community-bot zed-community-bot Bot added the staff Pull requests authored by a current member of Zed staff label Oct 9, 2026
Key cached highlights by typed event indices instead of source offsets, which can collide during tab expansion.
Add a layout regression test covering the crash and related edge cases.

@MrSubidubi MrSubidubi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this, I am almost curious why we do not cache based on text content and rather on event index - the latter I consider infinitely more unstable than the former in a lot of cases. Curious whether I am just missing something? Does highlight_text_resolved not already key by content? What exactly does the cache here add us atop of the existing text highlights cache? Merely curious.

Similarly, took another look just now and noticed that highlight_text_resolved takes a rope by ref - I checked and while this would reach pretty deep, I have the feeling that we could at this stage maybe support passsing in anther trait where the trait requires implementation of textprovider? We currently implement that only for ropes, yet if we were to add this (probably not here though), we could save a ton of large string allocations for cases like this one here perhaps? As we convert the str to a rope, then pass by ref, then potentially again create an entire string from it - that seems wasteful.

Not insisting, maybe something better suited for its own PR to be merged post Wednesday :)

@SomeoneToIgnore

Copy link
Copy Markdown
Contributor Author

Currently, the map is not reused across parses, so it does not need to survive edits — hence it's an ok fix to key by such "unstable" key.
The idea of the cache was to smoothen the scrolling and other rendering shenanigans (before, always re-highlighted on each frame), and we cannot really rely on more stable highlights given sequential edits may invalidate large "past" chunks of markdown: e.g. the inline code block is rendered correctly as an inline code block only after the last ` symbol added — so, I'm afraid that caching it more intelligently and more stable causes more issues than it solves — hence, using the text content is also dangerous?

The allocations is definitely a great point, but not related to the crash per se, and is, indeed, quite a large change, fine to do a follow-up later.

Not sure I follow what's blocking this PR though, given the first paragraph is the curiosity and the other one is postponed?

@MrSubidubi MrSubidubi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking post the clarification around parsing, forgot we clear this during parse events.

That said, I'd be interested in how many cache hits we have in the underlying cache on the language level, given that we are also caching there based on the content (and that should survive reparsing if I am not mistaken/missing something obvious in

pub fn highlight_text<'a>(
self: &'a Arc<Self>,
text: &'a Rope,
range: Range<usize>,
) -> Vec<(Range<usize>, HighlightId)> {
self.highlight_text_resolved(text, range).runs.to_vec()
}
)

Another save for allocations - I suppose we could newtype the runs returned by that method and then get rid of the to_vec there in that method by just returning the wrapped arc and probably deriving deref on that.

@SomeoneToIgnore

Copy link
Copy Markdown
Contributor Author

Yeah, we sure have the test for the reparsing survival, test_code_block_highlights_reused_when_streaming — not sure about the hit rate though, but from the code: completed blocks should hit, while the block still being streamed misses on every parse; theme changes, blocks over 512 KB, and evictions from the shared 4 MB grammar budget also miss.
I guess, we could improve the parsing more on the streaming part, e.g. parse incrementally, but again, I'm sort of concerned that this is a large change with complex invalidation cases like those inline code blocks example, or links or tables or something else...

@SomeoneToIgnore
SomeoneToIgnore added this pull request to the merge queue Oct 9, 2026
Merged via the queue into main with commit 2c99f54 Oct 9, 2026
37 checks passed
@SomeoneToIgnore
SomeoneToIgnore deleted the kb/fix-markdown-panic branch October 9, 2026 16:03
@SomeoneToIgnore

Copy link
Copy Markdown
Contributor Author

/cherry-pick preview
/cherry-pick stable

@zed-zippy

zed-zippy Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

zed-zippy Bot added a commit that referenced this pull request Oct 9, 2026
…ck to preview) (#65406)

Cherry-pick of #65398 to preview

----
Fixes #65320

Key cached highlights by typed event indices instead of source offsets,
which can collide during tab expansion. Add a layout regression test
covering the crash and related edge cases.

Release Notes:

- Fixed crashes on tab-indented code blocks

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
@SomeoneToIgnore

Copy link
Copy Markdown
Contributor Author

I think I've captured most of the advice into a normal form here: #65409

opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-zed-industries__zed that referenced this pull request Oct 15, 2026
Follow-up to
zed-industries#65398 (review)

Not implemented from the feedback:

- Returning the cached `Arc` runs from `highlight_text` behind a
newtype: read-only callers now use `highlight_text_resolved(..).runs`
directly, while `CodeLabel` callers still need an owned `Vec`, which
`highlight_text` now collects once.
- Caching the `Rope` itself to avoid the copy on a cache miss: this
needs a new rope-to-rope comparison and roughly doubles the memory each
entry retains, so the cache keeps one `String` copy.


Release Notes:

- N/A
amirkhakshour pushed a commit to SourceShift/zed-mini-ork that referenced this pull request Oct 18, 2026
Follow-up to
zed-industries#65398 (review)

Not implemented from the feedback:

- Returning the cached `Arc` runs from `highlight_text` behind a
newtype: read-only callers now use `highlight_text_resolved(..).runs`
directly, while `CodeLabel` callers still need an owned `Vec`, which
`highlight_text` now collects once.
- Caching the `Rope` itself to avoid the copy on a cache miss: this
needs a new rope-to-rope comparison and roughly doubles the memory each
entry retains, so the cache keeps one `String` copy.


Release Notes:

- N/A
neowu pushed a commit to neowu/editor that referenced this pull request Oct 21, 2026
Follow-up to
zed-industries/zed#65398 (review)

Not implemented from the feedback:

- Returning the cached `Arc` runs from `highlight_text` behind a
newtype: read-only callers now use `highlight_text_resolved(..).runs`
directly, while `CodeLabel` callers still need an owned `Vec`, which
`highlight_text` now collects once.
- Caching the `Rope` itself to avoid the copy on a cache miss: this
needs a new rope-to-rope comparison and roughly doubles the memory each
entry retains, so the cache keeps one `String` copy.

Release Notes:

- N/A

(cherry picked from commit 7fa2adbe088c8feb3173aec4fc5f8ac0fc153f31)
Aditya-233 pushed a commit to Aditya-233/zed that referenced this pull request Nov 15, 2026
Fixes zed-industries#65320

Key cached highlights by typed event indices instead of source offsets,
which can collide during tab expansion. Add a layout regression test
covering the crash and related edge cases.

Release Notes:

- Fixed crashes on tab-indented code blocks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement staff Pull requests authored by a current member of Zed staff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clicking the Agent Panel button makes Zed crash

2 participants