From 9f7d9da449cab4119d07da3ecd2e3b868e092afd Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 16 Sep 2024 19:18:20 -0400 Subject: [PATCH] Prune unzipped source distributions in `uv cache prune --ci` (#7446) ## Summary It's very unlikely that retaining these is beneficial, since you tend to partition the cache by platform anyway. Closes https://github.com/astral-sh/uv/issues/7394. --- crates/uv-cache/src/lib.rs | 27 +++++++++++++++++++++++---- crates/uv/tests/cache_prune.rs | 2 +- docs/concepts/cache.md | 6 +++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/crates/uv-cache/src/lib.rs b/crates/uv-cache/src/lib.rs index 0ecf2f39e018..e97db0897c2a 100644 --- a/crates/uv-cache/src/lib.rs +++ b/crates/uv-cache/src/lib.rs @@ -447,12 +447,31 @@ impl Cache { Err(err) => return Err(err), } - // Remove any unzipped wheels (i.e., symlinks) from the built wheels cache. for entry in walkdir::WalkDir::new(self.bucket(CacheBucket::SourceDistributions)) { let entry = entry?; - if entry.file_type().is_symlink() { - debug!("Removing unzipped wheel entry: {}", entry.path().display()); - summary += rm_rf(entry.path())?; + + // If the directory contains a `metadata.msgpack`, then it's a built wheel revision. + if !entry.file_type().is_dir() { + continue; + } + + if !entry.path().join("metadata.msgpack").exists() { + continue; + } + + // Remove any symlinks and directories in the revision. The symlinks represent + // unzipped wheels, and the directories represent the source distribution archives. + for entry in fs_err::read_dir(entry.path())? { + let entry = entry?; + let path = entry.path(); + + if path.is_dir() { + debug!("Removing unzipped built wheel entry: {}", path.display()); + summary += rm_rf(path)?; + } else if path.is_symlink() { + debug!("Removing unzipped built wheel entry: {}", path.display()); + summary += rm_rf(path)?; + } } } } diff --git a/crates/uv/tests/cache_prune.rs b/crates/uv/tests/cache_prune.rs index 2c3c436a7a0e..227150c49265 100644 --- a/crates/uv/tests/cache_prune.rs +++ b/crates/uv/tests/cache_prune.rs @@ -204,7 +204,7 @@ fn prune_unzipped() -> Result<()> { ----- stderr ----- Pruning cache at: [CACHE_DIR]/ - Removed 162 files ([SIZE]) + Removed 170 files ([SIZE]) "###); // Reinstalling the source distribution should not require re-downloading the source diff --git a/docs/concepts/cache.md b/docs/concepts/cache.md index 04caf9889d35..7f5c9441b00f 100644 --- a/docs/concepts/cache.md +++ b/docs/concepts/cache.md @@ -115,9 +115,9 @@ from source tends to be worthwhile, since the wheel building process can be expe for extension modules. To support this caching strategy, uv provides a `uv cache prune --ci` command, which removes all -pre-built wheels from the cache but retains any wheels that were built from source. We recommend -running `uv cache prune --ci` at the end of your continuous integration job to ensure maximum cache -efficiency. For an example, see the +pre-built wheels and unzipped source distributions from the cache, but retains any wheels that were +built from source. We recommend running `uv cache prune --ci` at the end of your continuous +integration job to ensure maximum cache efficiency. For an example, see the [GitHub integration guide](../guides/integration/github.md#caching). ## Cache directory