Use unambiguous relative paths in uv export (#7378)

This commit is contained in:
Charlie Marsh 2024-09-13 16:48:46 -04:00 committed by GitHub
parent c1888364b5
commit 54f171c80a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 13 deletions

View file

@ -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;
@ -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;
@ -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)),
}
}