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

fix(solc): remove changed artifacts from the cache #630

Merged
merged 1 commit into from Nov 28, 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
4 changes: 4 additions & 0 deletions ethers-solc/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl SolFilesCache {
self.files.retain(|file, _| Path::new(file).exists())
}

pub fn remove_changed_files(&mut self, changed_files: &Sources) {
self.files.retain(|file, _| !changed_files.contains_key(file))
}

/// Returns only the files that were changed from the provided sources, to save time
/// when compiling.
pub fn get_changed_files<'a>(
Expand Down
1 change: 1 addition & 0 deletions ethers-solc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl<Artifacts: ArtifactOutput> Project<Artifacts> {
Some(&self.solc_config),
&self.paths.artifacts,
);
cache.remove_changed_files(&changed_files);

let cached_artifacts = if self.paths.artifacts.exists() {
cache.read_artifacts::<Artifacts>(&self.paths.artifacts)?
Expand Down
7 changes: 7 additions & 0 deletions ethers-solc/test-data/cache-sample/Dapp.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.6.6;

contract Dapp {

function modified() public {}
}
27 changes: 25 additions & 2 deletions ethers-solc/tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn can_compile_dapp_sample_with_cache() {

let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let orig_root = manifest_dir.join("test-data/dapp-sample");
let new_file = manifest_dir.join("test-data/cache-sample/NewContract.sol");
let cache_testdata_dir = manifest_dir.join("test-data/cache-sample/");
copy_dir_all(orig_root, &tmp_dir).unwrap();
let paths = ProjectPathsConfig::builder()
.cache(cache)
Expand Down Expand Up @@ -117,11 +117,34 @@ fn can_compile_dapp_sample_with_cache() {
assert!(!compiled.is_unchanged());

// new file is compiled even with partial cache
std::fs::copy(new_file, root.join("src/NewContract.sol")).unwrap();
std::fs::copy(cache_testdata_dir.join("NewContract.sol"), root.join("src/NewContract.sol"))
.unwrap();
let compiled = project.compile().unwrap();
assert!(compiled.find("Dapp").is_some());
assert!(compiled.find("NewContract").is_some());
assert!(!compiled.is_unchanged());
assert_eq!(
compiled.into_artifacts().map(|(name, _)| name).collect::<Vec<_>>(),
vec![
r#""Dapp.json":Dapp"#,
r#""DappTest.json":DappTest"#,
r#""DSTest.json":DSTest"#,
"NewContract"
]
);

// old cached artifact is not taken from the cache
std::fs::copy(cache_testdata_dir.join("Dapp.sol"), root.join("src/Dapp.sol")).unwrap();
let compiled = project.compile().unwrap();
assert_eq!(
compiled.into_artifacts().map(|(name, _)| name).collect::<Vec<_>>(),
vec![
r#""DappTest.json":DappTest"#,
r#""NewContract.json":NewContract"#,
r#""DSTest.json":DSTest"#,
"Dapp"
]
);

// deleted artifact is not taken from the cache
std::fs::remove_file(&project.paths.sources.join("Dapp.sol")).unwrap();
Expand Down