Normalize paths when lowering Git dependencies (#9595)

## Summary

Discovered while working on https://github.com/astral-sh/uv/issues/9516.
In the linked repo, the root uses a `../dependency` path for the
workspace member, which we weren't normalizing.
This commit is contained in:
Charlie Marsh 2024-12-03 00:41:26 -05:00 committed by GitHub
parent 90d8105117
commit ae6f66effc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 102 additions and 7 deletions

View File

@ -315,11 +315,12 @@ impl LoweredRequirement {
})?; })?;
let source = if let Some(git_member) = &git_member { let source = if let Some(git_member) = &git_member {
// If the workspace comes from a git dependency, all workspace // If the workspace comes from a Git dependency, all workspace
// members need to be git deps, too. // members need to be Git dependencies, too.
let subdirectory = let subdirectory = uv_fs::normalize_path(
uv_fs::relative_to(member.root(), git_member.fetch_root) &uv_fs::relative_to(member.root(), git_member.fetch_root)
.expect("Workspace member must be relative"); .expect("Workspace member must be relative"),
);
RequirementSource::Git { RequirementSource::Git {
repository: git_member.git_source.git.repository().clone(), repository: git_member.git_source.git.repository().clone(),
reference: git_member.git_source.git.reference().clone(), reference: git_member.git_source.git.reference().clone(),

View File

@ -191,11 +191,14 @@ impl Urls {
fn same_resource(a: &ParsedUrl, b: &ParsedUrl, git: &GitResolver) -> bool { fn same_resource(a: &ParsedUrl, b: &ParsedUrl, git: &GitResolver) -> bool {
match (a, b) { match (a, b) {
(ParsedUrl::Archive(a), ParsedUrl::Archive(b)) => { (ParsedUrl::Archive(a), ParsedUrl::Archive(b)) => {
a.subdirectory == b.subdirectory a.subdirectory.as_deref().map(uv_fs::normalize_path)
== b.subdirectory.as_deref().map(uv_fs::normalize_path)
&& CanonicalUrl::new(&a.url) == CanonicalUrl::new(&b.url) && CanonicalUrl::new(&a.url) == CanonicalUrl::new(&b.url)
} }
(ParsedUrl::Git(a), ParsedUrl::Git(b)) => { (ParsedUrl::Git(a), ParsedUrl::Git(b)) => {
a.subdirectory == b.subdirectory && git.same_ref(&a.url, &b.url) a.subdirectory.as_deref().map(uv_fs::normalize_path)
== b.subdirectory.as_deref().map(uv_fs::normalize_path)
&& git.same_ref(&a.url, &b.url)
} }
(ParsedUrl::Path(a), ParsedUrl::Path(b)) => { (ParsedUrl::Path(a), ParsedUrl::Path(b)) => {
a.install_path == b.install_path a.install_path == b.install_path

View File

@ -5319,6 +5319,97 @@ fn sync_git_repeated_member_dynamic_metadata() -> Result<()> {
Ok(()) Ok(())
} }
/// See: <https://github.com/astral-sh/uv/issues/8887>
#[test]
fn sync_git_repeated_member_backwards_path() -> Result<()> {
let context = TestContext::new("3.13");
let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(
r#"
[project]
name = "foo"
version = "0.1.0"
requires-python = ">=3.13"
dependencies = ["package", "dependency"]
[tool.uv.sources]
package = { git = "https://github.com/astral-sh/uv-backwards-path-test", subdirectory = "root" }
dependency = { git = "https://github.com/astral-sh/uv-backwards-path-test", subdirectory = "dependency" }
"#,
)?;
uv_snapshot!(context.filters(), context.lock(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 3 packages in [TIME]
"###);
let lock = context.read("uv.lock");
insta::with_settings!(
{
filters => context.filters(),
},
{
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.13"
[options]
exclude-newer = "2024-03-25T00:00:00Z"
[[package]]
name = "dependency"
version = "0.1.0"
source = { git = "https://github.com/astral-sh/uv-backwards-path-test?subdirectory=dependency#4bcc7fcd2e548c2ab7ba6b97b1c4e3ababccc7a9" }
[[package]]
name = "foo"
version = "0.1.0"
source = { virtual = "." }
dependencies = [
{ name = "dependency" },
{ name = "package" },
]
[package.metadata]
requires-dist = [
{ name = "dependency", git = "https://github.com/astral-sh/uv-backwards-path-test?subdirectory=dependency" },
{ name = "package", git = "https://github.com/astral-sh/uv-backwards-path-test?subdirectory=root" },
]
[[package]]
name = "package"
version = "0.1.0"
source = { git = "https://github.com/astral-sh/uv-backwards-path-test?subdirectory=root#4bcc7fcd2e548c2ab7ba6b97b1c4e3ababccc7a9" }
dependencies = [
{ name = "dependency" },
]
"###
);
}
);
uv_snapshot!(context.filters(), context.sync(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 3 packages in [TIME]
Prepared 2 packages in [TIME]
Installed 2 packages in [TIME]
+ dependency==0.1.0 (from git+https://github.com/astral-sh/uv-backwards-path-test@4bcc7fcd2e548c2ab7ba6b97b1c4e3ababccc7a9#subdirectory=dependency)
+ package==0.1.0 (from git+https://github.com/astral-sh/uv-backwards-path-test@4bcc7fcd2e548c2ab7ba6b97b1c4e3ababccc7a9#subdirectory=root)
"###);
Ok(())
}
/// The project itself is marked as an editable dependency, but under the wrong name. The project /// The project itself is marked as an editable dependency, but under the wrong name. The project
/// is a package. /// is a package.
#[test] #[test]