Skip to content

Commit

Permalink
Make cache robust to removed archives
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 20, 2024
1 parent f10ccc4 commit 614cb17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
7 changes: 6 additions & 1 deletion crates/uv-distribution/src/archive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use distribution_types::Hashed;
use pypi_types::HashDigest;
use uv_cache::ArchiveId;
use uv_cache::{ArchiveId, Cache};

/// An archive (unzipped wheel) that exists in the local cache.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
Expand All @@ -16,6 +16,11 @@ impl Archive {
pub(crate) fn new(id: ArchiveId, hashes: Vec<HashDigest>) -> Self {
Self { id, hashes }
}

/// Returns `true` if the archive exists in the cache.
pub(crate) fn exists(&self, cache: &Cache) -> bool {
cache.archive(&self.id).exists()
}
}

impl Hashed for Archive {
Expand Down
18 changes: 13 additions & 5 deletions crates/uv-distribution/src/distribution_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,12 @@ impl<'a, Context: BuildContext> DistributionDatabase<'a, Context> {
CachedClientError::Client(err) => Error::Client(err),
})?;

// If the archive is missing the required hashes, force a refresh.
let archive = if archive.has_digests(hashes) {
// If the archive is missing the required hashes, or has since been removed, force a refresh.
let archive = Some(archive)
.filter(|archive| archive.has_digests(hashes))
.filter(|archive| archive.exists(self.build_context.cache()));

let archive = if let Some(archive) = archive {
archive
} else {
self.client
Expand Down Expand Up @@ -746,12 +750,16 @@ impl<'a, Context: BuildContext> DistributionDatabase<'a, Context> {
CachedClientError::Client(err) => Error::Client(err),
})?;

// If the archive is missing the required hashes, force a refresh.
let archive = if archive.has_digests(hashes) {
// If the archive is missing the required hashes, or has since been removed, force a refresh.
let archive = Some(archive)
.filter(|archive| archive.has_digests(hashes))
.filter(|archive| archive.exists(self.build_context.cache()));

let archive = if let Some(archive) = archive {
archive
} else {
self.client
.managed(|client| async move {
.managed(|client| async {
client
.cached_client()
.skip_cache(self.request(url)?, &http_entry, download)
Expand Down

0 comments on commit 614cb17

Please sign in to comment.