From 7c6c4a0e852b0042156a4254ed13bbae7ffb7cf1 Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 23 Jan 2026 04:36:30 -0600 Subject: [PATCH] Refactor path handling in sourcemap processing - Canonicalized project directory once for consistent comparison, improving the logic for generating relative paths. - Enhanced fallback mechanism for absolute paths when files are outside the project directory, increasing robustness in path management. --- src/cli/sourcemap.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/cli/sourcemap.rs b/src/cli/sourcemap.rs index 05cae541..297f0056 100644 --- a/src/cli/sourcemap.rs +++ b/src/cli/sourcemap.rs @@ -20,7 +20,6 @@ use crate::{ use super::resolve_path; -const PATH_STRIP_FAILED_ERR: &str = "Failed to create relative paths for project file!"; const ABSOLUTE_PATH_FAILED_ERR: &str = "Failed to turn relative path into absolute path!"; /// Representation of a node in the generated sourcemap tree. @@ -198,24 +197,29 @@ fn recurse_create_node<'a>( let mut output_file_paths: Vec> = Vec::with_capacity(instance.metadata().relevant_paths.len()); - if use_absolute_paths { - // It's somewhat important to note here that `path::absolute` takes in a Path and returns a PathBuf - for val in file_paths { - output_file_paths.push(Cow::Owned( - path::absolute(val).expect(ABSOLUTE_PATH_FAILED_ERR), - )); - } - } else { - for val in file_paths { - // Try to create relative path, fall back to absolute if path is outside project dir - match val.strip_prefix(project_dir) { - Ok(relative) => output_file_paths.push(Cow::from(relative)), - Err(_) => output_file_paths.push(Cow::Owned( - path::absolute(val).expect(ABSOLUTE_PATH_FAILED_ERR), - )), + // Canonicalize project_dir once for consistent comparison + let canonical_project_dir = std::fs::canonicalize(project_dir).ok(); + + for val in file_paths { + if use_absolute_paths { + let abs_path = path::absolute(val).expect(ABSOLUTE_PATH_FAILED_ERR); + output_file_paths.push(Cow::Owned(abs_path)); + } else if let Some(ref proj_dir) = canonical_project_dir { + // Canonicalize file path and strip project dir prefix to get relative path + if let Ok(canonical_file) = std::fs::canonicalize(val) { + if let Ok(relative) = canonical_file.strip_prefix(proj_dir) { + output_file_paths.push(Cow::Owned(relative.to_path_buf())); + continue; + } } + // Fallback: file is outside project dir + let abs_path = path::absolute(val).expect(ABSOLUTE_PATH_FAILED_ERR); + output_file_paths.push(Cow::Owned(abs_path)); + } else { + let abs_path = path::absolute(val).expect(ABSOLUTE_PATH_FAILED_ERR); + output_file_paths.push(Cow::Owned(abs_path)); } - }; + } Some(SourcemapNode { name: instance.name(),