From 340d3eeb41e15bac5d33ae0e45faa0a632655834 Mon Sep 17 00:00:00 2001 From: Will Lillis Date: Sat, 9 Nov 2024 20:46:29 -0500 Subject: [PATCH] feat(loader): add error message when a `tree-sitter.json` file is invalid (cherry picked from commit 7b90dbf189d7e8e8eabb36bfd5d1910465c1b8b9) --- cli/loader/src/lib.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cli/loader/src/lib.rs b/cli/loader/src/lib.rs index 5c09a06b..1ee006f0 100644 --- a/cli/loader/src/lib.rs +++ b/cli/loader/src/lib.rs @@ -146,8 +146,10 @@ pub struct TreeSitterJSON { } impl TreeSitterJSON { - pub fn from_file(path: &Path) -> Option { - serde_json::from_str(&fs::read_to_string(path.join("tree-sitter.json")).ok()?).ok() + pub fn from_file(path: &Path) -> Result { + Ok(serde_json::from_str(&fs::read_to_string( + path.join("tree-sitter.json"), + )?)?) } pub fn has_multiple_language_configs(&self) -> bool { @@ -1115,7 +1117,8 @@ impl Loader { ) -> Result<&[LanguageConfiguration]> { let initial_language_configuration_count = self.language_configurations.len(); - if let Some(config) = TreeSitterJSON::from_file(parser_path) { + let ts_json = TreeSitterJSON::from_file(parser_path); + if let Ok(config) = ts_json { let language_count = self.languages_by_id.len(); for grammar in config.grammars { // Determine the path to the parser directory. This can be specified in @@ -1206,6 +1209,11 @@ impl Loader { Some(self.language_configurations.len() - 1); } } + } else if let Err(e) = ts_json { + eprintln!( + "Warning: Failed to read {} -- {e}", + parser_path.join("tree-sitter.json").display() + ); } // If we didn't find any language configurations in the tree-sitter.json file,