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
This commit is contained in:
korboismoe 2021-11-28 12:33:24 +00:00 committed by GitHub
parent 9b6cc37ca0
commit 41f8e295a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 2 deletions

View File

@ -58,6 +58,10 @@ impl SolFilesCache {
self.files.retain(|file, _| Path::new(file).exists()) 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 /// Returns only the files that were changed from the provided sources, to save time
/// when compiling. /// when compiling.
pub fn get_changed_files<'a>( pub fn get_changed_files<'a>(

View File

@ -251,6 +251,7 @@ impl<Artifacts: ArtifactOutput> Project<Artifacts> {
Some(&self.solc_config), Some(&self.solc_config),
&self.paths.artifacts, &self.paths.artifacts,
); );
cache.remove_changed_files(&changed_files);
let cached_artifacts = if self.paths.artifacts.exists() { let cached_artifacts = if self.paths.artifacts.exists() {
cache.read_artifacts::<Artifacts>(&self.paths.artifacts)? cache.read_artifacts::<Artifacts>(&self.paths.artifacts)?

View File

@ -0,0 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.6.6;
contract Dapp {
function modified() public {}
}

View File

@ -88,7 +88,7 @@ fn can_compile_dapp_sample_with_cache() {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let orig_root = manifest_dir.join("test-data/dapp-sample"); 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(); copy_dir_all(orig_root, &tmp_dir).unwrap();
let paths = ProjectPathsConfig::builder() let paths = ProjectPathsConfig::builder()
.cache(cache) .cache(cache)
@ -117,11 +117,34 @@ fn can_compile_dapp_sample_with_cache() {
assert!(!compiled.is_unchanged()); assert!(!compiled.is_unchanged());
// new file is compiled even with partial cache // 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(); let compiled = project.compile().unwrap();
assert!(compiled.find("Dapp").is_some()); assert!(compiled.find("Dapp").is_some());
assert!(compiled.find("NewContract").is_some()); assert!(compiled.find("NewContract").is_some());
assert!(!compiled.is_unchanged()); 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 // deleted artifact is not taken from the cache
std::fs::remove_file(&project.paths.sources.join("Dapp.sol")).unwrap(); std::fs::remove_file(&project.paths.sources.join("Dapp.sol")).unwrap();