Skip to content

Commit db3d58c

Browse files
fix: fix path strip panic on Windows (#36)
* fix: Fixed path strip panic on Windows * chore(vscode): update rust analyzer settings * fix(codspeed): fix handling of symbolic links in get_git_relative_path * fixup! fix(codspeed): fix handling of symbolic links in get_git_relative_path --------- Co-authored-by: Adrien Cacciaguerra <[email protected]>
1 parent 69c8309 commit db3d58c

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

.vscode/settings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"rust-analyzer.cargo.extraEnv": {
33
"RUSTFLAGS": "--cfg codspeed"
44
},
5-
"rust-analyzer.cargo.allFeatures": true,
6-
"rust-analyzer.checkOnSave.command": "clippy"
5+
"rust-analyzer.cargo.features": "all",
6+
"editor.formatOnSave": true,
7+
"rust-analyzer.checkOnSave": true,
8+
"rust-analyzer.check.command": "clippy"
79
}

crates/codspeed/src/utils.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ pub fn get_git_relative_path<P>(abs_path: P) -> PathBuf
1717
where
1818
P: AsRef<Path>,
1919
{
20-
let abs_path = abs_path.as_ref();
21-
match abs_path
22-
.canonicalize()
23-
.and_then(|p| get_parent_git_repo_path(&p))
24-
{
25-
Ok(repo_path) => abs_path.strip_prefix(repo_path).unwrap().to_path_buf(),
26-
Err(_) => abs_path.to_path_buf(),
20+
if let Ok(canonicalized_abs_path) = abs_path.as_ref().canonicalize() {
21+
// `repo_path` is still canonicalized as it is a subpath of `canonicalized_abs_path`
22+
if let Ok(repo_path) = get_parent_git_repo_path(&canonicalized_abs_path) {
23+
canonicalized_abs_path
24+
.strip_prefix(repo_path)
25+
.expect("Repository path is malformed.")
26+
.to_path_buf()
27+
} else {
28+
canonicalized_abs_path
29+
}
30+
} else {
31+
abs_path.as_ref().to_path_buf()
2732
}
2833
}
2934

@@ -62,6 +67,18 @@ mod tests {
6267
assert_eq!(relative_path, path_dir.canonicalize().unwrap());
6368
}
6469

70+
#[test]
71+
fn test_get_git_relative_path_not_found_with_symlink() {
72+
let dir = tempdir().unwrap();
73+
let path_dir = dir.path().join("folder");
74+
fs::create_dir_all(&path_dir).unwrap();
75+
let symlink = dir.path().join("symlink");
76+
std::os::unix::fs::symlink(&path_dir, &symlink).unwrap();
77+
78+
let relative_path = get_git_relative_path(&symlink);
79+
assert_eq!(relative_path, symlink.canonicalize().unwrap());
80+
}
81+
6582
#[test]
6683
fn test_get_formated_function_path() {
6784
let input = "std :: vec :: Vec :: new";

0 commit comments

Comments
 (0)