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

fix(solc): do not overwrite existing cache #629

Merged
merged 1 commit into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

24 changes: 20 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,7 @@ 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 +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)
}
}

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