Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic debug logging #5541

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v-next/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
"@nomicfoundation/hardhat-test-utils": "workspace:^",
"@microsoft/api-extractor": "^7.43.4",
"@nomicfoundation/hardhat-test-utils": "workspace:^",
"@types/node": "^20.14.9",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^7.7.1",
Expand Down
3 changes: 3 additions & 0 deletions v-next/hardhat-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"./common-errors": "./dist/src/common-errors.js",
"./crypto": "./dist/src/crypto.js",
"./date": "./dist/src/date.js",
"./debug": "./dist/src/debug.js",
"./error": "./dist/src/error.js",
"./eth": "./dist/src/eth.js",
"./fs": "./dist/src/fs.js",
Expand Down Expand Up @@ -57,6 +58,7 @@
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
"@types/bn.js": "^5.1.5",
"@types/debug": "^4.1.4",
"@types/keccak": "^3.0.4",
"@types/node": "^20.14.9",
"@typescript-eslint/eslint-plugin": "^7.7.1",
Expand All @@ -74,6 +76,7 @@
"typescript-eslint": "7.7.1"
},
"dependencies": {
"debug": "^4.1.1",
"fast-equals": "^5.0.1",
"keccak": "^3.0.4",
"rfdc": "^1.3.1",
Expand Down
38 changes: 38 additions & 0 deletions v-next/hardhat-utils/src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import debugLib from "debug";

/**
* A simple decorator that adds debug logging for when a method is entered and exited.
*
* This decorator is meant to be used for debugging purposes only. It should not be committed in runtime code.
*
* Example usage:
*
* ```
* class MyClass {
* @withDebugLogs("MyClass:exampleClassMethod")
* public function exampleClassMethod(...)
* }
* ```
*/
export function withDebugLogs<This, Args extends any[], Return>(
tag: string = "",
) {
return function actualDecorator(
originalMethod: (this: This, ...args: Args) => Return,
_context: ClassMethodDecoratorContext<
This,
(this: This, ...args: Args) => Return
>,
): (this: This, ...args: Args) => Return {
const log = debugLib(`hardhat:dev:core${tag === "" ? "" : `:${tag}`}`);

function replacementMethod(this: This, ...args: Args): Return {
log(`Entering method with args:`, args);
const result = originalMethod.call(this, ...args);
log(`Exiting method.`);
return result;
}

return replacementMethod;
};
}
2 changes: 2 additions & 0 deletions v-next/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
"@nomicfoundation/hardhat-test-utils": "workspace:^",
"@types/debug": "^4.1.4",
"@types/node": "^20.14.9",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
Expand All @@ -78,6 +79,7 @@
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-zod-utils": "workspace:^3.0.0-next.2",
"chalk": "^5.3.0",
"debug": "^4.1.1",
"enquirer": "^2.3.0",
"tsx": "^4.11.0",
"zod": "^3.23.8"
Expand Down
19 changes: 19 additions & 0 deletions v-next/hardhat/src/internal/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { isCi } from "@ignored/hardhat-vnext-utils/ci";
import { getRealPath } from "@ignored/hardhat-vnext-utils/fs";
import { kebabToCamelCase } from "@ignored/hardhat-vnext-utils/string";
import debug from "debug";

import { resolveHardhatConfigPath } from "../../config.js";
import { createHardhatRuntimeEnvironment } from "../../hre.js";
Expand All @@ -47,8 +48,12 @@ export async function main(
cliArguments: string[],
print: (message: string) => void = console.log,
): Promise<void> {
const log = debug("hardhat:core:cli:main");

let builtinGlobalOptions;

log("Hardhat CLI started");

try {
const usedCliArguments: boolean[] = new Array(cliArguments.length).fill(
false,
Expand All @@ -59,6 +64,8 @@ export async function main(
usedCliArguments,
);

log("Parsed builtin global options");

if (builtinGlobalOptions.version) {
return await printVersionMessage(print);
}
Expand All @@ -69,8 +76,12 @@ export async function main(

await ensureTelemetryConsent();

log("Retrieved telemetry consent");

if (builtinGlobalOptions.configPath === undefined) {
builtinGlobalOptions.configPath = await resolveHardhatConfigPath();

log("Resolved config path");
}

const projectRoot = await resolveProjectRoot(
Expand All @@ -79,12 +90,16 @@ export async function main(

const userConfig = await importUserConfig(builtinGlobalOptions.configPath);

log("User config imported");

const configPlugins = Array.isArray(userConfig.plugins)
? userConfig.plugins
: [];
const plugins = [...builtinPlugins, ...configPlugins];
const resolvedPlugins = await resolvePluginList(projectRoot, plugins);

log("Resolved plugins");

const pluginGlobalOptionDefinitions =
buildGlobalOptionDefinitions(resolvedPlugins);
const globalOptionDefinitions = new Map([
Expand All @@ -97,6 +112,8 @@ export async function main(
usedCliArguments,
);

log("Creating Hardhat Runtime Environment");

const hre = await createHardhatRuntimeEnvironment(
userConfig,
{ ...builtinGlobalOptions, ...userProvidedGlobalOptions },
Expand Down Expand Up @@ -141,6 +158,8 @@ export async function main(
task,
);

log(`Running task "${task.id.join(" ")}"`);

await task.run(taskArguments);
} catch (error) {
printErrorMessages(error, builtinGlobalOptions?.showStackTraces);
Expand Down