fix(loader): allow filenames with dots (#5529)

(cherry picked from commit 4cb11acd46)
This commit is contained in:
Volker Mische 2026-04-22 09:41:04 +02:00 committed by Will Lillis
parent a4bdd941d5
commit 8850e11bd8
2 changed files with 51 additions and 2 deletions

View file

@ -127,6 +127,52 @@ fn detect_language_by_double_barrel_file_extension() {
);
}
#[test]
fn detect_language_with_dots_in_filename() {
let blade_dir = tree_sitter_dir(
r#"{
"grammars": [
{
"name": "blade_dots",
"path": ".",
"scope": "source.blade",
"file-types": [
"blade.php"
]
},
{
"name": "php_dots",
"path": ".",
"scope": "source.php",
"file-types": [
"php"
]
}
],
"metadata": {
"version": "0.0.1"
}
}
"#,
"blade_dots",
);
let mut loader = Loader::with_parser_lib_path(scratch_dir().to_path_buf());
let config = loader
.find_language_configurations_at_path(blade_dir.path(), false)
.unwrap();
// this is just to validate that we can read the tree-sitter.json correctly
assert_eq!(config[0].scope.as_ref().unwrap(), "source.blade");
let file_name = blade_dir.path().join("foo.bar.baz.blade.php");
fs::write(&file_name, "").unwrap();
assert_eq!(
get_lang_scope(&loader, &file_name),
Some("source.blade".into())
);
}
#[test]
fn detect_language_without_filename() {
let gitignore_dir = tree_sitter_dir(

View file

@ -810,8 +810,11 @@ impl Loader {
path = PathBuf::from(path.file_stem()?.to_os_string());
}
extensions.reverse();
self.language_configuration_ids_by_file_type
.get(&extensions.join("."))
// Try longest extension suffixs first (e.g. "foo.bar.baz"->"bar.baz"->"baz"),
// stopping at the first match.
(0..extensions.len())
.map(|i| extensions[i..].join("."))
.find_map(|key| self.language_configuration_ids_by_file_type.get(&key))
});
if let Some(configuration_ids) = configuration_ids {