Skip to content

Commit

Permalink
Expand tildes when matching against PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 29, 2024
1 parent 34d7450 commit 4ca0827
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/uv-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
15 changes: 15 additions & 0 deletions crates/uv-shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<PathBuf>());
}
}
path
})
.any(|p| same_file::is_same_file(path, p).unwrap_or(false))
}

Expand Down

0 comments on commit 4ca0827

Please sign in to comment.