Skip to content

Commit bd9eb7d

Browse files
authored
Merge pull request github#18770 from hvitved/shared/windows-drive-letter-trap-id
Rust extractors: Normalize drive letter paths with a trailing `/`
2 parents 1cfc8f6 + 02fd23e commit bd9eb7d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

shared/tree-sitter-extractor/src/file_paths.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ pub fn normalize_path(path: &Path) -> String {
88
// have to do a bit of work removing certain prefixes and replacing
99
// backslashes.
1010
let mut components: Vec<String> = Vec::new();
11-
for component in path.components() {
11+
let mut path_components = path.components().peekable();
12+
while let Some(component) = path_components.next() {
1213
match component {
1314
std::path::Component::Prefix(prefix) => match prefix.kind() {
1415
std::path::Prefix::Disk(letter) | std::path::Prefix::VerbatimDisk(letter) => {
@@ -26,7 +27,13 @@ pub fn normalize_path(path: &Path) -> String {
2627
std::path::Component::Normal(n) => {
2728
components.push(n.to_string_lossy().to_string());
2829
}
29-
std::path::Component::RootDir => {}
30+
std::path::Component::RootDir => {
31+
if path_components.peek().is_none() {
32+
// The path points at a root directory, so we need to add a
33+
// trailing slash, e.g. `C:/` instead of `C:`.
34+
components.push("".to_string());
35+
}
36+
}
3037
std::path::Component::CurDir => {}
3138
std::path::Component::ParentDir => {}
3239
}

0 commit comments

Comments
 (0)