How to Ignore Solidity Files in Hardhat Compilation

09-Sep-2025

Ignore specific Solidity files in hardhat compilation to streamline your development workflow and avoid interruptions, as fast as possible

Photo by Mohammad Mardani on Unsplash

Introduction

In Solidity, the hardhat tool is often used for compilation and testing. By default, hardhat will take all .sol files in your project and compile them. However, sometimes you may only want to compile a subset of these files, and ignore others that are in development or not ready for inclusion in the compilation. This can be useful when hardhat may emit errors for files in development, which can interrupt the compilation process for deployment or testing. In this article, we'll show you how to configure hardhat to ignore specific Solidity files during compilation, so you don't have to remove or rename the files temporarily to avoid errors.

Setting up hardhat.config.js

1. Importing the TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS Task

The first step in configuring hardhat to ignore Solidity files is to import the TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS task from the hardhat/builtin-tasks/task-names module. This task allows you to specify a filter function that will be applied to the list of source paths before they are passed to the Solidity compiler.

To import this task, add the following line to your hardhat.config.js file:

import { TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS } from "hardhat/builtin-tasks/task-names";

2. Adding a Subtask to Filter Source Paths

Once you have imported the TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS task, you can add a subtask that sets the action for this task. The action should be a function that takes three arguments: the taskArgs, taskState, and runSuper function.

In the function, you can call the runSuper function to get the list of source paths that would normally be passed to the Solidity compiler. Then, you can apply a filter function to this list to remove any paths that you don't want to include in the compilation. For example, you could use a filter like this:

// Add a subtask that sets the action for the TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS task
subtask(TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS).setAction(async (_, __, runSuper) => {
// Get the list of source paths that would normally be passed to the Solidity compiler
const paths = await runSuper();

// Apply a filter function to exclude paths that contain the string "ignore"
return paths.filter((p: any) => !p.includes("ignore"));
});

This filter will exclude any paths that contain the string “ignore” from the list of source paths passed to the Solidity compiler. You can modify this filter as needed to suit your specific requirements.

With the configuration provided in this article, you can add the string “ignore” to the names of any Solidity files that you don’t want to include in the compilation. For example, if you have a file named token.ignore.sol, hardhat will not include it in the compilation process.

Conclusion

In this article, we showed you how to configure hardhat to ignore specific Solidity files during compilation. By importing the TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS task and adding a subtask with a custom filter function, you can control which files are included in the compilation process. This can be useful when working on large projects with many Solidity files, and can help you to focus on the files that are relevant to your current development tasks. It can also prevent hardhat from emitting errors for files in development, which can interrupt the compilation process for deployment or testing. Overall, using this method can help you to streamline your Solidity development workflow and improve the efficiency of your hardhat compilation and testing processes.

`Want to Connect? You can find me on Twitter/Github/Discord`


How to Ignore Solidity Files in Hardhat Compilation was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.

Also read: How Vietnam Uncovered Paynet Coin: A Massive Crypto MLM Ponzi Scheme
WHAT'S YOUR OPINION?
Related News