Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix(solc): do not overwrite existing cache
Browse files Browse the repository at this point in the history
previously if 1 file changed, it'd overwrite the existing cache on disk with just
that 1 changed file. this change reads the cache from disk and merges it with the new checksums
  • Loading branch information
gakonst committed Nov 26, 2021
1 parent e1476a1 commit 84dcd84
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions ethers-solc/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -200,7 +200,11 @@ impl SolFilesCacheBuilder {
self
}

pub fn insert_files(self, sources: Sources) -> Result<SolFilesCache> {
pub fn insert_files(
self,
sources: Sources,
dest: Option<PathBuf>,
) -> Result<SolFilesCache> {
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())?;
Expand Down Expand Up @@ -234,7 +238,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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ethers-solc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<Artifacts: ArtifactOutput> Project<Artifacts> {
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 {
Expand Down

0 comments on commit 84dcd84

Please sign in to comment.