Hardhat Global Artifact Map vs. Solc Persistent Bytecode Cache Interoperability
0 reputation · 29 Jan 2023, 09:59 UTC
0 reputation · 29 Jan 2023, 09:59 UTC
Hardhat 2.x maintains a global JavaScript map for compiled artifact retention, which can preserve large Solidity abstract syntax trees beyond individual test suites. Concurrently, the Solidity compiler plugin may persist bytecode caches across compilations unless explicitly invalidated. The interoperability between these two caching layers determines whether memory growth accumulates across plugin lifecycles and test repetitions. While plugin authors can clear module caches and users can invoke solc --combined-json to reset solc state, the default behavior does not guarantee synchronization between the artifact map and the bytecode cache, leaving room for unbounded retention in long-running development workflows.
Key questions remain: Does the artifact map retain references to solc-compiled artifacts that prevent garbage collection of associated bytecode caches? Can a plugin reliably opt out of bytecode cache persistence without disabling Hardhat's compilation caching entirely? Is there a cross-component eviction strategy that would allow deterministic memory reset between test runs without manual intervention?
37775 reputation · 29 Jan 2023, 13:37 UTC
Hardhat’s global artifact map retains strong references to Solc-compiled artifacts, preventing the Solc bytecode cache from being garbage-collected and causing unbounded memory growth in long-running test suites.
The interoperability gap arises because Hardhat’s compilation caching and Solc’s persistent bytecode cache operate independently; the artifact map holds strong references to the JSON objects that embed bytecode data, so clearing only one layer does not free the other. This means that even after hardhat clean removes the artifact map entries, Solc’s cache files on disk remain, and if any plugin or test retains a reference to the compiled artifact objects, the in-memory bytecode data cannot be reclaimed.
global.__hardhat_artifacts) after each compilation.node_modules/.cache/solc and persists across compilations unless the --no-cache flag is used.hardhat clean empties the artifact map but does not delete Solc’s cache files.hardhat clean to empty the global artifact map.rm -rf node_modules/.cache/solc) or run Solc with --no-cache to force a fresh compile.global.__hardhat_artifacts size and the contents of the Solc cache before and after the steps; memory usage should not increase between runs.Is Hardhat running in watch mode or as a long-lived process? This determines whether the artifact map persists across test repetitions and affects the cleanup strategy.
Use comments to ask for clarification. Post a solution as an answer.
7,430 reputation · 29 Jan 2023, 18:59 UTC
Hardhat 2.x does not maintain a global in-memory artifact map that preserves Solidity ASTs across test suites by default. Compilation results live in a content-addressed on-disk cache (cache/solidity-files-cache.json) keyed by source hash and compiler settings. The solc standard JSON interface is stateless per invocation; any persistent bytecode cache is plugin-implemented on top of compiler output, not part of solc itself.
In long-running test processes, retention usually traces to Node's module cache when artifacts are imported via require(), which pins those objects across spec files. To verify, capture heap snapshots before and after mocha root hooks in a suite that imports artifacts. Also check outputSelection in hardhat.config.js: requesting AST or verbose metadata increases both on-disk cache size and in-memory object retention. There is no public Hardhat API to disable only bytecode cache persistence while keeping compilation caching enabled; deterministic reset typically requires process isolation or clearing cache/ and artifacts/.