diff --git a/crates/pep508-rs/src/verbatim_url.rs b/crates/pep508-rs/src/verbatim_url.rs index 72dce7503..48acbec79 100644 --- a/crates/pep508-rs/src/verbatim_url.rs +++ b/crates/pep508-rs/src/verbatim_url.rs @@ -1,12 +1,14 @@ use std::borrow::Cow; use std::fmt::Debug; use std::ops::Deref; -use std::path::{Component, Path, PathBuf}; +use std::path::{Path, PathBuf}; use once_cell::sync::Lazy; use regex::Regex; use url::{ParseError, Url}; +use uv_fs::normalize_path; + /// A wrapper around [`Url`] that preserves the original string. #[derive(Debug, Clone, Eq, derivative::Derivative)] #[derivative(PartialEq, Hash)] @@ -60,7 +62,7 @@ impl VerbatimUrl { }; // Normalize the path. - let path = normalize_path(&path); + let path = normalize_path(path); // Convert to a URL. let url = Url::from_file_path(path).expect("path is absolute"); @@ -81,7 +83,7 @@ impl VerbatimUrl { }; // Normalize the path. - let path = normalize_path(&path); + let path = normalize_path(path); // Convert to a URL. let url = Url::from_file_path(path).expect("path is absolute"); @@ -200,36 +202,6 @@ fn expand_env_vars(s: &str, escape: bool) -> Cow<'_, str> { }) } -/// Normalize a path, removing things like `.` and `..`. -/// -/// Source: -fn normalize_path(path: &Path) -> PathBuf { - let mut components = path.components().peekable(); - let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().copied() { - components.next(); - PathBuf::from(c.as_os_str()) - } else { - PathBuf::new() - }; - - for component in components { - match component { - Component::Prefix(..) => unreachable!(), - Component::RootDir => { - ret.push(component.as_os_str()); - } - Component::CurDir => {} - Component::ParentDir => { - ret.pop(); - } - Component::Normal(c) => { - ret.push(c); - } - } - } - ret -} - /// Like [`Url::parse`], but only splits the scheme. Derived from the `url` crate. pub fn split_scheme(s: &str) -> Option<(&str, &str)> { /// diff --git a/crates/uv-fs/src/path.rs b/crates/uv-fs/src/path.rs index 4fec75af7..a890e3f87 100644 --- a/crates/uv-fs/src/path.rs +++ b/crates/uv-fs/src/path.rs @@ -1,5 +1,5 @@ use std::borrow::Cow; -use std::path::Path; +use std::path::{Component, Path, PathBuf}; pub trait Simplified { /// Simplify a [`Path`]. @@ -46,6 +46,36 @@ pub fn normalize_url_path(path: &str) -> Cow<'_, str> { } } +/// Normalize a path, removing things like `.` and `..`. +/// +/// Source: +pub fn normalize_path(path: impl AsRef) -> PathBuf { + let mut components = path.as_ref().components().peekable(); + let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().copied() { + components.next(); + PathBuf::from(c.as_os_str()) + } else { + PathBuf::new() + }; + + for component in components { + match component { + Component::Prefix(..) => unreachable!(), + Component::RootDir => { + ret.push(component.as_os_str()); + } + Component::CurDir => {} + Component::ParentDir => { + ret.pop(); + } + Component::Normal(c) => { + ret.push(c); + } + } + } + ret +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/uv-interpreter/src/python_query.rs b/crates/uv-interpreter/src/python_query.rs index cf8384d9c..503d4564a 100644 --- a/crates/uv-interpreter/src/python_query.rs +++ b/crates/uv-interpreter/src/python_query.rs @@ -8,6 +8,7 @@ use tracing::{debug, instrument}; use platform_host::Platform; use uv_cache::Cache; +use uv_fs::normalize_path; use crate::{Error, Interpreter}; @@ -63,7 +64,8 @@ pub fn find_requested_python( Interpreter::query(&executable, platform.clone(), cache).map(Some) } else { // `-p /home/ferris/.local/bin/python3.10` - let executable = fs_err::canonicalize(request)?; + let executable = normalize_path(request); + Interpreter::query(&executable, platform.clone(), cache).map(Some) } }