#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cache {
pub id: &'static str,
pub name: &'static str,
pub locations: &'static [&'static str],
pub markers: &'static [&'static str],
pub note: &'static str,
}
impl Cache {
#[must_use]
pub fn proven_in(&self, dir: &std::path::Path) -> bool {
let Ok(entries) = std::fs::read_dir(dir) else {
return false;
};
entries.flatten().any(|entry| {
let name = entry.file_name();
self.markers
.iter()
.any(|marker| name.as_os_str() == std::ffi::OsStr::new(marker))
})
}
}
pub static CACHES: &[Cache] = &[
Cache {
id: "cargo-registry",
name: "Cargo registry",
locations: &[".cargo/registry"],
markers: &["CACHEDIR.TAG"],
note: "Crate sources and their index. Re-downloaded on the next build; needs network.",
},
Cache {
id: "cargo-git",
name: "Cargo git checkouts",
locations: &[".cargo/git"],
markers: &["CACHEDIR.TAG"],
note: "Checkouts of git dependencies. Re-cloned on the next build; needs network.",
},
Cache {
id: "uv",
name: "uv",
locations: &[".cache/uv"],
markers: &["CACHEDIR.TAG"],
note: "Wheels and built distributions. `uv cache prune` removes only the stale part.",
},
Cache {
id: "gradle-caches",
name: "Gradle caches",
locations: &[".gradle/caches"],
markers: &["CACHEDIR.TAG"],
note: "Downloaded dependencies and build caches. The next build refetches them.",
},
Cache {
id: "go-build",
name: "Go build cache",
locations: &["Library/Caches/go-build", ".cache/go-build"],
markers: &["trim.txt"],
note: "Compiled package objects. Rebuilt locally, no network needed.",
},
Cache {
id: "go-mod",
name: "Go module cache",
locations: &["go/pkg/mod"],
markers: &["cache"],
note: "Downloaded modules. `go clean -modcache` is the same thing; needs network after.",
},
Cache {
id: "npm",
name: "npm cache",
locations: &[".npm/_cacache"],
markers: &["content-v2", "index-v5"],
note: "Content-addressed package tarballs. Refetched on the next install.",
},
Cache {
id: "pnpm",
name: "pnpm metadata cache",
locations: &["Library/Caches/pnpm", ".cache/pnpm"],
markers: &["lockfile-verified.jsonl"],
note: "Registry metadata. Not the content-addressable store, which holds linked packages.",
},
Cache {
id: "pub",
name: "Dart pub cache",
locations: &[".pub-cache"],
markers: &["hosted-hashes"],
note: "Downloaded packages. `dart pub get` refetches them; needs network.",
},
Cache {
id: "deno",
name: "Deno cache",
locations: &["Library/Caches/deno", ".cache/deno", ".deno"],
markers: &["dep_analysis_cache_v2", "node_analysis_cache_v2"],
note: "Fetched modules and analysis caches. Refetched on the next run; needs network.",
},
Cache {
id: "alef-global",
name: "alef global cache",
locations: &[".cache/alef"],
markers: &["zig-hashes.json"],
note: "Shared snippet build output and toolchain hashes. Rebuilt on the next run.",
},
];
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
#[test]
fn every_cache_declares_a_marker() {
for cache in CACHES {
assert!(!cache.markers.is_empty(), "`{}` has no marker to prove it", cache.id);
for marker in cache.markers {
assert!(!marker.is_empty(), "`{}` has an empty marker", cache.id);
}
}
}
#[test]
fn every_cache_declares_a_location_and_a_note() {
for cache in CACHES {
assert!(!cache.locations.is_empty(), "`{}` has no location", cache.id);
assert!(!cache.note.is_empty(), "`{}` has no note", cache.id);
}
}
#[test]
fn cache_ids_are_unique() {
let mut seen = HashSet::new();
for cache in CACHES {
assert!(seen.insert(cache.id), "`{}` is declared twice", cache.id);
}
}
#[test]
fn no_cache_id_collides_with_an_ecosystem_id() {
for cache in CACHES {
assert!(
crate::catalog::find(cache.id).is_none(),
"`{}` is both a cache id and an ecosystem id, so the report cannot say which",
cache.id
);
}
}
#[test]
fn no_cache_location_escapes_the_home_directory() {
for cache in CACHES {
for location in cache.locations {
let path = std::path::Path::new(location);
assert!(path.is_relative(), "`{}`: `{location}` is absolute", cache.id);
assert!(
path.components()
.all(|component| matches!(component, std::path::Component::Normal(_))),
"`{}`: `{location}` has a `.` or `..` segment",
cache.id
);
}
}
}
#[test]
fn every_cache_location_is_also_skipped_by_location() {
for cache in CACHES {
for location in cache.locations {
assert!(
super::super::CACHE_DIRS
.iter()
.any(|skipped| location == skipped || location.starts_with(&format!("{skipped}/"))),
"`{}`: `{location}` is not under any CACHE_DIRS entry, so a default sweep \
would walk into it",
cache.id
);
}
}
}
}