From 7e6df8ffd4570dcd59c60033be3c78a7146ef703 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 1 Sep 2024 14:23:11 -0400 Subject: [PATCH] Remove canonicalize calls (#6919) ## Summary A few of these should use `absolute` instead of `canonicalize`; and apparently we no longer need to strip the `CANONICAL_CWD` to get tests passing. --- crates/uv-fs/src/path.rs | 45 ++++---------------------- crates/uv-virtualenv/src/virtualenv.rs | 2 +- crates/uv/src/commands/project/init.rs | 11 ++----- 3 files changed, 11 insertions(+), 47 deletions(-) diff --git a/crates/uv-fs/src/path.rs b/crates/uv-fs/src/path.rs index 11e88feef..b010a3576 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::{absolute, Component, Path, PathBuf}; +use std::path::{Component, Path, PathBuf}; use std::sync::LazyLock; use either::Either; @@ -9,14 +9,6 @@ use path_slash::PathExt; pub static CWD: LazyLock = LazyLock::new(|| std::env::current_dir().expect("The current directory must exist")); -/// The current working directory, canonicalized. -pub static CANONICAL_CWD: LazyLock = LazyLock::new(|| { - std::env::current_dir() - .expect("The current directory must exist") - .simple_canonicalize() - .expect("The current directory must be canonicalized") -}); - pub trait Simplified { /// Simplify a [`Path`]. /// @@ -61,21 +53,7 @@ impl> Simplified for T { } fn simple_canonicalize(&self) -> std::io::Result { - match dunce::canonicalize(self.as_ref()) { - Ok(path) => Ok(path), - // On Windows, `dunce::canonicalize` can't canonicalize paths on network drives or RAM - // drives. In that case, fall back on `std::path::absolute`, but also verify that the - // path exists. - Err(e) if std::env::consts::OS != "windows" => Err(e), - _ => match absolute(self.as_ref()) { - Ok(path) if path.exists() => Ok(path), - Ok(_) => Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - format!("Path does not exist: {}", self.simplified_display()), - )), - Err(e) => Err(e), - }, - } + dunce::canonicalize(self.as_ref()) } fn user_display(&self) -> impl std::fmt::Display { @@ -88,10 +66,7 @@ impl> Simplified for T { // Attempt to strip the current working directory, then the canonicalized current working // directory, in case they differ. - let path = path.strip_prefix(CWD.simplified()).unwrap_or_else(|_| { - path.strip_prefix(CANONICAL_CWD.simplified()) - .unwrap_or(path) - }); + let path = path.strip_prefix(CWD.simplified()).unwrap_or(path); path.display() } @@ -106,12 +81,9 @@ impl> Simplified for T { // Attempt to strip the base, then the current working directory, then the canonicalized // current working directory, in case they differ. - let path = path.strip_prefix(base.as_ref()).unwrap_or_else(|_| { - path.strip_prefix(CWD.simplified()).unwrap_or_else(|_| { - path.strip_prefix(CANONICAL_CWD.simplified()) - .unwrap_or(path) - }) - }); + let path = path + .strip_prefix(base.as_ref()) + .unwrap_or_else(|_| path.strip_prefix(CWD.simplified()).unwrap_or(path)); path.display() } @@ -121,10 +93,7 @@ impl> Simplified for T { // Attempt to strip the current working directory, then the canonicalized current working // directory, in case they differ. - let path = path.strip_prefix(CWD.simplified()).unwrap_or_else(|_| { - path.strip_prefix(CANONICAL_CWD.simplified()) - .unwrap_or(path) - }); + let path = path.strip_prefix(CWD.simplified()).unwrap_or(path); // Use a portable representation for relative paths. path.to_slash() diff --git a/crates/uv-virtualenv/src/virtualenv.rs b/crates/uv-virtualenv/src/virtualenv.rs index 08d09b619..a1d97b37d 100644 --- a/crates/uv-virtualenv/src/virtualenv.rs +++ b/crates/uv-virtualenv/src/virtualenv.rs @@ -119,7 +119,7 @@ pub(crate) fn create( Err(err) => return Err(Error::Io(err)), } - let location = location.canonicalize()?; + let location = std::path::absolute(location)?; let bin_name = if cfg!(unix) { "bin" diff --git a/crates/uv/src/commands/project/init.rs b/crates/uv/src/commands/project/init.rs index f8fe7f8de..9f156e264 100644 --- a/crates/uv/src/commands/project/init.rs +++ b/crates/uv/src/commands/project/init.rs @@ -47,10 +47,7 @@ pub(crate) async fn init( // Make sure a project does not already exist in the given directory. if path.join("pyproject.toml").exists() { - let path = path - .simple_canonicalize() - .unwrap_or_else(|_| path.simplified().to_path_buf()); - + let path = std::path::absolute(&path).unwrap_or_else(|_| path.simplified().to_path_buf()); anyhow::bail!( "Project is already initialized in `{}`", path.display().cyan() @@ -102,10 +99,8 @@ pub(crate) async fn init( } // Initialized a project in the given directory. Some(path) => { - let path = path - .simple_canonicalize() - .unwrap_or_else(|_| path.simplified().to_path_buf()); - + let path = + std::path::absolute(&path).unwrap_or_else(|_| path.simplified().to_path_buf()); writeln!( printer.stderr(), "Initialized project `{}` at `{}`",