From 909e64d1c3474eb82d0774b0584c78f7f0e49324 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Sat, 27 Jul 2024 17:22:06 +0800 Subject: [PATCH] fix: remove outdated build infos (#177) Closes https://github.com/foundry-rs/compilers/issues/172 --- crates/compilers/src/cache.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/compilers/src/cache.rs b/crates/compilers/src/cache.rs index f6aa5b1c..30a4c9f3 100644 --- a/crates/compilers/src/cache.rs +++ b/crates/compilers/src/cache.rs @@ -150,6 +150,27 @@ impl CompilerCache { Ok(()) } + /// Removes build infos which don't have any artifacts linked to them. + pub fn remove_outdated_builds(&mut self) { + let mut outdated = Vec::new(); + for build_id in &self.builds { + if !self + .entries() + .flat_map(|e| e.artifacts.values()) + .flat_map(|a| a.values()) + .any(|a| a.build_id == *build_id) + { + outdated.push(build_id.to_owned()); + } + } + + for build_id in outdated { + self.builds.remove(&build_id); + let path = self.paths.build_infos.join(build_id).with_extension("json"); + let _ = std::fs::remove_file(path); + } + } + /// Sets the `CacheEntry`'s file paths to `root` adjoined to `self.file`. pub fn join_entries(&mut self, root: &Path) -> &mut Self { self.files = std::mem::take(&mut self.files) @@ -1035,6 +1056,7 @@ impl<'a, T: ArtifactOutput, C: Compiler> ArtifactsCache<'a, T, C> { // write to disk if write_to_disk { + cache.remove_outdated_builds(); // make all `CacheEntry` paths relative to the project root and all artifact // paths relative to the artifact's directory cache