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

Use a cross-platform representation for relative paths in pip compile #3804

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ derivative = { version = "2.2.0" }
directories = { version = "5.0.1" }
dirs-sys = { version = "0.4.1" }
dunce = { version = "1.0.4" }
either = { version = "1.9.0" }
either = { version = "1.12.0" }
encoding_rs_io = { version = "0.1.7" }
flate2 = { version = "1.0.28", default-features = false }
fs-err = { version = "2.11.0" }
Expand All @@ -98,6 +98,7 @@ nanoid = { version = "0.4.0" }
once_cell = { version = "1.19.0" }
owo-colors = { version = "4.0.0" }
path-absolutize = { version = "3.1.1" }
path-slash = { version = "0.2.1" }
pathdiff = { version = "0.2.1" }
petgraph = { version = "0.6.4" }
platform-info = { version = "2.0.2" }
Expand Down
8 changes: 4 additions & 4 deletions crates/distribution-types/src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ impl std::fmt::Display for SourceAnnotation {
match self {
Self::Requirement(origin) => match origin {
RequirementOrigin::File(path) => {
write!(f, "-r {}", path.user_display())
write!(f, "-r {}", path.portable_display())
}
RequirementOrigin::Project(path, project_name) => {
write!(f, "{project_name} ({})", path.user_display())
write!(f, "{project_name} ({})", path.portable_display())
}
},
Self::Constraint(origin) => {
write!(f, "-c {}", origin.path().user_display())
write!(f, "-c {}", origin.path().portable_display())
}
Self::Override(origin) => {
write!(f, "--override {}", origin.path().user_display())
write!(f, "--override {}", origin.path().portable_display())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ uv-normalize = { workspace = true }

anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"], optional = true }
itertools = { workspace = true }
either = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-configuration/src/overrides.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::hash::BuildHasherDefault;

use itertools::Either;
use either::Either;
use rustc_hash::FxHashMap;

use distribution_types::Requirement;
Expand Down
2 changes: 2 additions & 0 deletions crates/uv-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ uv-warnings = { workspace = true }
backoff = { workspace = true }
cachedir = { workspace = true }
dunce = { workspace = true }
either = { workspace = true }
encoding_rs_io = { workspace = true }
fs-err = { workspace = true }
fs2 = { workspace = true }
once_cell = { workspace = true }
path-absolutize = { workspace = true }
path-slash = { workspace = true }
tempfile = { workspace = true }
tracing = { workspace = true }
urlencoding = { workspace = true }
Expand Down
58 changes: 51 additions & 7 deletions crates/uv-fs/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use either::Either;
use std::borrow::Cow;
use std::path::{Component, Path, PathBuf};

use once_cell::sync::Lazy;
use path_slash::PathExt;

/// The current working directory.
pub static CWD: Lazy<PathBuf> =
Expand All @@ -25,41 +27,83 @@ pub trait Simplified {
///
/// On Windows, this will strip the `\\?\` prefix from paths. On other platforms, it's
/// equivalent to [`std::path::Display`].
fn simplified_display(&self) -> std::path::Display;
fn simplified_display(&self) -> impl std::fmt::Display;

/// Canonicalize a path without a `\\?\` prefix on Windows.
fn simple_canonicalize(&self) -> std::io::Result<PathBuf>;

/// Render a [`Path`] for user-facing display.
///
/// Like [`simplified_display`], but relativizes the path against the current working directory.
fn user_display(&self) -> std::path::Display;
fn user_display(&self) -> impl std::fmt::Display;

/// Render a [`Path`] for user-facing display, where the [`Path`] is relative to a base path.
///
/// If the [`Path`] is not relative to the base path, will attempt to relativize the path
/// against the current working directory.
fn user_display_from(&self, base: impl AsRef<Path>) -> impl std::fmt::Display;

/// Render a [`Path`] for user-facing display using a portable representation.
///
/// Like [`user_display`], but uses a portable representation for relative paths.
fn portable_display(&self) -> impl std::fmt::Display;
}

impl<T: AsRef<Path>> Simplified for T {
fn simplified(&self) -> &Path {
dunce::simplified(self.as_ref())
}

fn simplified_display(&self) -> std::path::Display {
fn simplified_display(&self) -> impl std::fmt::Display {
dunce::simplified(self.as_ref()).display()
}

fn simple_canonicalize(&self) -> std::io::Result<PathBuf> {
dunce::canonicalize(self.as_ref())
}

fn user_display(&self) -> std::path::Display {
fn user_display(&self) -> impl std::fmt::Display {
let path = dunce::simplified(self.as_ref());

// Attempt to strip the current working directory, then the canonicalized current working
// directory, in case they differ.
path.strip_prefix(CWD.simplified())
.unwrap_or_else(|_| {
let path = path.strip_prefix(CWD.simplified()).unwrap_or_else(|_| {
path.strip_prefix(CANONICAL_CWD.simplified())
.unwrap_or(path)
});

path.display()
}

fn user_display_from(&self, base: impl AsRef<Path>) -> impl std::fmt::Display {
let path = dunce::simplified(self.as_ref());

// 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)
})
.display()
});

path.display()
}

fn portable_display(&self) -> impl std::fmt::Display {
let path = dunce::simplified(self.as_ref());

// 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)
});

// Use a portable representation for relative paths.
path.to_slash()
.map(Either::Left)
.unwrap_or_else(|| Either::Right(path.display()))
}
}

Expand Down
8 changes: 1 addition & 7 deletions crates/uv-interpreter/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,17 +1256,11 @@ impl fmt::Display for InterpreterNotFound {
path.user_display()
),
Self::ExecutableNotFoundInDirectory(directory, executable) => {
let executable = if let Ok(relative_executable) = executable.strip_prefix(directory)
{
relative_executable.display()
} else {
executable.user_display()
};
write!(
f,
"Interpreter directory `{}` does not contain Python executable at `{}`",
directory.user_display(),
executable
executable.user_display_from(directory)
)
}
Self::ExecutableNotFoundInSearchPath(name) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/pip/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ fn cmd(
}
let args = env::args_os()
.skip(1)
.map(|arg| arg.user_display().to_string())
.map(|arg| arg.to_string_lossy().to_string())
.scan(None, move |skip_next, arg| {
if matches!(skip_next, Some(true)) {
// Reset state; skip this iteration.
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7511,7 +7511,7 @@ fn local_version_of_remote_package() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z requirements.in
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z [TEMP_DIR]/requirements.in
anyio==4.3.0
# via -r requirements.in
idna==3.6
Expand Down Expand Up @@ -7548,7 +7548,7 @@ fn local_version_of_remote_package() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z requirements.in
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z [TEMP_DIR]/requirements.in
anyio==4.3.0
# via -r requirements.in
idna==3.6
Expand Down Expand Up @@ -7580,7 +7580,7 @@ fn local_version_of_remote_package() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z requirements.in --output-file requirements.txt
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z [TEMP_DIR]/requirements.in --output-file [TEMP_DIR]/requirements.txt
anyio==4.3.0
# via -r requirements.in
idna==3.6
Expand Down
Loading