Skip to content

Commit 7b90dbf

Browse files
authored
feat(loader): add error message when a tree-sitter.json file is invalid
1 parent 0dc1044 commit 7b90dbf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

cli/loader/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ pub struct TreeSitterJSON {
147147
}
148148

149149
impl TreeSitterJSON {
150-
#[must_use]
151-
pub fn from_file(path: &Path) -> Option<Self> {
152-
serde_json::from_str(&fs::read_to_string(path.join("tree-sitter.json")).ok()?).ok()
150+
pub fn from_file(path: &Path) -> Result<Self> {
151+
Ok(serde_json::from_str(&fs::read_to_string(
152+
path.join("tree-sitter.json"),
153+
)?)?)
153154
}
154155

155156
#[must_use]
@@ -1119,7 +1120,8 @@ impl Loader {
11191120
) -> Result<&[LanguageConfiguration]> {
11201121
let initial_language_configuration_count = self.language_configurations.len();
11211122

1122-
if let Some(config) = TreeSitterJSON::from_file(parser_path) {
1123+
let ts_json = TreeSitterJSON::from_file(parser_path);
1124+
if let Ok(config) = ts_json {
11231125
let language_count = self.languages_by_id.len();
11241126
for grammar in config.grammars {
11251127
// Determine the path to the parser directory. This can be specified in
@@ -1210,6 +1212,11 @@ impl Loader {
12101212
Some(self.language_configurations.len() - 1);
12111213
}
12121214
}
1215+
} else if let Err(e) = ts_json {
1216+
eprintln!(
1217+
"Warning: Failed to read {} -- {e}",
1218+
parser_path.join("tree-sitter.json").display()
1219+
);
12131220
}
12141221

12151222
// If we didn't find any language configurations in the tree-sitter.json file,

0 commit comments

Comments
 (0)