This commit is contained in:
Harshith VH 2025-12-16 17:20:10 +01:00 committed by GitHub
commit e9baad60b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 156 additions and 13 deletions

View File

@ -816,7 +816,7 @@ enum RequirementSourceWire {
Registry {
#[serde(skip_serializing_if = "VersionSpecifiers::is_empty", default)]
specifier: VersionSpecifiers,
index: Option<DisplaySafeUrl>,
index: Option<String>,
conflict: Option<ConflictItem>,
},
}
@ -829,9 +829,22 @@ impl From<RequirementSource> for RequirementSourceWire {
index,
conflict,
} => {
let index = index.map(|index| index.url.into_url()).map(|mut index| {
index.remove_credentials();
index
let index = index.map(|index| {
// Check if this is a path index that was originally relative
if let IndexUrl::Path(path_url) = &index.url {
if let Some(given) = path_url.given() {
let given_path = Path::new(given);
// For relative paths, preserve the original string directly
if given_path.is_relative() && !given.starts_with("file://") {
return given.to_string();
}
}
}
// Default behavior for absolute paths and non-path URLs
let mut url = index.url.into_url();
url.remove_credentials();
url.to_string()
});
Self::Registry {
specifier,
@ -943,12 +956,22 @@ impl TryFrom<RequirementSourceWire> for RequirementSource {
specifier,
index,
conflict,
} => Ok(Self::Registry {
specifier,
index: index
.map(|index| IndexMetadata::from(IndexUrl::from(VerbatimUrl::from_url(index)))),
conflict,
}),
} => {
let index = if let Some(index_str) = index {
let verbatim_url = VerbatimUrl::from_url_or_path(&index_str, None)?;
Some(IndexMetadata::from(IndexUrl::from(
verbatim_url.with_given(&index_str),
)))
} else {
None
};
Ok(Self::Registry {
specifier,
index,
conflict,
})
}
RequirementSourceWire::Git { git } => {
let mut repository = DisplaySafeUrl::parse(&git)?;

View File

@ -3639,10 +3639,24 @@ impl Source {
let path = url
.to_file_path()
.map_err(|()| LockErrorKind::UrlToPath { url: url.to_url() })?;
// Try to compute a relative path from the project root. If that fails (e.g., paths
// on different drives), preserve the original relative path from the user's config
// via `url.given()` if available, since flat indices should remain relative.
// Otherwise, fall back to an absolute path.
let path = relative_to(&path, root)
.or_else(|_| std::path::absolute(&path))
.map_err(LockErrorKind::IndexRelativePath)?;
let source = RegistrySource::Path(path.into_boxed_path());
.or_else(|_| {
if let Some(given) = url.given() {
let given_path = Path::new(given);
if given_path.is_relative() {
return Ok(uv_fs::normalize_path(given_path).into_owned());
}
}
std::path::absolute(&path)
})
.map_err(LockErrorKind::IndexRelativePath)?
.into_boxed_path();
let source = RegistrySource::Path(path);
Ok(Self::Registry(source))
}
}

View File

@ -11465,6 +11465,112 @@ fn lock_find_links_relative_url() -> Result<()> {
Ok(())
}
/// Ensure that flat indices preserve relative paths in lockfiles (issue with flat index portability).
#[test]
fn lock_find_links_relative_path_preserved() -> Result<()> {
let context = TestContext::new("3.12");
// Populate the `--find-links` entries in a subdirectory.
fs_err::create_dir_all(context.temp_dir.join("local_packages"))?;
for entry in fs_err::read_dir(context.workspace_root.join("scripts/links"))? {
let entry = entry?;
let path = entry.path();
if path
.file_name()
.and_then(|file_name| file_name.to_str())
.is_some_and(|file_name| file_name.starts_with("tqdm-"))
{
let dest = context
.temp_dir
.join("local_packages")
.join(path.file_name().unwrap());
fs_err::copy(&path, &dest)?;
}
}
let workspace = context.temp_dir.child("workspace");
let pyproject_toml = workspace.child("pyproject.toml");
pyproject_toml.write_str(
r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["tqdm"]
[[tool.uv.index]]
name = "local"
format = "flat"
url = "../local_packages"
explicit = true
[tool.uv.sources]
tqdm = { index = "local" }
"#,
)?;
uv_snapshot!(context.filters(), context.lock().current_dir(&workspace), @r"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
Resolved 2 packages in [TIME]
");
let lock = fs_err::read_to_string(workspace.join("uv.lock"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
lock, @r#"
version = 1
revision = 3
requires-python = ">=3.12"
[options]
exclude-newer = "2024-03-25T00:00:00Z"
[[package]]
name = "project"
version = "0.1.0"
source = { virtual = "." }
dependencies = [
{ name = "tqdm" },
]
[package.metadata]
requires-dist = [{ name = "tqdm", index = "../local_packages" }]
[[package]]
name = "tqdm"
version = "1000.0.0"
source = { registry = "../local_packages" }
wheels = [
{ path = "tqdm-1000.0.0-py3-none-any.whl" },
]
"#
);
});
// Re-run with `--locked` to ensure the lockfile is valid.
uv_snapshot!(context.filters(), context.lock().arg("--locked").current_dir(&workspace), @r"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
Resolved 2 packages in [TIME]
");
Ok(())
}
/// Lock a local source distribution via `--find-links`.
#[test]
fn lock_find_links_local_sdist() -> Result<()> {