Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions crates/uv-resolver/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3566,9 +3566,23 @@ impl Source {
.to_file_path()
.map_err(|()| LockErrorKind::UrlToPath { url: url.to_url() })?;
let path = relative_to(&path, root)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some documentation around why we need both the relative_to and reading the relative path from given?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added docs! Hope this helps future readers

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I still don't follow. When they are on different drives (I assume you mean Windows drive letters, not Unix mounts?), how can the user config use a relative path?

Can you write a test case for this? The current test case passes with only the requirement.rs changes applied.

.or_else(|_| std::path::absolute(&path))
.map_err(LockErrorKind::IndexRelativePath)?;
let source = RegistrySource::Path(path.into_boxed_path());
.or_else(|_| {
// If relative_to fails, check if the user originally provided a relative path
// that we should preserve (for flat indices)
if let Some(given) = url.given() {
let given_path = Path::new(given);
if given_path.is_relative() {
// Keep the original relative path for flat indices
return Ok(uv_fs::normalize_path(given_path).into_owned());
}
}
// Default fallback behavior
std::path::absolute(&path)
})
.map_err(LockErrorKind::IndexRelativePath)?
.into_boxed_path();

let source = RegistrySource::Path(path);
Ok(Self::Registry(source))
}
}
Expand Down
106 changes: 106 additions & 0 deletions crates/uv/tests/it/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11180,6 +11180,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")).unwrap();
Comment thread
harshithvh marked this conversation as resolved.
Outdated

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 = "file://[TEMP_DIR]/local_packages" }]
Comment thread
harshithvh marked this conversation as resolved.
Outdated

[[package]]
name = "tqdm"
version = "1000.0.0"
source = { registry = "../local_packages" }
Comment thread
harshithvh marked this conversation as resolved.
wheels = [
{ path = "tqdm-1000.0.0-py3-none-any.whl" },
]
"#
);
});

// Re-run with `--locked` to ensure the lockfile is valid.
Comment thread
harshithvh marked this conversation as resolved.
Outdated
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<()> {
Expand Down
Loading