From 0ce6d757525ee217c0eae1293e047c80636eae47 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 29 Aug 2024 15:50:09 -0400 Subject: [PATCH] Expand tildes when matching against PATH (#6829) ## Summary Closes https://github.com/astral-sh/uv/issues/6802. --- crates/uv-shell/Cargo.toml | 4 ++-- crates/uv-shell/src/lib.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/uv-shell/Cargo.toml b/crates/uv-shell/Cargo.toml index 062bf74e8..a6040c07a 100644 --- a/crates/uv-shell/Cargo.toml +++ b/crates/uv-shell/Cargo.toml @@ -11,8 +11,8 @@ workspace = true uv-fs = { workspace = true } anyhow = { workspace = true } -home = { workspace = true } -same-file = { workspace = true } +home = { workspace = true } +same-file = { workspace = true } tracing = { workspace = true } [target.'cfg(windows)'.dependencies] diff --git a/crates/uv-shell/src/lib.rs b/crates/uv-shell/src/lib.rs index a5722a546..75cab2779 100644 --- a/crates/uv-shell/src/lib.rs +++ b/crates/uv-shell/src/lib.rs @@ -171,10 +171,25 @@ impl Shell { /// Returns `true` if the given path is on the `PATH` in this shell. pub fn contains_path(path: &Path) -> bool { + let home_dir = home::home_dir(); std::env::var_os("PATH") .as_ref() .iter() .flat_map(std::env::split_paths) + .map(|path| { + // If the first component is `~`, expand to the home directory. + if let Some(home_dir) = home_dir.as_ref() { + if path + .components() + .next() + .map(std::path::Component::as_os_str) + == Some("~".as_ref()) + { + return home_dir.join(path.components().skip(1).collect::()); + } + } + path + }) .any(|p| same_file::is_same_file(path, p).unwrap_or(false)) }