Skip to content

Commit

Permalink
Merge pull request #5908 from NomicFoundation/common-error-messages
Browse files Browse the repository at this point in the history
Errors and messages improvements
  • Loading branch information
alcuadrado authored Oct 30, 2024
2 parents 5f5e626 + 5e5ab14 commit 45fe599
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 21 deletions.
21 changes: 21 additions & 0 deletions v-next/hardhat/src/internal/cli/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export function printErrorMessages(
shouldShowStackTraces: boolean = false,
print: (message: string) => void = console.error,
): void {
// If Hardhat is running on CJS mode we print a special message
if (isRequireESMError(error)) {
printEsmOnlyErrorMessage(print);
return;
}

const showStackTraces =
shouldShowStackTraces ||
getErrorWithCategory(error).category === ErrorCategory.OTHER;
Expand Down Expand Up @@ -151,3 +157,18 @@ function getErrorMessages(error: unknown): ErrorMessages {
};
}
}

function isRequireESMError(error: unknown): boolean {
return (
error instanceof Error &&
"code" in error &&
error.code === "ERR_REQUIRE_CYCLE_MODULE" &&
error.message.includes("Cannot require() ES Module")
);
}

function printEsmOnlyErrorMessage(print: (message: string) => void) {
print(`Hardhat only supports ESM projects.
Please make sure you have \`"type": "module"\` in your package.json`);
}
40 changes: 19 additions & 21 deletions v-next/hardhat/src/internal/cli/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path";
import chalk from "chalk";

import { findClosestHardhatConfig } from "../../config-loading.js";
import {
getHardhatVersion,
getLatestHardhatVersion,
} from "../../utils/package.js";
import { getHardhatVersion } from "../../utils/package.js";

import { HARDHAT_NAME } from "./constants.js";
import {
Expand Down Expand Up @@ -134,23 +131,24 @@ export async function printWelcomeMessage(): Promise<void> {
chalk.cyan(`👷 Welcome to ${HARDHAT_NAME} v${hardhatVersion} 👷\n`),
);

// Warn the user if they are using an outdated version of Hardhat
try {
const latestHardhatVersion = await getLatestHardhatVersion();
if (hardhatVersion !== latestHardhatVersion) {
console.warn(
chalk.yellow.bold(
`⚠️ You are using an outdated version of Hardhat. The latest version is v${latestHardhatVersion}. Please consider upgrading to the latest version before continuing with the project initialization. ⚠️\n`,
),
);
}
} catch (e) {
console.warn(
chalk.yellow.bold(
`⚠️ We couldn't check if you are using the latest version of Hardhat. Please consider upgrading to the latest version if you are not using it yet. ⚠️\n`,
),
);
}
// TODO: Disabled this until the first release of v3
// // Warn the user if they are using an outdated version of Hardhat
// try {
// const latestHardhatVersion = await getLatestHardhatVersion();
// if (hardhatVersion !== latestHardhatVersion) {
// console.warn(
// chalk.yellow.bold(
// `⚠️ You are using an outdated version of Hardhat. The latest version is v${latestHardhatVersion}. Please consider upgrading to the latest version before continuing with the project initialization. ⚠️\n`,
// ),
// );
// }
// } catch (e) {
// console.warn(
// chalk.yellow.bold(
// `⚠️ We couldn't check if you are using the latest version of Hardhat. Please consider upgrading to the latest version if you are not using it yet. ⚠️\n`,
// ),
// );
// }
}

/**
Expand Down
5 changes: 5 additions & 0 deletions v-next/hardhat/src/internal/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { createHardhatRuntimeEnvironment } from "../hre-intialization.js";
import { printErrorMessages } from "./error-handler.js";
import { getGlobalHelpString } from "./helpers/getGlobalHelpString.js";
import { getHelpString } from "./helpers/getHelpString.js";
import { printNodeJsVersionWarningIfNecessary } from "./node-version.js";
import { ensureTelemetryConsent } from "./telemetry/telemetry-permissions.js";
import { printVersionMessage } from "./version.js";

Expand All @@ -50,6 +51,8 @@ export async function main(
): Promise<void> {
const print = options.print ?? console.log;

printNodeJsVersionWarningIfNecessary(print);

const log = debug("hardhat:core:cli:main");

let builtinGlobalOptions;
Expand Down Expand Up @@ -176,6 +179,8 @@ export async function main(
if (options.rethrowErrors) {
throw error;
}

process.exitCode = 1;
}
}

Expand Down
48 changes: 48 additions & 0 deletions v-next/hardhat/src/internal/cli/node-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// NOTE: We don't use the `semver` package because it's slow to load, and this
// is always run during the initialization of the CLI.

import chalk from "chalk";

const MIN_SUPPORTED_NODE_VERSION = [22, 10, 0];

function isNodeVersionSupported(): boolean {
try {
const [majorStr, minorStr, patchStr] = process.versions.node.split(".");

const major = parseInt(majorStr, 10);
const minor = parseInt(minorStr, 10);
const patch = parseInt(patchStr, 10);

if (major < MIN_SUPPORTED_NODE_VERSION[0]) {
return false;
}

if (minor < MIN_SUPPORTED_NODE_VERSION[1]) {
return false;
}

if (patch < MIN_SUPPORTED_NODE_VERSION[2]) {
return false;
}
} catch {
// If our parsing of the version fails we assume it's supported.
return true;
}

return true;
}

export function printNodeJsVersionWarningIfNecessary(
print: (message: string) => void,
): void {
if (isNodeVersionSupported()) {
return;
}

print("");
print(
chalk.bold(`${chalk.yellow("WARNING:")} You are using Node.js ${process.versions.node} which is not supported by Hardhat.
Please upgrade to ${MIN_SUPPORTED_NODE_VERSION.join(".")} or a later version.`),
);
print("");
}

0 comments on commit 45fe599

Please sign in to comment.