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

fix: pass partial artifact cache to project compiler output #623

Merged
merged 4 commits into from Nov 26, 2021
Merged

fix: pass partial artifact cache to project compiler output #623

merged 4 commits into from Nov 26, 2021

Conversation

ghost
Copy link

@ghost ghost commented Nov 26, 2021

Motivation

If the cache contains some artifacts, but not all, and there are changed files, then the project compiler output does not contain the cached artifacts.

This means when calling find or into_artifacts, the cached artifacts would not be present in artifacts.

For example, if you run a compile, then create a new contract and run compile again, the ProjectCompileOutput
will not contain the cached artifacts and you have read the cache manually in order to get them. The provided
test covers this scenario.

This also results in some strange behavior when you run compile multiple times, as the previous cache is overwritten
with a new partial cache covering only the new files.

This originated from using foundry and seeing the above issue when running a newly created test and having it flip flop
between the old and new tests.
If I misunderstood how this works, or this is the intended behavior of the library and if cache reading should be done manually, then feel free to close this PR.

Solution

If any cached artifacts exists, they are now returned as part of the project compiler output. Any missing files are removed from the cache.

PR Checklist

  • Added Tests
  • Updated the changelog

korboros added 2 commits November 26, 2021 00:42
If the cache contains some artifacts but not all, the
project compiler output now contains the cached artifacts
in addition to the newly compiled artifacts.
@mattsse
Copy link
Collaborator

mattsse commented Nov 26, 2021

If the cache contains some artifacts, but not all, and there are changed files, then the project compiler output does not contain the cached artifacts.

That sounds right to me, that means, in that case, we need to merge the compiler output with the cached artifacts.

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great find, thank you!
this looks reasonable to me and the root cause you identified is the reason for the issue.

some nits.

ethers-solc/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines +127 to +129
std::fs::remove_file(&project.paths.sources.join("Dapp.sol")).unwrap();
let compiled = project.compile().unwrap();
assert!(compiled.find("Dapp").is_none());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this verifies that we clear deleted files from the cache

Comment on lines +120 to +124
std::fs::copy(new_file, 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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hits from_compiler_output_and_cache since we have a cached artifact but need to compile the NewContract.
lgtm

Comment on lines 252 to 254
let cached_artifacts = cache
.read_artifacts::<Artifacts>(&self.paths.artifacts)
.unwrap_or_else(|_| BTreeMap::default());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unlikely to fail since we checked that those files exists, but could still fail due to some race conditions, like the files got wiped after we checked that they exist...

I feel we should propagate the error here because if that would fail, we end up with a successful output that lacks some artifacts.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main failure case (outside of a race) for the fallback was when the artifacts folder is deleted, but the cache metadata still exists.
I'll change it to propagate the error, and fall back to empty cache if there is no artifacts folder. This way if some of the files have been wiped, it'll return the error. There's still a chance that the folder gets deleted at the same time still and then there'd be missing artifacts.

Copy link
Owner

@gakonst gakonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great find!

@gakonst gakonst merged commit d77068e into gakonst:master Nov 26, 2021
@ghost ghost deleted the fix/partial-cache branch November 27, 2021 02:37
gakonst added a commit to foundry-rs/foundry that referenced this pull request Nov 28, 2021
1. Introduces ethers-solc tracing
2. Fixes various ethers-solc caching bugs
    gakonst/ethers-rs#623
    gakonst/ethers-rs#629
    gakonst/ethers-rs#630
gakonst added a commit to foundry-rs/foundry that referenced this pull request Nov 28, 2021
1. Introduces ethers-solc tracing
2. Fixes various ethers-solc caching bugs
    gakonst/ethers-rs#623
    gakonst/ethers-rs#629
    gakonst/ethers-rs#630
meetmangukiya pushed a commit to meetmangukiya/ethers-rs that referenced this pull request Mar 21, 2022
* Use relative path for git commands in install cmd

* Simplify function signatures in `forge install`
charisma98 added a commit to charisma98/foundry that referenced this pull request Mar 4, 2023
1. Introduces ethers-solc tracing
2. Fixes various ethers-solc caching bugs
    gakonst/ethers-rs#623
    gakonst/ethers-rs#629
    gakonst/ethers-rs#630
0129general added a commit to 0129general/FoundryProject that referenced this pull request May 8, 2024
1. Introduces ethers-solc tracing
2. Fixes various ethers-solc caching bugs
    gakonst/ethers-rs#623
    gakonst/ethers-rs#629
    gakonst/ethers-rs#630
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants