From 8a47b3510959f5daba5c8b45bc3d04d68c5bd551 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 3 Nov 2026 13:37:20 +0000 Subject: [PATCH] Do not panic on wrong grammar results (#66474) Closes https://github.com/zed-industries/zed/issues/65051 Adjust the code to not panic if an underlying grammar returns an error. This does not fix the slim extension per se, instead just not showing any highlights at all. To fix Slim extension, it has to update to a never tree-sitter grammar that contains the fix. Release Notes: - Fixed a panic when underlying tree-sitter highlights fail --- crates/language/src/language.rs | 36 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index cf2dfcbd523..720a087c9ac 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -1158,7 +1158,7 @@ impl Language { }; let text_len = text.source_len(); let highlights = if text_len > MAX_TEXT_HIGHLIGHT_ENTRY_BYTES { - Self::compute_resolved_highlights(grammar, &highlights_config.query, text) + self.compute_resolved_highlights(grammar, &highlights_config.query, text) } else { let key = TextHighlightKey::new(text.source_chunks(), text_len); match highlights_config @@ -1172,7 +1172,7 @@ impl Language { highlights_config.text_highlight_cache.insert( key, cached_text, - Self::compute_resolved_highlights(grammar, &highlights_config.query, text), + self.compute_resolved_highlights(grammar, &highlights_config.query, text), ) } } @@ -1187,13 +1187,23 @@ impl Language { } fn compute_resolved_highlights( + &self, grammar: &Arc, query: &tree_sitter::Query, text: &(impl HighlightSource + ?Sized), ) -> ResolvedHighlights { let highlight_map = grammar.highlight_map(); let text_len = text.source_len(); - let tree = text.parse(grammar); + let tree = match text.parse(grammar) { + Ok(tree) => tree, + Err(error) => { + log::warn!( + "highlighting {} text of {text_len} bytes: {error:#}", + self.name() + ); + return ResolvedHighlights::default(); + } + }; let regions = with_query_cursor(|cursor| { cursor.set_byte_range(0..text_len); let mut captures = cursor.captures(query, tree.root_node(), text.text_provider()); @@ -1474,7 +1484,7 @@ impl Debug for Language { pub trait HighlightSource { fn source_len(&self) -> usize; fn source_chunks(&self) -> impl Iterator; - fn parse(&self, grammar: &Grammar) -> Tree; + fn parse(&self, grammar: &Grammar) -> Result; fn text_provider<'a>(&'a self) -> impl tree_sitter::TextProvider<&'a [u8]> + 'a; } @@ -1487,12 +1497,12 @@ impl HighlightSource for str { iter::once(self) } - fn parse(&self, grammar: &Grammar) -> Tree { + fn parse(&self, grammar: &Grammar) -> Result { with_parser(|parser| { + parser.set_language(&grammar.ts_language)?; parser - .set_language(&grammar.ts_language) - .expect("incompatible grammar"); - parser.parse(self, None).unwrap() + .parse(self, None) + .context("tree-sitter returned no tree") }) } @@ -1510,7 +1520,7 @@ impl HighlightSource for Rope { self.chunks() } - fn parse(&self, grammar: &Grammar) -> Tree { + fn parse(&self, grammar: &Grammar) -> Result { parse_text(grammar, self, None) } @@ -1519,11 +1529,9 @@ impl HighlightSource for Rope { } } -pub(crate) fn parse_text(grammar: &Grammar, text: &Rope, old_tree: Option) -> Tree { +pub(crate) fn parse_text(grammar: &Grammar, text: &Rope, old_tree: Option) -> Result { with_parser(|parser| { - parser - .set_language(&grammar.ts_language) - .expect("incompatible grammar"); + parser.set_language(&grammar.ts_language)?; let mut chunks = text.chunks_in_range(0..text.len()); parser .parse_with_options( @@ -1534,7 +1542,7 @@ pub(crate) fn parse_text(grammar: &Grammar, text: &Rope, old_tree: Option) old_tree.as_ref(), None, ) - .unwrap() + .context("tree-sitter returned no tree") }) }