Skip to content

Commit

Permalink
Show warning if HardhatConfig returns undefined/empty object
Browse files Browse the repository at this point in the history
This is usually indicative of a mistake in HardhatConfig, hence we should warn users.

Relates to NomicFoundation#1490.
  • Loading branch information
kanej committed Dec 10, 2021
1 parent 75589c7 commit 6753b93
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-cows-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Show warning if user doesn't export or exports empty object from Hardhat config file (issue #1490)
3 changes: 3 additions & 0 deletions packages/hardhat-core/src/internal/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,13 @@ async function main() {

let taskName = parsedTaskName ?? TASK_HELP;

const showEmptyWarnings = true;
const showSolidityConfigWarnings = taskName === TASK_COMPILE;

const ctx = HardhatContext.createHardhatContext();

const config = loadConfigAndTasks(hardhatArguments, {
showEmptyWarnings,
showSolidityConfigWarnings,
});

Expand Down
34 changes: 33 additions & 1 deletion packages/hardhat-core/src/internal/core/config/config-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ export function resolveConfigPath(configPath: string | undefined) {

export function loadConfigAndTasks(
hardhatArguments?: Partial<HardhatArguments>,
{ showSolidityConfigWarnings } = { showSolidityConfigWarnings: false }
{
showEmptyWarnings = false,
showSolidityConfigWarnings = false,
}: {
showEmptyWarnings?: boolean;
showSolidityConfigWarnings?: boolean;
} = {
showEmptyWarnings: false,
showSolidityConfigWarnings: false,
}
): HardhatConfig {
let configPath =
hardhatArguments !== undefined ? hardhatArguments.config : undefined;
Expand Down Expand Up @@ -73,6 +82,10 @@ export function loadConfigAndTasks(
ctx.setConfigLoadingAsFinished();
}

if (showEmptyWarnings) {
checkEmptyConfig(userConfig, { showSolidityConfigWarnings });
}

validateConfig(userConfig);

if (showSolidityConfigWarnings) {
Expand Down Expand Up @@ -220,6 +233,25 @@ function readPackageJson(packageName: string): PackageJson | undefined {
}
}

function checkEmptyConfig(
userConfig: any,
{
showSolidityConfigWarnings = false,
}: { showSolidityConfigWarnings: boolean } = {
showSolidityConfigWarnings: false,
}
) {
if (userConfig === undefined || Object.keys(userConfig).length === 0) {
let warning = `Hardhat config is returning an empty config object, check the export from the config file if this is unexpected.\n`;

if (!showSolidityConfigWarnings) {
warning += `Learn more about compiler configuration at https://hardhat.org/config"\n`;
}

console.warn(chalk.yellow(warning));
}
}

function checkMissingSolidityConfig(userConfig: any) {
if (userConfig.solidity === undefined) {
console.warn(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = {};
module.exports = { solidity: undefined };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
15 changes: 15 additions & 0 deletions packages/hardhat-core/test/internal/core/config/config-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,21 @@ Hardhat plugin instead.`
resetHardhatContext();
});

it("should emit a warning if config is the empty object", function () {
loadConfigAndTasks(
{
config: "empty-config.js",
},
{ showEmptyWarnings: true }
);

assert.equal(consoleWarnStub.callCount, 1);
assert.include(
consoleWarnStub.args[0][0],
"Hardhat config is returning an empty config object, check the export from the config file if this is unexpected."
);
});

it("should emit a warning if there's no configured solidity", function () {
const config = loadConfigAndTasks(
{
Expand Down

0 comments on commit 6753b93

Please sign in to comment.