diff --git a/Cargo.lock b/Cargo.lock index 7b11daf9..3963bbab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1190,6 +1190,7 @@ dependencies = [ "tempdir", "thiserror", "tokio", + "tracing", "walkdir", ] diff --git a/ethers-solc/src/cache.rs b/ethers-solc/src/cache.rs index 626cc4d4..e6815ac8 100644 --- a/ethers-solc/src/cache.rs +++ b/ethers-solc/src/cache.rs @@ -8,7 +8,7 @@ use crate::{ use serde::{Deserialize, Serialize}; use std::{ collections::BTreeMap, - fs, + fs::{self, File}, path::{Path, PathBuf}, time::{Duration, UNIX_EPOCH}, }; @@ -36,7 +36,7 @@ impl SolFilesCache { /// use ethers_solc::artifacts::Source; /// use ethers_solc::cache::SolFilesCache; /// let files = Source::read_all_from("./sources").unwrap(); - /// let config = SolFilesCache::builder().insert_files(files).unwrap(); + /// let config = SolFilesCache::builder().insert_files(files, None).unwrap(); /// ``` pub fn builder() -> SolFilesCacheBuilder { SolFilesCacheBuilder::default() @@ -200,7 +200,7 @@ impl SolFilesCacheBuilder { self } - pub fn insert_files(self, sources: Sources) -> Result { + pub fn insert_files(self, sources: Sources, dest: Option) -> Result { let format = self.format.unwrap_or_else(|| HH_FORMAT_VERSION.to_string()); let solc_config = self.solc_config.map(Ok).unwrap_or_else(|| SolcConfig::builder().build())?; @@ -234,7 +234,23 @@ impl SolFilesCacheBuilder { files.insert(file, entry); } - Ok(SolFilesCache { format, files }) + let cache = if let Some(ref dest) = dest { + if dest.exists() { + // read the existing cache and extend it by the files that changed + // (if we just wrote to the cache file, we'd overwrite the existing data) + let reader = std::io::BufReader::new(File::open(dest)?); + let mut cache: SolFilesCache = serde_json::from_reader(reader)?; + assert_eq!(cache.format, format); + cache.files.extend(files); + cache + } else { + SolFilesCache { format, files } + } + } else { + SolFilesCache { format, files } + }; + + Ok(cache) } } diff --git a/ethers-solc/src/lib.rs b/ethers-solc/src/lib.rs index ef27c4f4..dd64ac67 100644 --- a/ethers-solc/src/lib.rs +++ b/ethers-solc/src/lib.rs @@ -98,7 +98,7 @@ impl Project { let mut cache = SolFilesCache::builder() .root(&self.paths.root) .solc_config(self.solc_config.clone()) - .insert_files(sources)?; + .insert_files(sources, Some(self.paths.cache.clone()))?; // add the artifacts for each file to the cache entry for (file, artifacts) in artifacts {