Skip to content

Commit

Permalink
Allow relative
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 25, 2024
1 parent ea636b1 commit 1d5808c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
6 changes: 4 additions & 2 deletions crates/pypi-types/src/requirement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ impl RequirementSource {
ext,
url,
} => Ok(Self::Path {
install_path: relative_to(&install_path, path)?,
install_path: relative_to(&install_path, path)
.or_else(|_| std::path::absolute(install_path))?,
ext,
url,
}),
Expand All @@ -516,7 +517,8 @@ impl RequirementSource {
editable,
url,
} => Ok(Self::Directory {
install_path: relative_to(&install_path, path)?,
install_path: relative_to(&install_path, path)
.or_else(|_| std::path::absolute(install_path))?,
editable,
url,
}),
Expand Down
3 changes: 3 additions & 0 deletions crates/uv-fs/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ pub fn canonicalize_executable(path: impl AsRef<Path>) -> std::io::Result<PathBu
/// `lib/python/site-packages/foo/__init__.py` and `lib/python/site-packages` -> `foo/__init__.py`
/// `lib/marker.txt` and `lib/python/site-packages` -> `../../marker.txt`
/// `bin/foo_launcher` and `lib/python/site-packages` -> `../../../bin/foo_launcher`
///
/// Returns `Err` if there is no relative path between `path` and `base` (for example, if the paths
/// are on different drives on Windows).
pub fn relative_to(
path: impl AsRef<Path>,
base: impl AsRef<Path>,
Expand Down
27 changes: 17 additions & 10 deletions crates/uv-resolver/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,9 @@ impl Lock {
IndexUrl::Pypi(_) | IndexUrl::Url(_) => None,
IndexUrl::Path(index_url) => {
let path = index_url.to_file_path().ok()?;
let path = relative_to(&path, workspace.install_path()).ok()?;
let path = relative_to(&path, workspace.install_path())
.or_else(|_| std::path::absolute(path))
.ok()?;
Some(path)
}
})
Expand All @@ -802,7 +804,9 @@ impl Lock {
FlatIndexLocation::Url(_) => None,
FlatIndexLocation::Path(index_url) => {
let path = index_url.to_file_path().ok()?;
let path = relative_to(&path, workspace.install_path()).ok()?;
let path = relative_to(&path, workspace.install_path())
.or_else(|_| std::path::absolute(path))
.ok()?;
Some(path)
}
}),
Expand Down Expand Up @@ -2026,14 +2030,15 @@ impl Source {

fn from_path_built_dist(path_dist: &PathBuiltDist, root: &Path) -> Result<Source, LockError> {
let path = relative_to(&path_dist.install_path, root)
.or_else(|_| std::path::absolute(&path_dist.install_path))
.map_err(LockErrorKind::DistributionRelativePath)?;
Ok(Source::Path(path))
}

fn from_path_source_dist(path_dist: &PathSourceDist, root: &Path) -> Result<Source, LockError> {
let path = relative_to(&path_dist.install_path, root)
.or_else(|_| std::path::absolute(&path_dist.install_path))
.map_err(LockErrorKind::DistributionRelativePath)?;

Ok(Source::Path(path))
}

Expand All @@ -2042,6 +2047,7 @@ impl Source {
root: &Path,
) -> Result<Source, LockError> {
let path = relative_to(&directory_dist.install_path, root)
.or_else(|_| std::path::absolute(&directory_dist.install_path))
.map_err(LockErrorKind::DistributionRelativePath)?;
if directory_dist.editable {
Ok(Source::Editable(path))
Expand All @@ -2059,11 +2065,10 @@ impl Source {
Ok(Source::Registry(source))
}
IndexUrl::Path(url) => {
let path = relative_to(
url.to_file_path().map_err(|()| LockErrorKind::UrlToPath)?,
root,
)
.map_err(LockErrorKind::IndexRelativePath)?;
let path = url.to_file_path().map_err(|()| LockErrorKind::UrlToPath)?;
let path = relative_to(&path, root)
.or_else(|_| std::path::absolute(&path))
.map_err(LockErrorKind::IndexRelativePath)?;
let source = RegistrySource::Path(path);
Ok(Source::Registry(source))
}
Expand Down Expand Up @@ -2520,7 +2525,8 @@ impl SourceDist {
.map_err(LockErrorKind::InvalidFileUrl)?
.to_file_path()
.map_err(|()| LockErrorKind::UrlToPath)?;
let path = relative_to(reg_dist_path, index_path)
let path = relative_to(&reg_dist_path, index_path)
.or_else(|_| std::path::absolute(&reg_dist_path))
.map_err(LockErrorKind::DistributionRelativePath)?;
let hash = reg_dist.file.hashes.iter().max().cloned().map(Hash::from);
let size = reg_dist.file.size;
Expand Down Expand Up @@ -2798,7 +2804,8 @@ impl Wheel {
.map_err(LockErrorKind::InvalidFileUrl)?
.to_file_path()
.map_err(|()| LockErrorKind::UrlToPath)?;
let path = relative_to(wheel_path, index_path)
let path = relative_to(&wheel_path, index_path)
.or_else(|_| std::path::absolute(&wheel_path))
.map_err(LockErrorKind::DistributionRelativePath)?;
Ok(Wheel {
url: WheelWireSource::Path { path },
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,21 +724,21 @@ fn sync_relative_wheel() -> Result<()> {
[[package]]
name = "ok"
version = "1.0.0"
source = { path = "wheels/ok-1.0.0-py3-none-any.whl" }
source = { path = "[TEMP_DIR]/wheels/ok-1.0.0-py3-none-any.whl" }
wheels = [
{ filename = "ok-1.0.0-py3-none-any.whl", hash = "sha256:79f0b33e6ce1e09eaa1784c8eee275dfe84d215d9c65c652f07c18e85fdaac5f" },
]
[[package]]
name = "relative-wheel"
version = "0.1.0"
source = { editable = "." }
source = { editable = "[TEMP_DIR]/" }
dependencies = [
{ name = "ok" },
]
[package.metadata]
requires-dist = [{ name = "ok", path = "wheels/ok-1.0.0-py3-none-any.whl" }]
requires-dist = [{ name = "ok", path = "[TEMP_DIR]/wheels/ok-1.0.0-py3-none-any.whl" }]
"###
);
}
Expand Down

0 comments on commit 1d5808c

Please sign in to comment.