Skip to content

Commit 9f4f8ad

Browse files
committed
build: add the workaround to ignore Foundry tests when compiling with Hardhat
1 parent 2c20094 commit 9f4f8ad

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

hardhat.config.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint @typescript-eslint/no-non-null-assertion: ["off"] */
22

3-
import { HardhatUserConfig } from "hardhat/config";
3+
import { HardhatUserConfig, subtask } from "hardhat/config";
44
import type { MultiSolcUserConfig } from "hardhat/src/types/config";
5+
import { TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS } from "hardhat/builtin-tasks/task-names";
56
/* Uncomment if support of TypeScript `paths` mappings is needed.
67
* Make sure to run `pnpm add -D "tsconfig-paths@4.2.0"` in this case.
78
*/
@@ -22,6 +23,22 @@ dotenv.config();
2223

2324
import "./scripts/tasks/generate-account";
2425

26+
/* Sets the action for the `TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS` task.
27+
*
28+
* The approach to ignore Solidity files in `test` directories for Hardhat speeds up its compilation process
29+
* significantly. This is especially true in large projects with a lot of Foundry tests.
30+
* It can be also extended for deployment scripts as well.
31+
* See for more details:
32+
* `https://kennysliding.medium.com/how-to-ignore-solidity-files-in-hardhat-compilation-6162963f8c84`
33+
*/
34+
subtask(TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS).setAction(async (_, __, runSuper) => {
35+
/* Get the list of source paths that would normally be passed to the Solidity compiler, then
36+
* apply a filter function to exclude paths that contain the string "test".
37+
*/
38+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
39+
return (await runSuper()).filter((p: any) => !p.includes("test"));
40+
});
41+
2542
const envs = process.env;
2643

2744
// Private keys can be set in `.env` file.

0 commit comments

Comments
 (0)