Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle Windows path correctly #555

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 69 additions & 10 deletions tokio-console/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,22 +504,42 @@ impl Attribute {
}
}

// A naive way to determine if a path is a Windows path.
// If the path has a drive letter and more backslashes than forward slashes, it's a Windows path.
fn is_windows_path(path: &str) -> bool {
use once_cell::sync::OnceCell;
use regex::Regex;

static REGEX: OnceCell<Regex> = OnceCell::new();
let regex = REGEX.get_or_init(|| Regex::new(r"^[a-zA-Z]:\\").expect("failed to compile regex"));
let has_drive_letter = regex.is_match(path);
let slash_count = path.find('/').iter().count();
let backslash_count = path.find('\\').iter().count();
has_drive_letter && backslash_count > slash_count
}

fn truncate_registry_path(s: String) -> String {
use once_cell::sync::OnceCell;
use regex::Regex;
use std::borrow::Cow;

static REGEX: OnceCell<Regex> = OnceCell::new();
let regex = REGEX.get_or_init(|| {
Regex::new(r".*/\.cargo(/registry/src/[^/]*/|/git/checkouts/)")
Regex::new(r".*[/\\]\.cargo[/\\](registry[/\\]src[/\\][^/\\]*[/\\]|git[/\\]checkouts[/\\])")
.expect("failed to compile regex")
});

return match regex.replace(&s, "<cargo>/") {
let rep = if is_windows_path(&s) {
"<cargo>\\"
} else {
"<cargo>/"
};

match regex.replace(&s, rep) {
Cow::Owned(s) => s,
// String was not modified, return the original.
Cow::Borrowed(_) => s.to_string(),
};
Cow::Borrowed(_) => s,
}
}

fn format_location(loc: Option<proto::Location>) -> String {
Expand Down Expand Up @@ -586,30 +606,69 @@ mod tests {
#[test]
fn test_format_location_macos() {
// macOS style paths.
let location4 = proto::Location {
let location1 = proto::Location {
file: Some("/Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.0.1/src/lib.rs".to_string()),
..Default::default()
};
let location5 = proto::Location {
let location2 = proto::Location {
file: Some("/Users/user/.cargo/git/checkouts/tokio-1.0.1/src/lib.rs".to_string()),
..Default::default()
};
let location6 = proto::Location {
let location3 = proto::Location {
file: Some("/Users/user/projects/tokio-1.0.1/src/lib.rs".to_string()),
..Default::default()
};

assert_eq!(
format_location(Some(location4)),
format_location(Some(location1)),
"<cargo>/tokio-1.0.1/src/lib.rs"
);
assert_eq!(
format_location(Some(location5)),
format_location(Some(location2)),
"<cargo>/tokio-1.0.1/src/lib.rs"
);
assert_eq!(
format_location(Some(location6)),
format_location(Some(location3)),
"/Users/user/projects/tokio-1.0.1/src/lib.rs"
);
}

#[test]
fn test_format_location_windows() {
// Windows style paths.
let location1 = proto::Location {
file: Some(
"C:\\Users\\user\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\tokio-1.0.1\\src\\lib.rs"
.to_string(),
),
..Default::default()
};

let location2 = proto::Location {
file: Some(
"C:\\Users\\user\\.cargo\\git\\checkouts\\tokio-1.0.1\\src\\lib.rs".to_string(),
),
..Default::default()
};

let location3 = proto::Location {
file: Some("C:\\Users\\user\\projects\\tokio-1.0.1\\src\\lib.rs".to_string()),
..Default::default()
};

assert_eq!(
format_location(Some(location1)),
"<cargo>\\tokio-1.0.1\\src\\lib.rs"
);

assert_eq!(
format_location(Some(location2)),
"<cargo>\\tokio-1.0.1\\src\\lib.rs"
);

assert_eq!(
format_location(Some(location3)),
"C:\\Users\\user\\projects\\tokio-1.0.1\\src\\lib.rs"
);
}
}
Loading