Skip to content

fix(hardhat-3-plugin): build the mock contracts with the project - #439

Merged
alexshchur merged 1 commit into
FhenixProtocol:masterfrom
nftpoetrist:fix/hh3-build-mocks-with-project
Nov 2, 2026
Merged

alexshchur merged 1 commit into
FhenixProtocol:masterfrom
nftpoetrist:fix/hh3-build-mocks-with-project

Conversation

@nftpoetrist

@nftpoetrist nftpoetrist commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add the mock contracts to solidity.npmFilesToBuild in the plugin's config hook, so a full hardhat build builds them with the project instead of deleting them.
  • Add a patch changeset for @cofhe/hardhat-3-plugin.

Root cause

The plugin compiles the mocks when the HRE is created (hre.ts#L152-L166), outside the project's root files. A full build, which hardhat test runs before the tests, then removes the artifacts of every file it did not build (build.ts#L115-L121, build-system.ts#L905-L936), including the mocks and their build info.

Each node:test worker creates its own HRE, so the workers compile the mocks again during the test run while other workers read the build info in network.connect(). A read that lands in the middle of a write fails with "Failed to parse build info", and that test file fails at setup.

Hardhat's node test runner does not count a test file that fails inside its describe callback as failing (a describe callback that throws makes hardhat test exit 0), so CI stays green when this happens. Two recent Test (local) runs ran only 23 of the 43 hardhat-3-plugin tests, with "Failed to parse build info" in the log: 38910038228, 38904992679.

Fix

resolvePluginConfig appends the mock files (@cofhe/mock-contracts/contracts/*.sol) to solidity.npmFilesToBuild. Hardhat includes those in every full build, so the cleanup keeps the mocks and the workers' startup build finds them up to date. The startup build stays for runs that skip the build (hardhat run --no-compile). getMockContractsNpmPaths now maps a new getMockContractsNpmFiles helper that returns the npm module paths.

If @cofhe/mock-contracts can't be resolved from the project, the startup build already fails with HHE901, so this adds no new failure.

Testing

  • New mock-build.test.ts: hre.solidity.getRootFilePaths() includes every mock contract. RED on master: fails on the first file (ABITest.sol).
  • From a clean artifacts/cache, creating the HRE and then running a full hardhat build: mock artifact directories go from 12 to 0 on master, and stay at 12 with the fix.
  • Cold hardhat test runs (cleared artifacts/cache, as in CI): on master a worker compiled the mocks again during the node:test phase in each of 5 runs; with the fix, in none of 5. 46 passing (master: 45).
  • hardhat-plugin-test: 102 passing. pnpm check:types, pnpm lint, pnpm check:formatting (tracked files), check:circles, check:pinned-deps: pass.

Scope

Hardhat 3 plugin only. User projects get the same change: a full hardhat build no longer removes the mock artifacts. #431 and #438 touch other files of this plugin; merged together locally, they apply cleanly.

@changeset-bot

changeset-bot Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3a3c232

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@cofhe/hardhat-3-plugin Patch
@cofhe/abi Patch
@cofhe/foundry-plugin Patch
@cofhe/hardhat-plugin Patch
@cofhe/mock-contracts Patch
@cofhe/react Patch
@cofhe/sdk Patch
@cofhe/site Patch
@cofhe/example-react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@nftpoetrist

Copy link
Copy Markdown
Contributor Author

Extra check outside the monorepo, against the published packages: a clean npm project with @cofhe/hardhat-3-plugin@1.0.1 (pulls @cofhe/mock-contracts@1.0.1), one contract that imports FHE.sol, and only the cofhe plugin registered (solidity: { version: '0.8.28' }). Each run starts from empty artifacts/cache, creates the HRE (the plugin builds the mocks) and then runs the build task. Counted: directories under artifacts/@cofhe/mock-contracts/contracts.

Hardhat Config After HRE created After full build
3.1.12 (pinned in this repo) plugin only 12 0
3.1.12 + the 12 mock files in solidity.npmFilesToBuild 12 12
3.21.0 (latest) plugin only 12 0
3.21.0 + the 12 mock files in solidity.npmFilesToBuild 12 12

Plain npx hardhat build gives the same end state in all four cases (exit 0, the project contract is built; mock artifacts 0 without npmFilesToBuild, 12 with it).

So projects using the published plugin lose the mock artifacts on every full build, on both Hardhat versions, and listing the mocks in npmFilesToBuild keeps them. In the table that list is set by hand in the user config; this PR adds the same entries from the plugin's config hook.

@nftpoetrist
nftpoetrist force-pushed the fix/hh3-build-mocks-with-project branch from 4368bc3 to de24792 Compare October 27, 2026 13:40
@nftpoetrist
nftpoetrist force-pushed the fix/hh3-build-mocks-with-project branch from de24792 to 3a3c232 Compare October 28, 2026 21:33
@alexshchur
alexshchur merged commit 7e6d490 into FhenixProtocol:master Nov 2, 2026
12 checks passed

@alexshchur alexshchur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, nice find @nftpoetrist . The full build was deleting the mocks and the test workers tripped over each other rebuilding them, silently dropping whole files from the run. Building them with the project fixes it properly. Merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants