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

Commit 119d12a

Browse files
committed
fix(solc): do not overwrite existing cache
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
1 parent e1476a1 commit 119d12a

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Diff for: ethers-solc/src/cache.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
use serde::{Deserialize, Serialize};
99
use std::{
1010
collections::BTreeMap,
11-
fs,
11+
fs::{self, File},
1212
path::{Path, PathBuf},
1313
time::{Duration, UNIX_EPOCH},
1414
};
@@ -36,7 +36,7 @@ impl SolFilesCache {
3636
/// use ethers_solc::artifacts::Source;
3737
/// use ethers_solc::cache::SolFilesCache;
3838
/// let files = Source::read_all_from("./sources").unwrap();
39-
/// let config = SolFilesCache::builder().insert_files(files).unwrap();
39+
/// let config = SolFilesCache::builder().insert_files(files, None).unwrap();
4040
/// ```
4141
pub fn builder() -> SolFilesCacheBuilder {
4242
SolFilesCacheBuilder::default()
@@ -200,7 +200,11 @@ impl SolFilesCacheBuilder {
200200
self
201201
}
202202

203-
pub fn insert_files(self, sources: Sources) -> Result<SolFilesCache> {
203+
pub fn insert_files(
204+
self,
205+
sources: Sources,
206+
dest: Option<impl AsRef<Path>>,
207+
) -> Result<SolFilesCache> {
204208
let format = self.format.unwrap_or_else(|| HH_FORMAT_VERSION.to_string());
205209
let solc_config =
206210
self.solc_config.map(Ok).unwrap_or_else(|| SolcConfig::builder().build())?;
@@ -234,7 +238,23 @@ impl SolFilesCacheBuilder {
234238
files.insert(file, entry);
235239
}
236240

237-
Ok(SolFilesCache { format, files })
241+
let cache = if let Some(ref dest) = dest {
242+
if dest.as_ref().exists() {
243+
// read the existing cache and extend it by the files that changed
244+
// (if we just wrote to the cache file, we'd overwrite the existing data)
245+
let reader = std::io::BufReader::new(File::open(dest)?);
246+
let mut cache: SolFilesCache = serde_json::from_reader(reader)?;
247+
assert_eq!(cache.format, format);
248+
cache.files.extend(files);
249+
cache
250+
} else {
251+
SolFilesCache { format, files }
252+
}
253+
} else {
254+
SolFilesCache { format, files }
255+
};
256+
257+
Ok(cache)
238258
}
239259
}
240260

Diff for: ethers-solc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<Artifacts: ArtifactOutput> Project<Artifacts> {
9898
let mut cache = SolFilesCache::builder()
9999
.root(&self.paths.root)
100100
.solc_config(self.solc_config.clone())
101-
.insert_files(sources)?;
101+
.insert_files(sources, Some(&self.paths.cache))?;
102102

103103
// add the artifacts for each file to the cache entry
104104
for (file, artifacts) in artifacts {

0 commit comments

Comments
 (0)