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 unambiguous relative paths in uv export #7378

Merged
merged 1 commit into from
Sep 13, 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
26 changes: 16 additions & 10 deletions crates/uv-resolver/src/lock/requirements_txt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::borrow::Cow;
use std::collections::hash_map::Entry;
use std::collections::VecDeque;
use std::fmt::Formatter;
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};

use either::Either;
use petgraph::visit::IntoNodeReferences;
Expand Down Expand Up @@ -191,20 +192,14 @@ impl std::fmt::Display for RequirementsTxtExport<'_> {
write!(f, "{} @ {}", package.id.name, url)?;
}
Source::Path(path) | Source::Directory(path) => {
if path.as_os_str().is_empty() {
write!(f, ".")?;
} else if path.is_absolute() {
if path.is_absolute() {
write!(f, "{}", Url::from_file_path(path).unwrap())?;
} else {
write!(f, "{}", path.portable_display())?;
write!(f, "{}", anchor(path).portable_display())?;
}
}
Source::Editable(path) => {
if path.as_os_str().is_empty() {
write!(f, "-e .")?;
} else {
write!(f, "-e {}", path.portable_display())?;
}
write!(f, "-e {}", anchor(path).portable_display())?;
}
Source::Virtual(_) => {
continue;
Expand Down Expand Up @@ -252,3 +247,14 @@ impl<'lock> From<&'lock Package> for NodeComparator<'lock> {
}
}
}

/// Modify a relative [`Path`] to anchor it at the current working directory.
///
/// For example, given `foo/bar`, returns `./foo/bar`.
fn anchor(path: &Path) -> Cow<'_, Path> {
match path.components().next() {
None => Cow::Owned(PathBuf::from(".")),
Some(Component::CurDir | Component::ParentDir) => Cow::Borrowed(path),
_ => Cow::Owned(PathBuf::from("./").join(path)),
}
}
6 changes: 3 additions & 3 deletions crates/uv/tests/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ fn non_root() -> Result<()> {
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv export --cache-dir [CACHE_DIR] --package child
-e child
-e ./child
iniconfig==2.0.0 \
--hash=sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3 \
--hash=sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374
Expand Down Expand Up @@ -767,7 +767,7 @@ fn no_emit() -> Result<()> {
# This file was autogenerated by uv via the following command:
# uv export --cache-dir [CACHE_DIR] --no-emit-package anyio
-e .
-e child
-e ./child
idna==3.6 \
--hash=sha256:9ecdbbd083b06798ae1e86adcbfe8ab1479cf864e4ee30fe4e46a003d12491ca \
--hash=sha256:c05567e9c24a6b9faaa835c4821bad0590fbb9d5779e7caa6e1cc4978e7eb24f
Expand All @@ -789,7 +789,7 @@ fn no_emit() -> Result<()> {
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv export --cache-dir [CACHE_DIR] --no-emit-project
-e child
-e ./child
anyio==3.7.0 \
--hash=sha256:275d9973793619a5374e1c89a4f4ad3f4b0a5510a2b5b939444bee8f4c4d37ce \
--hash=sha256:eddca883c4175f14df8aedce21054bfca3adb70ffe76a9f607aef9d7fa2ea7f0
Expand Down
Loading