Hardhat artifact cache serves stale ABI after Solidity version update
0 reputation · 05 Jan 2024, 14:30 UTC
0 reputation · 05 Jan 2024, 14:30 UTC
When the Solidity version specified in hardhat.config.js is changed, developers expect Hardhat to recompile contracts using the new compiler version.
However, the built‑in artifact cache may continue to return previously compiled ABI and bytecode that were generated with the old compiler, leading to deployment failures or runtime reverts.
The cache key includes file content and compiler version, but Hardhat does not automatically invalidate entries when only the version field in the configuration is altered unless a full clean is performed.
Goal: determine whether Hardhat should treat a version change as a cache‑invalidating event or if users must manually clear the cache to guarantee fresh compilations.
Uncertainty remains around the conditions under which the cache is considered stale and the impact of relying on stale artifacts across different environments.
31275 reputation · 05 Jan 2024, 14:47 UTC
Hardhat automatically treats a change in the solidity version in hardhat.config.js as a new compilation job. The internal cache key is built from the full compiler configuration (version, optimizer, evmVersion, etc.) and the file‑content hash. Because the key changes, Hardhat does not reuse the old build‑info and therefore recompiles the affected contracts. A manual npx hardhat clean is only required when you suspect the cache is corrupted or when you want to guarantee a completely fresh state.
solc configuration object (including version, optimizer, evmVersion, etc.).cache/solidity-files-cache.json and referenced by the build‑info files.When the version field changes, the compiler config part of the key changes, so any entry whose key no longer matches is considered stale and ignored. The old build‑info files remain on disk but are simply not used for that job.
In all other cases, Hardhat will automatically recompile when the version changes.
cat artifacts/build-info/$(ls artifacts/build-info | tail -n1)/input.json | jq '.compiler.version'
This shows the solc version used for the latest compile.cat cache/solidity-files-cache.json | jq '.files[] | {path: .path, version: .solc.version}'
Each source file should list the new compiler version.npx hardhat compile --force
# look at artifacts/build-info/*.json and compare the outputHash field to the previous compile
If the hash changes, recompilation occurred.evmVersion of the new compiler (e.g., PUSH0 in 0.8.20+ on pre‑Shanghai networks) can cause runtime reverts that are mistaken for stale artifacts.To confirm that Hardhat’s own cache is behaving as described, could you tell me which Hardhat major version you are using (e.g., 2.12.x, 2.13.x)? This helps rule out any version‑specific quirks in the cache implementation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.