From 02fd23e53a9bd3666770e1e10169520f37f36b88 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Thu, 13 Feb 2025 13:01:36 +0100 Subject: [PATCH] Rust extractors: Normalize drive letter paths with a trailing `/` --- shared/tree-sitter-extractor/src/file_paths.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/shared/tree-sitter-extractor/src/file_paths.rs b/shared/tree-sitter-extractor/src/file_paths.rs index aa27bbec552d..917a2fb63227 100644 --- a/shared/tree-sitter-extractor/src/file_paths.rs +++ b/shared/tree-sitter-extractor/src/file_paths.rs @@ -8,7 +8,8 @@ pub fn normalize_path(path: &Path) -> String { // have to do a bit of work removing certain prefixes and replacing // backslashes. let mut components: Vec = Vec::new(); - for component in path.components() { + let mut path_components = path.components().peekable(); + while let Some(component) = path_components.next() { match component { std::path::Component::Prefix(prefix) => match prefix.kind() { std::path::Prefix::Disk(letter) | std::path::Prefix::VerbatimDisk(letter) => { @@ -26,7 +27,13 @@ pub fn normalize_path(path: &Path) -> String { std::path::Component::Normal(n) => { components.push(n.to_string_lossy().to_string()); } - std::path::Component::RootDir => {} + std::path::Component::RootDir => { + if path_components.peek().is_none() { + // The path points at a root directory, so we need to add a + // trailing slash, e.g. `C:/` instead of `C:`. + components.push("".to_string()); + } + } std::path::Component::CurDir => {} std::path::Component::ParentDir => {} }