Skip to content

Commit 83bcc42

Browse files
committed
added basic debug logging
1 parent 02730b0 commit 83bcc42

File tree

9 files changed

+98
-1
lines changed

9 files changed

+98
-1
lines changed

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v-next/core/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@
5555
"devDependencies": {
5656
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
5757
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
58-
"@nomicfoundation/hardhat-test-utils": "workspace:^",
5958
"@microsoft/api-extractor": "^7.43.4",
59+
"@nomicfoundation/hardhat-test-utils": "workspace:^",
60+
"@types/debug": "^4.1.4",
6061
"@types/node": "^20.14.9",
6162
"@types/semver": "^7.5.8",
6263
"@typescript-eslint/eslint-plugin": "^7.7.1",
@@ -79,6 +80,7 @@
7980
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2",
8081
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
8182
"chalk": "^5.3.0",
83+
"debug": "^4.1.1",
8284
"env-paths": "^2.2.0",
8385
"semver": "^7.6.2"
8486
}

v-next/core/src/internal/debug.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import debugLib from "debug";
2+
3+
/**
4+
* A simple decorator that adds debug logging for when a method is entered and exited.
5+
*
6+
* Example usage:
7+
*
8+
* ```
9+
* class MyClass {
10+
* @withDebugLogs("MyClass:exampleClassMethod")
11+
* public function exampleClassMethod(...)
12+
* }
13+
* ```
14+
*/
15+
export function withDebugLogs<This, Args extends any[], Return>(
16+
tag: string = "",
17+
) {
18+
return function actualDecorator(
19+
originalMethod: (this: This, ...args: Args) => Return,
20+
_context: ClassMethodDecoratorContext<
21+
This,
22+
(this: This, ...args: Args) => Return
23+
>,
24+
): (this: This, ...args: Args) => Return {
25+
const log = debugLib(`hardhat:core${tag === "" ? "" : `:${tag}`}`);
26+
27+
function replacementMethod(this: This, ...args: Args): Return {
28+
log(`Entering method with args:`, args);
29+
const result = originalMethod.call(this, ...args);
30+
log(`Exiting method.`);
31+
return result;
32+
}
33+
34+
return replacementMethod;
35+
};
36+
}

v-next/core/src/internal/hook-manager.ts

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
assertHardhatInvariant,
1818
} from "@ignored/hardhat-vnext-errors";
1919

20+
import { withDebugLogs } from "./debug.js";
21+
2022
export class HookManagerImplementation implements HookManager {
2123
readonly #pluginsInReverseOrder: HardhatPlugin[];
2224

@@ -99,6 +101,7 @@ export class HookManagerImplementation implements HookManager {
99101
);
100102
}
101103

104+
@withDebugLogs("HookManager:runHandlerChain")
102105
public async runHandlerChain<
103106
HookCategoryNameT extends keyof HardhatHooks,
104107
HookNameT extends keyof HardhatHooks[HookCategoryNameT],
@@ -137,6 +140,7 @@ export class HookManagerImplementation implements HookManager {
137140
return next(...handlerParams);
138141
}
139142

143+
@withDebugLogs("HookManager:runSequentialHandlers")
140144
public async runSequentialHandlers<
141145
HookCategoryNameT extends keyof HardhatHooks,
142146
HookNameT extends keyof HardhatHooks[HookCategoryNameT],
@@ -170,6 +174,7 @@ export class HookManagerImplementation implements HookManager {
170174
return result;
171175
}
172176

177+
@withDebugLogs("HookManager:runParallelHandlers")
173178
public async runParallelHandlers<
174179
HookCategoryNameT extends keyof HardhatHooks,
175180
HookNameT extends keyof HardhatHooks[HookCategoryNameT],

v-next/core/src/internal/hre.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { UserInterruptionManager } from "../types/user-interruptions.js";
1717
import { HardhatError } from "@ignored/hardhat-vnext-errors";
1818

