Skip to content

Commit aef6a87

Browse files
committed
Use unambiguous relative paths in uv export
1 parent c188836 commit aef6a87

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

crates/uv-resolver/src/lock/requirements_txt.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use std::borrow::Cow;
12
use std::collections::hash_map::Entry;
23
use std::collections::VecDeque;
34
use std::fmt::Formatter;
4-
use std::path::{Path, PathBuf};
5+
use std::path::{Component, Path, PathBuf};
56

67
use either::Either;
78
use petgraph::visit::IntoNodeReferences;
@@ -191,20 +192,14 @@ impl std::fmt::Display for RequirementsTxtExport<'_> {
191192
write!(f, "{} @ {}", package.id.name, url)?;
192193
}
193194
Source::Path(path) | Source::Directory(path) => {
194-
if path.as_os_str().is_empty() {
195-
write!(f, ".")?;
196-
} else if path.is_absolute() {
195+
if path.is_absolute() {
197196
write!(f, "{}", Url::from_file_path(path).unwrap())?;
198197
} else {
199-
write!(f, "{}", path.portable_display())?;
198+
write!(f, "{}", anchor(path).portable_display())?;
200199
}
201200
}
202201
Source::Editable(path) => {
203-
if path.as_os_str().is_empty() {
204-
write!(f, "-e .")?;
205-
} else {
206-
write!(f, "-e {}", path.portable_display())?;
207-
}
202+
write!(f, "-e {}", anchor(path).portable_display())?;
208203
}
209204
Source::Virtual(_) => {
210205
continue;
@@ -252,3 +247,14 @@ impl<'lock> From<&'lock Package> for NodeComparator<'lock> {
252247
}
253248
}
254249
}
250+
251+
/// Modify a relative [`Path`] to anchor it at the current working directory.
252+
///
253+
/// For example, given `foo/bar`, returns `./foo/bar`.
254+
fn anchor(path: &Path) -> Cow<'_, Path> {
255+
match path.components().next() {
256+
None => Cow::Owned(PathBuf::from(".")),
257+
Some(Component::CurDir | Component::ParentDir) => Cow::Borrowed(path),
258+
_ => Cow::Owned(PathBuf::from("./").join(path)),
259+
}
260+
}

crates/uv/tests/export.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ fn non_root() -> Result<()> {
451451
----- stdout -----
452452
# This file was autogenerated by uv via the following command:
453453
# uv export --cache-dir [CACHE_DIR] --package child
454-
-e child
454+
-e ./child
455455
iniconfig==2.0.0 \
456456
--hash=sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3 \
457457
--hash=sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374
@@ -767,7 +767,7 @@ fn no_emit() -> Result<()> {
767767
# This file was autogenerated by uv via the following command:
768768
# uv export --cache-dir [CACHE_DIR] --no-emit-package anyio
769769
-e .
770-
-e child
770+
-e ./child
771771
idna==3.6 \
772772
--hash=sha256:9ecdbbd083b06798ae1e86adcbfe8ab1479cf864e4ee30fe4e46a003d12491ca \
773773
--hash=sha256:c05567e9c24a6b9faaa835c4821bad0590fbb9d5779e7caa6e1cc4978e7eb24f

0 commit comments

Comments
 (0)