diff --git a/crates/codspeed/src/macros.rs b/crates/codspeed/src/macros.rs index df8414ab..f910085a 100644 --- a/crates/codspeed/src/macros.rs +++ b/crates/codspeed/src/macros.rs @@ -13,10 +13,18 @@ macro_rules! abs_file { #[macro_export] macro_rules! codspeed_uri { ( $name:ident ) => { - format!("{}::{}", file!(), stringify!($name)) + format!( + "{}::{}", + $crate::utils::normalize_file_path(file!()), + stringify!($name) + ) }; ( $function:path ) => { - format!("{}::{}", file!(), stringify!($function)) + format!( + "{}::{}", + $crate::utils::normalize_file_path(file!()), + stringify!($function) + ) }; } diff --git a/crates/codspeed/src/utils.rs b/crates/codspeed/src/utils.rs index 872a1513..c879c962 100644 --- a/crates/codspeed/src/utils.rs +++ b/crates/codspeed/src/utils.rs @@ -1,5 +1,6 @@ +use std::borrow::Cow; use std::io; -use std::path::{Path, PathBuf}; +use std::path::{Component, Path, PathBuf}; fn get_parent_git_repo_path(abs_path: &Path) -> io::Result { if abs_path.join(".git").exists() { @@ -63,6 +64,48 @@ pub fn caller_file_path() -> String { } } +/// Removes the `.` and `..` segments of a `file!()` path, for the benchmark +/// URI. It returns paths without such segments unchanged. +/// +/// rustc keeps these segments when a module comes from `#[path = "../x.rs"]` +/// or a target from `[[bench]] path = "../x.rs"`. In a URI, a URL parser +/// resolves them, so the benchmark page and its flame graph break. +/// +/// It doesn't touch the file system. A `..` segment removes the segment +/// before it, so leading `..` segments stay. +pub fn normalize_file_path(file: &str) -> Cow<'_, str> { + // Splits the string: `Path::components` hides the `.` segments not at the start + let has_dot_segment = file + .split(std::path::is_separator) + .any(|segment| segment == "." || segment == ".."); + if !has_dot_segment { + return Cow::Borrowed(file); + } + + let mut components: Vec = Vec::new(); + for component in Path::new(file).components() { + match component { + Component::CurDir => {} + Component::ParentDir => match components.last() { + Some(Component::Normal(_)) => { + components.pop(); + } + // The root has no parent + Some(Component::RootDir | Component::Prefix(_)) => {} + _ => components.push(component), + }, + _ => components.push(component), + } + } + Cow::Owned( + components + .iter() + .collect::() + .to_string_lossy() + .into_owned(), + ) +} + /// Fixes spaces around `::` created by stringify!($function). pub fn get_formated_function_path(function_path: impl Into) -> String { let function_path = function_path.into(); @@ -142,6 +185,41 @@ mod tests { assert_eq!(relative_path, symlink.canonicalize().unwrap()); } + #[test] + fn test_normalize_file_path_removes_dot_segments() { + let cases = [ + ("crates/p/benches/../src/x.rs", "crates/p/src/x.rs"), + ("crates/p/./benches/x.rs", "crates/p/benches/x.rs"), + ("./benches/x.rs", "benches/x.rs"), + ("benches/../../shared/x.rs", "../shared/x.rs"), + ("../../x.rs", "../../x.rs"), + ]; + for (file, expected) in cases { + assert_eq!( + Path::new(normalize_file_path(file).as_ref()), + Path::new(expected), + "{file}" + ); + } + } + + #[cfg(unix)] + #[test] + fn test_normalize_file_path_keeps_absolute_paths_absolute() { + assert_eq!( + normalize_file_path("/home/ci/ws/a/../b.rs"), + "/home/ci/ws/b.rs" + ); + assert_eq!(normalize_file_path("/../x.rs"), "/x.rs"); + } + + #[test] + fn test_normalize_file_path_keeps_paths_without_dot_segments() { + for file in ["benches/bench.rs", "a//b.rs", "a/.../b.rs", "a/..b.rs"] { + assert!(matches!(normalize_file_path(file), Cow::Borrowed(f) if f == file)); + } + } + /// COD-2324: empty segments (no file/macro group when `criterion_group!` is bypassed) /// must not produce URIs like `::::my_group::my_bench`. #[test] diff --git a/crates/divan_compat/divan_fork/src/divan.rs b/crates/divan_compat/divan_fork/src/divan.rs index 3a78d6c1..827c8762 100644 --- a/crates/divan_compat/divan_fork/src/divan.rs +++ b/crates/divan_compat/divan_fork/src/divan.rs @@ -413,7 +413,7 @@ mod codspeed { } } - let file = bench_entry.meta().location.file; + let file = ::codspeed::utils::normalize_file_path(bench_entry.meta().location.file); let mut module_path = bench_entry.meta().module_path_components().skip(1).collect::>().join("::"); if !module_path.is_empty() { diff --git a/crates/divan_compat/src/compat/uri.rs b/crates/divan_compat/src/compat/uri.rs index e9ece793..1956acda 100644 --- a/crates/divan_compat/src/compat/uri.rs +++ b/crates/divan_compat/src/compat/uri.rs @@ -66,7 +66,7 @@ pub(crate) fn generate(bench_entry: &AnyBenchEntry, closure_bench_display_name: } } - let file = bench_entry.meta().location.file; + let file = codspeed::utils::normalize_file_path(bench_entry.meta().location.file); // In the context of a bench, the top level module will be a repetition of the file name, we // chose to skip it let mut module_path = bench_entry @@ -161,6 +161,30 @@ mod tests { assert_eq!(uri, "bar.rs::bench_function"); } + #[test] + fn test_generate_removes_dot_segments_from_the_file() { + let meta = EntryMeta { + display_name: "bench_function", + raw_name: "bench_function", + module_path: "test::inner", + location: EntryLocation { + file: "crates/p/benches/../src/inner.rs", + ..Default::default() + }, + bench_options: None, + }; + let bench_entry = BenchEntry { + meta, + bench: BenchEntryRunner::Plain(|_| {}), + }; + let uri = generate(&AnyBenchEntry::Bench(&bench_entry), "root/../"); + // The args keep their dot segments: they are not a path + assert_eq!( + uri, + "crates/p/src/inner.rs::inner::bench_function[root/../]" + ); + } + #[allow(non_upper_case_globals)] static mock_group_entry: GroupEntry = GroupEntry { meta: EntryMeta {