1919
import { ResolvedConfigurationVariableImplementation } from "./configuration-variables.js";
20+
import { withDebugLogs } from "./debug.js";
2021
import {
2122
buildGlobalOptionDefinitions,
2223
resolveGlobalOptions,
@@ -29,6 +30,7 @@ import { UserInterruptionManagerImplementation } from "./user-interruptions.js";
2930
export class HardhatRuntimeEnvironmentImplementation
3031
implements HardhatRuntimeEnvironment
3132
{
33+
@withDebugLogs("HardhatRuntimeEnvironment:create")
3234
public static async create(
3335
inputUserConfig: HardhatUserConfig,
3436
userProvidedGlobalOptions: Partial<GlobalOptions>,

v-next/core/src/internal/tasks/resolved-task.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "@ignored/hardhat-vnext-errors";
1919
import { ensureError } from "@ignored/hardhat-vnext-utils/error";
2020

21+
import { withDebugLogs } from "../debug.js";
2122
import { detectPluginNpmDependencyProblems } from "../plugins/detect-plugin-npm-dependency-problems.js";
2223

2324
import { formatTaskId } from "./utils.js";
@@ -26,6 +27,7 @@ import { validateTaskArgumentValue } from "./validations.js";
2627
export class ResolvedTask implements Task {
2728
readonly #hre: HardhatRuntimeEnvironment;
2829

30+
@withDebugLogs("ResolvedTask:createEmptyTask")
2931
public static createEmptyTask(
3032
hre: HardhatRuntimeEnvironment,
3133
id: string[],
@@ -44,6 +46,7 @@ export class ResolvedTask implements Task {
4446
);
4547
}
4648

49+
@withDebugLogs("ResolvedTask:createNewTask")
4750
public static createNewTask(
4851
hre: HardhatRuntimeEnvironment,
4952
id: string[],
@@ -92,6 +95,7 @@ export class ResolvedTask implements Task {
9295
* @throws HardhatError if the task is empty, a required argument is missing,
9396
* a argument has an invalid type, or the file actions can't be resolved.
9497
*/
98+
@withDebugLogs("ResolvedTask:run")
9599
public async run(taskArguments: TaskArguments): Promise<any> {
96100
if (this.isEmpty) {
97101
throw new HardhatError(HardhatError.ERRORS.TASK_DEFINITIONS.EMPTY_TASK, {

v-next/core/src/internal/tasks/task-manager.ts

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@ignored/hardhat-vnext-errors";
1616

1717
import { TaskDefinitionType } from "../../types/tasks.js";
18+
import { withDebugLogs } from "../debug.js";
1819

1920
import { ResolvedTask } from "./resolved-task.js";
2021
import { formatTaskId, getActorFragment } from "./utils.js";
@@ -62,6 +63,7 @@ export class TaskManagerImplementation implements TaskManager {
6263
return this.#rootTasks;
6364
}
6465

66+
@withDebugLogs("TaskManager:getTask")
6567
public getTask(taskId: string | string[]): Task {
6668
taskId = Array.isArray(taskId) ? taskId : [taskId];
6769
if (taskId.length === 0) {
@@ -99,6 +101,7 @@ export class TaskManagerImplementation implements TaskManager {
99101
return task;
100102
}
101103

104+
@withDebugLogs("TaskManager:insertTask")
102105
#insertTask(taskId: string[], task: Task, pluginId?: string) {
103106
if (taskId.length === 0) {
104107
throw new HardhatError(

v-next/hardhat/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
5959
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
6060
"@nomicfoundation/hardhat-test-utils": "workspace:^",
61+
"@types/debug": "^4.1.4",
6162
"@types/node": "^20.14.9",
6263
"@typescript-eslint/eslint-plugin": "^7.7.1",
6364
"@typescript-eslint/parser": "^7.7.1",
@@ -78,6 +79,7 @@
7879
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
7980
"@ignored/hardhat-vnext-zod-utils": "workspace:^3.0.0-next.2",
8081
"chalk": "^5.3.0",
82+
"debug": "^4.1.1",
8183
"enquirer": "^2.3.0",
8284
"tsx": "^4.11.0",
8385
"zod": "^3.23.8"

v-next/hardhat/src/internal/cli/main.ts

+31
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "@ignored/hardhat-vnext-errors";
2727
import { isCi } from "@ignored/hardhat-vnext-utils/ci";
2828
import { kebabToCamelCase } from "@ignored/hardhat-vnext-utils/string";
29+
import debug from "debug";
2930

3031
import { resolveHardhatConfigPath } from "../../config.js";
3132
import { createHardhatRuntimeEnvironment } from "../../hre.js";
@@ -45,18 +46,26 @@ export async function main(
4546
cliArguments: string[],
4647
print: (message: string) => void = console.log,
4748
): Promise<void> {
49+
const log = debug("hardhat:core:cli:main");
50+
4851
let builtinGlobalOptions;
4952

53+
log("Hardhat CLI started with arguments:", cliArguments);
54+
5055
try {
5156
const usedCliArguments: boolean[] = new Array(cliArguments.length).fill(
5257
false,
5358
);
5459

60+
log("Parsing builtin global options");
61+
5562
builtinGlobalOptions = await parseBuiltinGlobalOptions(
5663
cliArguments,
5764
usedCliArguments,
5865
);
5966

67+
log("Parsed builtin global options:", builtinGlobalOptions);
68+
6069
if (builtinGlobalOptions.version) {
6170
return await printVersionMessage(print);
6271
}
@@ -65,15 +74,27 @@ export async function main(
6574
return await initHardhat();
6675
}
6776

77+
log("Checking telemetry consent");
78+
6879
// TODO: the consent will be enabled in the other PRs related to telemetry
6980
const _telemetryConsent = await getTelemetryConsent();
7081

82+
log("Telemetry consent:", _telemetryConsent);
83+
7184
if (builtinGlobalOptions.configPath === undefined) {
85+
log("Config path not provided, attempting to resolve it");
86+
7287
builtinGlobalOptions.configPath = await resolveHardhatConfigPath();
88+
89+
log("Resolved config path:", builtinGlobalOptions.configPath);
7390
}
7491

92+
log("Importing user config");
93+
7594
const userConfig = await importUserConfig(builtinGlobalOptions.configPath);
7695

96+
log("User config imported:", userConfig);
97+
7798
const configPlugins = Array.isArray(userConfig.plugins)
7899
? userConfig.plugins
79100
: [];
@@ -83,6 +104,8 @@ export async function main(
83104
builtinGlobalOptions.configPath,
84105
);
85106

107+
log("Resolved plugins:", resolvedPlugins);
108+
86109
const pluginGlobalOptionDefinitions =
87110
buildGlobalOptionDefinitions(resolvedPlugins);
88111
const globalOptionDefinitions = new Map([
@@ -95,6 +118,10 @@ export async function main(
95118
usedCliArguments,
96119
);
97120

121+
log("User provided global options:", userProvidedGlobalOptions);
122+
123+
log("Creating Hardhat Runtime Environment");
124+
98125
const hre = await createHardhatRuntimeEnvironment(
99126
userConfig,
100127
{ ...builtinGlobalOptions, ...userProvidedGlobalOptions },
@@ -106,6 +133,8 @@ export async function main(
106133

107134
const taskOrId = parseTask(cliArguments, usedCliArguments, hre);
108135

136+
log("Parsed task:", taskOrId);
137+
109138
if (Array.isArray(taskOrId)) {
110139
if (taskOrId.length === 0) {
111140
const globalHelp = await getGlobalHelpString(
@@ -138,6 +167,8 @@ export async function main(
138167
task,
139168
);
140169

170+
log(`Running task "${task.id.join(" ")}" with arguments:`, taskArguments);
171+
141172
await task.run(taskArguments);
142173
} catch (error) {
143174
printErrorMessages(error, builtinGlobalOptions?.showStackTraces);

0 commit comments

Comments
 (0)