From 41f8e295a05d9b25bdd9457209ae1d26b4e01b1a Mon Sep 17 00:00:00 2001 From: korboismoe <82175143+korboismoe@users.noreply.github.com> Date: Sun, 28 Nov 2021 12:33:24 +0000 Subject: [PATCH] fix: remove changed artifacts from the cache (#630) Duplicate artifacts will be returned if there is a changed artifact that was already present in the cache --- ethers-solc/src/cache.rs | 4 +++ ethers-solc/src/lib.rs | 1 + ethers-solc/test-data/cache-sample/Dapp.sol | 7 ++++++ ethers-solc/tests/project.rs | 27 +++++++++++++++++++-- 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 ethers-solc/test-data/cache-sample/Dapp.sol diff --git a/ethers-solc/src/cache.rs b/ethers-solc/src/cache.rs index e6815ac8..20ef24bd 100644 --- a/ethers-solc/src/cache.rs +++ b/ethers-solc/src/cache.rs @@ -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>( diff --git a/ethers-solc/src/lib.rs b/ethers-solc/src/lib.rs index dd64ac67..e7cd94f1 100644 --- a/ethers-solc/src/lib.rs +++ b/ethers-solc/src/lib.rs @@ -251,6 +251,7 @@ impl Project { Some(&self.solc_config), &self.paths.artifacts, ); + cache.remove_changed_files(&changed_files); let cached_artifacts = if self.paths.artifacts.exists() { cache.read_artifacts::(&self.paths.artifacts)? diff --git a/ethers-solc/test-data/cache-sample/Dapp.sol b/ethers-solc/test-data/cache-sample/Dapp.sol new file mode 100644 index 00000000..762db2c3 --- /dev/null +++ b/ethers-solc/test-data/cache-sample/Dapp.sol @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity >=0.6.6; + +contract Dapp { + + function modified() public {} +} diff --git a/ethers-solc/tests/project.rs b/ethers-solc/tests/project.rs index 6caebe3e..3157b66a 100644 --- a/ethers-solc/tests/project.rs +++ b/ethers-solc/tests/project.rs @@ -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) @@ -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![ + 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![ + 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();