From 134810c5478d0623db663dcd47f6ff89435f129c Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 8 Apr 2024 14:58:33 -0400 Subject: [PATCH] Respect cached local `--find-links` in install plan (#2907) ## Summary I think this is kind of just an oversight. If a wheel is available via `--find-links`, and the index is "local", we never find it in the cache. ## Test Plan `cargo test` --- .../src/distribution_database.rs | 3 +- .../src/index/registry_wheel_index.rs | 5 +- crates/uv/tests/pip_sync.rs | 48 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/crates/uv-distribution/src/distribution_database.rs b/crates/uv-distribution/src/distribution_database.rs index e5c75187a..5c63f5428 100644 --- a/crates/uv-distribution/src/distribution_database.rs +++ b/crates/uv-distribution/src/distribution_database.rs @@ -150,10 +150,9 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context> Url::parse(url).map_err(|err| Error::Url(url.clone(), err))? } FileLocation::Path(path) => { - let url = Url::from_file_path(path).expect("path is absolute"); let cache_entry = self.build_context.cache().entry( CacheBucket::Wheels, - WheelCache::Url(&url).wheel_dir(wheel.name().as_ref()), + WheelCache::Index(&wheel.index).wheel_dir(wheel.name().as_ref()), wheel.filename.stem(), ); return self diff --git a/crates/uv-distribution/src/index/registry_wheel_index.rs b/crates/uv-distribution/src/index/registry_wheel_index.rs index 1aa5ab017..8342cdb8a 100644 --- a/crates/uv-distribution/src/index/registry_wheel_index.rs +++ b/crates/uv-distribution/src/index/registry_wheel_index.rs @@ -83,7 +83,10 @@ impl<'a> RegistryWheelIndex<'a> { let flat_index_urls: Vec = index_locations .flat_index() .filter_map(|flat_index| match flat_index { - FlatIndexLocation::Path(_) => None, + FlatIndexLocation::Path(path) => { + let path = fs_err::canonicalize(path).ok()?; + Some(IndexUrl::Url(VerbatimUrl::from_path(path))) + } FlatIndexLocation::Url(url) => { Some(IndexUrl::Url(VerbatimUrl::unknown(url.clone()))) } diff --git a/crates/uv/tests/pip_sync.rs b/crates/uv/tests/pip_sync.rs index 3f15bb13d..79088af57 100644 --- a/crates/uv/tests/pip_sync.rs +++ b/crates/uv/tests/pip_sync.rs @@ -2575,6 +2575,54 @@ fn find_links_offline_no_match() -> Result<()> { Ok(()) } +/// Sync using `--find-links` with a local directory. Ensure that cached wheels are reused. +#[test] +fn find_links_cache() -> Result<()> { + let context = TestContext::new("3.12"); + + let requirements_txt = context.temp_dir.child("requirements.txt"); + requirements_txt.write_str(indoc! {r" + tqdm + "})?; + + // Install `tqdm`. + uv_snapshot!(context.filters(), command(&context) + .arg("requirements.txt") + .arg("--find-links") + .arg(context.workspace_root.join("scripts/links/")), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 1 package in [TIME] + Downloaded 1 package in [TIME] + Installed 1 package in [TIME] + + tqdm==1000.0.0 + "### + ); + + // Reinstall `tqdm` with `--reinstall`. Ensure that the wheel is reused. + uv_snapshot!(context.filters(), command(&context) + .arg("requirements.txt") + .arg("--reinstall") + .arg("--find-links") + .arg(context.workspace_root.join("scripts/links/")), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Uninstalled 1 package in [TIME] + Installed 1 package in [TIME] + - tqdm==1000.0.0 + + tqdm==1000.0.0 + "### + ); + + Ok(()) +} + /// Install without network access via the `--offline` flag. #[test] fn offline() -> Result<()> {