hardhat 2.17.3 → 2.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +1 -1
  2. package/builtin-tasks/help.js +36 -5
  3. package/builtin-tasks/help.js.map +1 -1
  4. package/internal/cli/ArgumentsParser.d.ts +7 -2
  5. package/internal/cli/ArgumentsParser.d.ts.map +1 -1
  6. package/internal/cli/ArgumentsParser.js +72 -11
  7. package/internal/cli/ArgumentsParser.js.map +1 -1
  8. package/internal/cli/HelpPrinter.d.ts +7 -3
  9. package/internal/cli/HelpPrinter.d.ts.map +1 -1
  10. package/internal/cli/HelpPrinter.js +53 -22
  11. package/internal/cli/HelpPrinter.js.map +1 -1
  12. package/internal/cli/cli.js +69 -27
  13. package/internal/cli/cli.js.map +1 -1
  14. package/internal/core/config/config-env.d.ts +2 -1
  15. package/internal/core/config/config-env.d.ts.map +1 -1
  16. package/internal/core/config/config-env.js +7 -1
  17. package/internal/core/config/config-env.js.map +1 -1
  18. package/internal/core/errors-list.d.ts +42 -0
  19. package/internal/core/errors-list.d.ts.map +1 -1
  20. package/internal/core/errors-list.js +52 -2
  21. package/internal/core/errors-list.js.map +1 -1
  22. package/internal/core/jsonrpc/types/input/blockTag.d.ts +3 -3
  23. package/internal/core/jsonrpc/types/input/filterRequest.d.ts +6 -6
  24. package/internal/core/runtime-environment.d.ts +5 -3
  25. package/internal/core/runtime-environment.d.ts.map +1 -1
  26. package/internal/core/runtime-environment.js +30 -7
  27. package/internal/core/runtime-environment.js.map +1 -1
  28. package/internal/core/tasks/dsl.d.ts +10 -1
  29. package/internal/core/tasks/dsl.d.ts.map +1 -1
  30. package/internal/core/tasks/dsl.js +64 -4
  31. package/internal/core/tasks/dsl.js.map +1 -1
  32. package/internal/core/tasks/task-definitions.d.ts +31 -4
  33. package/internal/core/tasks/task-definitions.d.ts.map +1 -1
  34. package/internal/core/tasks/task-definitions.js +51 -5
  35. package/internal/core/tasks/task-definitions.js.map +1 -1
  36. package/internal/core/tasks/util.d.ts +6 -0
  37. package/internal/core/tasks/util.d.ts.map +1 -0
  38. package/internal/core/tasks/util.js +19 -0
  39. package/internal/core/tasks/util.js.map +1 -0
  40. package/internal/hardhat-network/provider/modules/base.d.ts +17 -0
  41. package/internal/hardhat-network/provider/modules/base.d.ts.map +1 -0
  42. package/internal/hardhat-network/provider/modules/base.js +127 -0
  43. package/internal/hardhat-network/provider/modules/base.js.map +1 -0
  44. package/internal/hardhat-network/provider/modules/debug.d.ts +5 -3
  45. package/internal/hardhat-network/provider/modules/debug.d.ts.map +1 -1
  46. package/internal/hardhat-network/provider/modules/debug.js +21 -5
  47. package/internal/hardhat-network/provider/modules/debug.js.map +1 -1
  48. package/internal/hardhat-network/provider/modules/eth.d.ts +2 -7
  49. package/internal/hardhat-network/provider/modules/eth.d.ts.map +1 -1
  50. package/internal/hardhat-network/provider/modules/eth.js +12 -60
  51. package/internal/hardhat-network/provider/modules/eth.js.map +1 -1
  52. package/internal/hardhat-network/provider/node.d.ts +1 -0
  53. package/internal/hardhat-network/provider/node.d.ts.map +1 -1
  54. package/internal/hardhat-network/provider/node.js +6 -0
  55. package/internal/hardhat-network/provider/node.js.map +1 -1
  56. package/internal/lib/hardhat-lib.d.ts.map +1 -1
  57. package/internal/lib/hardhat-lib.js +1 -1
  58. package/internal/lib/hardhat-lib.js.map +1 -1
  59. package/internal/sentry/anonymizer.d.ts.map +1 -1
  60. package/internal/sentry/anonymizer.js +3 -1
  61. package/internal/sentry/anonymizer.js.map +1 -1
  62. package/package.json +1 -1
  63. package/register.js +1 -1
  64. package/register.js.map +1 -1
  65. package/src/builtin-tasks/help.ts +47 -5
  66. package/src/internal/cli/ArgumentsParser.ts +79 -13
  67. package/src/internal/cli/HelpPrinter.ts +89 -24
  68. package/src/internal/cli/cli.ts +106 -38
  69. package/src/internal/core/config/config-env.ts +11 -0
  70. package/src/internal/core/errors-list.ts +56 -2
  71. package/src/internal/core/runtime-environment.ts +32 -7
  72. package/src/internal/core/tasks/dsl.ts +99 -4
  73. package/src/internal/core/tasks/task-definitions.ts +97 -3
  74. package/src/internal/core/tasks/util.ts +18 -0
  75. package/src/internal/hardhat-network/provider/modules/base.ts +156 -0
  76. package/src/internal/hardhat-network/provider/modules/debug.ts +50 -9
  77. package/src/internal/hardhat-network/provider/modules/eth.ts +16 -91
  78. package/src/internal/hardhat-network/provider/node.ts +12 -0
  79. package/src/internal/lib/hardhat-lib.ts +1 -0
  80. package/src/internal/sentry/anonymizer.ts +3 -1
  81. package/src/register.ts +1 -0
  82. package/src/types/runtime.ts +35 -1
  83. package/types/runtime.d.ts +24 -1
  84. package/types/runtime.d.ts.map +1 -1
@@ -2,6 +2,9 @@ import {
2
2
  HardhatParamDefinitions,
3
3
  ParamDefinition,
4
4
  ParamDefinitionsMap,
5
+ ScopeDefinition,
6
+ ScopesMap,
7
+ TaskDefinition,
5
8
  TasksMap,
6
9
  } from "../../types";
7
10
  import { HardhatError } from "../core/errors";
@@ -15,55 +18,69 @@ export class HelpPrinter {
15
18
  private readonly _executableName: string,
16
19
  private readonly _version: string,
17
20
  private readonly _hardhatParamDefinitions: HardhatParamDefinitions,
18
- private readonly _tasks: TasksMap
21
+ private readonly _tasks: TasksMap,
22
+ private readonly _scopes: ScopesMap
19
23
  ) {}
20
24
 
21
25
  public printGlobalHelp(includeSubtasks = false) {
22
26
  console.log(`${this._programName} version ${this._version}\n`);
23
27
 
24
28
  console.log(
25
- `Usage: ${this._executableName} [GLOBAL OPTIONS] <TASK> [TASK OPTIONS]\n`
29
+ `Usage: ${this._executableName} [GLOBAL OPTIONS] [SCOPE] <TASK> [TASK OPTIONS]\n`
26
30
  );
27
31
 
28
32
  console.log("GLOBAL OPTIONS:\n");
29
33
 
30
- this._printParamDetails(this._hardhatParamDefinitions);
34
+ let length = this._printParamDetails(this._hardhatParamDefinitions);
31
35
 
32
36
  console.log("\n\nAVAILABLE TASKS:\n");
33
37
 
34
- const tasksToShow: TasksMap = {};
35
- for (const [taskName, taskDefinition] of Object.entries(this._tasks)) {
36
- if (includeSubtasks || !taskDefinition.isSubtask) {
37
- tasksToShow[taskName] = taskDefinition;
38
- }
39
- }
40
-
41
- const nameLength = Object.keys(tasksToShow)
42
- .map((n) => n.length)
43
- .reduce((a, b) => Math.max(a, b), 0);
38
+ length = this._printTasks(this._tasks, includeSubtasks, length);
44
39
 
45
- for (const name of Object.keys(tasksToShow).sort()) {
46
- const { description = "" } = this._tasks[name];
40
+ if (Object.keys(this._scopes).length > 0) {
41
+ console.log("\n\nAVAILABLE TASK SCOPES:\n");
47
42
 
48
- console.log(` ${name.padEnd(nameLength)}\t${description}`);
43
+ this._printScopes(this._scopes, length);
49
44
  }
50
45
 
51
46
  console.log("");
52
47
 
53
48
  console.log(
54
- `To get help for a specific task run: npx ${this._executableName} help [task]\n`
49
+ `To get help for a specific task run: npx ${this._executableName} help [SCOPE] <TASK>\n`
55
50
  );
56
51
  }
57
52
 
58
- public printTaskHelp(taskName: string) {
59
- const taskDefinition = this._tasks[taskName];
53
+ public printScopeHelp(
54
+ scopeDefinition: ScopeDefinition,
55
+ includeSubtasks = false
56
+ ) {
57
+ const name = scopeDefinition.name;
58
+ const description = scopeDefinition.description ?? "";
59
+
60
+ console.log(`${this._programName} version ${this._version}`);
60
61
 
61
- if (taskDefinition === undefined) {
62
- throw new HardhatError(ERRORS.ARGUMENTS.UNRECOGNIZED_TASK, {
63
- task: taskName,
62
+ console.log(
63
+ `\nUsage: hardhat [GLOBAL OPTIONS] ${name} <TASK> [TASK OPTIONS]`
64
+ );
65
+
66
+ console.log(`\nAVAILABLE TASKS:\n`);
67
+
68
+ if (this._scopes[name] === undefined) {
69
+ throw new HardhatError(ERRORS.ARGUMENTS.UNRECOGNIZED_SCOPE, {
70
+ scope: name,
64
71
  });
65
72
  }
66
73
 
74
+ this._printTasks(this._scopes[name].tasks, includeSubtasks);
75
+
76
+ console.log(`\n${name}: ${description}`);
77
+
78
+ console.log(
79
+ `\nFor global options help run: ${this._executableName} help\n`
80
+ );
81
+ }
82
+
83
+ public printTaskHelp(taskDefinition: TaskDefinition) {
67
84
  const {
68
85
  description = "",
69
86
  name,
@@ -78,8 +95,11 @@ export class HelpPrinter {
78
95
  positionalParamDefinitions
79
96
  );
80
97
 
98
+ const scope =
99
+ taskDefinition.scope !== undefined ? `${taskDefinition.scope} ` : "";
100
+
81
101
  console.log(
82
- `Usage: ${this._executableName} [GLOBAL OPTIONS] ${name}${paramsList}${positionalParamsList}\n`
102
+ `Usage: ${this._executableName} [GLOBAL OPTIONS] ${scope}${name}${paramsList}${positionalParamsList}\n`
83
103
  );
84
104
 
85
105
  if (Object.keys(paramDefinitions).length > 0) {
@@ -103,6 +123,49 @@ export class HelpPrinter {
103
123
  console.log(`For global options help run: ${this._executableName} help\n`);
104
124
  }
105
125
 
126
+ private _printTasks(
127
+ tasksMap: TasksMap,
128
+ includeSubtasks: boolean,
129
+ length: number = 0
130
+ ) {
131
+ const taskNameList = Object.entries(tasksMap)
132
+ .filter(
133
+ ([, taskDefinition]) => includeSubtasks || !taskDefinition.isSubtask
134
+ )
135
+ .map(([taskName]) => taskName)
136
+ .sort();
137
+
138
+ const nameLength = taskNameList
139
+ .map((n) => n.length)
140
+ .reduce((a, b) => Math.max(a, b), length);
141
+
142
+ for (const name of taskNameList) {
143
+ const { description = "" } = tasksMap[name];
144
+
145
+ console.log(` ${name.padEnd(nameLength)}\t${description}`);
146
+ }
147
+
148
+ return nameLength;
149
+ }
150
+
151
+ private _printScopes(scopesMap: ScopesMap, length: number) {
152
+ const scopeNamesList = Object.entries(scopesMap)
153
+ .map(([scopeName]) => scopeName)
154
+ .sort();
155
+
156
+ const nameLength = scopeNamesList
157
+ .map((n) => n.length)
158
+ .reduce((a, b) => Math.max(a, b), length);
159
+
160
+ for (const name of scopeNamesList) {
161
+ const { description = "" } = scopesMap[name];
162
+
163
+ console.log(` ${name.padEnd(nameLength)}\t${description}`);
164
+ }
165
+
166
+ return nameLength;
167
+ }
168
+
106
169
  private _getParamValueDescription<T>(paramDefinition: ParamDefinition<T>) {
107
170
  return `<${paramDefinition.type.name.toUpperCase()}>`;
108
171
  }
@@ -162,7 +225,7 @@ export class HelpPrinter {
162
225
  return paramsList;
163
226
  }
164
227
 
165
- private _printParamDetails(paramDefinitions: ParamDefinitionsMap) {
228
+ private _printParamDetails(paramDefinitions: ParamDefinitionsMap): number {
166
229
  const paramsNameLength = Object.keys(paramDefinitions)
167
230
  .map((n) => ArgumentsParser.paramNameToCLA(n).length)
168
231
  .reduce((a, b) => Math.max(a, b), 0);
@@ -185,6 +248,8 @@ export class HelpPrinter {
185
248
 
186
249
  console.log(msg);
187
250
  }
251
+
252
+ return paramsNameLength;
188
253
  }
189
254
 
190
255
  private _printPositionalParamDetails(
@@ -23,7 +23,10 @@ import { ERRORS, getErrorCode } from "../core/errors-list";
23
23
  import { isHardhatInstalledLocallyOrLinked } from "../core/execution-mode";
24
24
  import { getEnvHardhatArguments } from "../core/params/env-variables";
25
25
  import { HARDHAT_PARAM_DEFINITIONS } from "../core/params/hardhat-params";
26
- import { isCwdInsideProject } from "../core/project-structure";
26
+ import {
27
+ getUserConfigPath,
28
+ isCwdInsideProject,
29
+ } from "../core/project-structure";
27
30
  import { Environment } from "../core/runtime-environment";
28
31
  import { loadTsNode, willRunWithTypescript } from "../core/typescript-support";
29
32
  import { Reporter } from "../sentry/reporter";
@@ -124,15 +127,12 @@ async function main() {
124
127
 
125
128
  const argumentsParser = new ArgumentsParser();
126
129
 
127
- const {
128
- hardhatArguments,
129
- taskName: parsedTaskName,
130
- unparsedCLAs,
131
- } = argumentsParser.parseHardhatArguments(
132
- HARDHAT_PARAM_DEFINITIONS,
133
- envVariableArguments,
134
- process.argv.slice(2)
135
- );
130
+ const { hardhatArguments, scopeOrTaskName, allUnparsedCLAs } =
131
+ argumentsParser.parseHardhatArguments(
132
+ HARDHAT_PARAM_DEFINITIONS,
133
+ envVariableArguments,
134
+ process.argv.slice(2)
135
+ );
136
136
 
137
137
  if (hardhatArguments.verbose) {
138
138
  Reporter.setVerbose(true);
@@ -151,25 +151,45 @@ async function main() {
151
151
  return;
152
152
  }
153
153
 
154
- if (hardhatArguments.config === undefined && !isCwdInsideProject()) {
154
+ // ATTENTION! DEPRECATED CODE!
155
+ // The command `npx hardhat`, when used to create a new Hardhat project, will be removed with Hardhat V3.
156
+ // It will become `npx hardhat init`.
157
+ // The code marked with the tag #INIT-DEP can be deleted after HarhatV3 is out.
158
+
159
+ // Create a new Hardhat project
160
+ if (scopeOrTaskName === "init") {
161
+ return await createNewProject();
162
+ }
163
+ // #INIT-DEP - START OF DEPRECATED CODE
164
+ else {
155
165
  if (
156
- process.stdout.isTTY === true ||
157
- process.env.HARDHAT_CREATE_JAVASCRIPT_PROJECT_WITH_DEFAULTS !==
158
- undefined ||
159
- process.env.HARDHAT_CREATE_TYPESCRIPT_PROJECT_WITH_DEFAULTS !==
160
- undefined
166
+ scopeOrTaskName === undefined &&
167
+ hardhatArguments.config === undefined &&
168
+ !isCwdInsideProject()
161
169
  ) {
162
- await createProject();
170
+ await createNewProject();
171
+
172
+ // Warning for Hardhat V3 deprecation
173
+ console.warn(
174
+ chalk.yellow.bold("\n\nDEPRECATION WARNING\n\n"),
175
+ chalk.yellow(
176
+ `Initializing a project with ${chalk.white.italic(
177
+ "npx hardhat"
178
+ )} is deprecated and will be removed in the future.\n`
179
+ ),
180
+ chalk.yellow(
181
+ `Please use ${chalk.white.italic("npx hardhat init")} instead.\n\n`
182
+ )
183
+ );
184
+
163
185
  return;
164
186
  }
187
+ }
188
+ // #INIT-DEP - END OF DEPRECATED CODE
165
189
 
166
- // Many terminal emulators in windows fail to run the createProject()
167
- // workflow, and don't present themselves as TTYs. If we are in this
168
- // situation we throw a special error instructing the user to use WSL or
169
- // powershell to initialize the project.
170
- if (process.platform === "win32") {
171
- throw new HardhatError(ERRORS.GENERAL.NOT_INSIDE_PROJECT_ON_WINDOWS);
172
- }
190
+ // Tasks are only allowed inside a Hardhat project (except the init task)
191
+ if (hardhatArguments.config === undefined && !isCwdInsideProject()) {
192
+ throw new HardhatError(ERRORS.GENERAL.NOT_INSIDE_PROJECT);
173
193
  }
174
194
 
175
195
  if (
@@ -190,21 +210,29 @@ async function main() {
190
210
  }
191
211
  }
192
212
 
193
- let taskName = parsedTaskName ?? TASK_HELP;
194
-
195
- const showEmptyConfigWarning = true;
196
- const showSolidityConfigWarnings = taskName === TASK_COMPILE;
197
-
198
213
  const ctx = HardhatContext.createHardhatContext();
199
214
 
200
215
  const { resolvedConfig, userConfig } = loadConfigAndTasks(
201
216
  hardhatArguments,
202
217
  {
203
- showEmptyConfigWarning,
204
- showSolidityConfigWarnings,
218
+ showEmptyConfigWarning: true,
219
+ showSolidityConfigWarnings: scopeOrTaskName === TASK_COMPILE,
205
220
  }
206
221
  );
207
222
 
223
+ const envExtenders = ctx.environmentExtenders;
224
+ const providerExtenders = ctx.providerExtenders;
225
+ const taskDefinitions = ctx.tasksDSL.getTaskDefinitions();
226
+ const scopesDefinitions = ctx.tasksDSL.getScopesDefinitions();
227
+
228
+ // eslint-disable-next-line prefer-const
229
+ let { scopeName, taskName, unparsedCLAs } =
230
+ argumentsParser.parseScopeAndTaskNames(
231
+ allUnparsedCLAs,
232
+ taskDefinitions,
233
+ scopesDefinitions
234
+ );
235
+
208
236
  let telemetryConsent: boolean | undefined = hasConsentedTelemetry();
209
237
 
210
238
  const isHelpCommand = hardhatArguments.help || taskName === TASK_HELP;
@@ -229,22 +257,34 @@ async function main() {
229
257
  Reporter.setEnabled(true);
230
258
  }
231
259
 
232
- const envExtenders = ctx.environmentExtenders;
233
- const providerExtenders = ctx.providerExtenders;
234
- const taskDefinitions = ctx.tasksDSL.getTaskDefinitions();
235
-
236
260
  const [abortAnalytics, hitPromise] = await analytics.sendTaskHit();
237
261
 
238
262
  let taskArguments: TaskArguments;
239
263
 
240
264
  // --help is a also special case
241
265
  if (hardhatArguments.help && taskName !== TASK_HELP) {
242
- taskArguments = { task: taskName };
266
+ // we "move" the task and scope names to the task arguments,
267
+ // and run the help task
268
+ if (scopeName !== undefined) {
269
+ taskArguments = { scopeOrTask: scopeName, task: taskName };
270
+ } else {
271
+ taskArguments = { scopeOrTask: taskName };
272
+ }
243
273
  taskName = TASK_HELP;
274
+ scopeName = undefined;
244
275
  } else {
245
- const taskDefinition = taskDefinitions[taskName];
276
+ const taskDefinition = ctx.tasksDSL.getTaskDefinition(
277
+ scopeName,
278
+ taskName
279
+ );
246
280
 
247
281
  if (taskDefinition === undefined) {
282
+ if (scopeName !== undefined) {
283
+ throw new HardhatError(ERRORS.ARGUMENTS.UNRECOGNIZED_SCOPED_TASK, {
284
+ scope: scopeName,
285
+ task: taskName,
286
+ });
287
+ }
248
288
  throw new HardhatError(ERRORS.ARGUMENTS.UNRECOGNIZED_TASK, {
249
289
  task: taskName,
250
290
  });
@@ -266,6 +306,7 @@ async function main() {
266
306
  resolvedConfig,
267
307
  hardhatArguments,
268
308
  taskDefinitions,
309
+ scopesDefinitions,
269
310
  envExtenders,
270
311
  ctx.experimentalHardhatNetworkMessageTraceHooks,
271
312
  userConfig,
@@ -277,7 +318,7 @@ async function main() {
277
318
  try {
278
319
  const timestampBeforeRun = new Date().getTime();
279
320
 
280
- await env.run(taskName, taskArguments);
321
+ await env.run({ scope: scopeName, task: taskName }, taskArguments);
281
322
 
282
323
  const timestampAfterRun = new Date().getTime();
283
324
 
@@ -377,6 +418,33 @@ async function main() {
377
418
  }
378
419
  }
379
420
 
421
+ async function createNewProject() {
422
+ if (isCwdInsideProject()) {
423
+ throw new HardhatError(ERRORS.GENERAL.HARDHAT_PROJECT_ALREADY_CREATED, {
424
+ hardhatProjectRootPath: getUserConfigPath(),
425
+ });
426
+ }
427
+
428
+ if (
429
+ process.stdout.isTTY === true ||
430
+ process.env.HARDHAT_CREATE_JAVASCRIPT_PROJECT_WITH_DEFAULTS !== undefined ||
431
+ process.env.HARDHAT_CREATE_TYPESCRIPT_PROJECT_WITH_DEFAULTS !== undefined
432
+ ) {
433
+ await createProject();
434
+ return;
435
+ }
436
+
437
+ // Many terminal emulators in windows fail to run the createProject()
438
+ // workflow, and don't present themselves as TTYs. If we are in this
439
+ // situation we throw a special error instructing the user to use WSL or
440
+ // powershell to initialize the project.
441
+ if (process.platform === "win32") {
442
+ throw new HardhatError(ERRORS.GENERAL.NOT_INSIDE_PROJECT_ON_WINDOWS);
443
+ }
444
+
445
+ throw new HardhatError(ERRORS.GENERAL.NOT_IN_INTERACTIVE_SHELL);
446
+ }
447
+
380
448
  main()
381
449
  .then(() => process.exit(process.exitCode))
382
450
  .catch((error) => {
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  ActionType,
3
3
  ConfigExtender,
4
+ ConfigurableScopeDefinition,
4
5
  ConfigurableTaskDefinition,
5
6
  EnvironmentExtender,
6
7
  ExperimentalHardhatNetworkMessageTraceHook,
@@ -116,6 +117,16 @@ export function subtask<TaskArgumentsT extends TaskArguments>(
116
117
  // Backwards compatibility alias
117
118
  export const internalTask = subtask;
118
119
 
120
+ export function scope(
121
+ name: string,
122
+ description?: string
123
+ ): ConfigurableScopeDefinition {
124
+ const ctx = HardhatContext.getHardhatContext();
125
+ const dsl = ctx.tasksDSL;
126
+
127
+ return dsl.scope(name, description);
128
+ }
129
+
119
130
  export const types = argumentTypes;
120
131
 
121
132
  /**
@@ -288,6 +288,24 @@ To initialize the provider, make sure you first call \`.init()\` or any method t
288
288
  Please double check the file path.`,
289
289
  shouldBeReported: false,
290
290
  },
291
+ HARDHAT_PROJECT_ALREADY_CREATED: {
292
+ number: 23,
293
+ message:
294
+ "You are trying to initialize a project inside an existing Hardhat project. The path to the project's configuration file is: %hardhatProjectRootPath%.",
295
+ title: "Hardhat project already created",
296
+ description: `Cannot create a new Hardhat project, the current folder is already associated with a project.`,
297
+ shouldBeReported: false,
298
+ },
299
+ NOT_IN_INTERACTIVE_SHELL: {
300
+ number: 24,
301
+ message:
302
+ "You are trying to initialize a project but you are not in an interactive shell.",
303
+ title: "Not inside an interactive shell",
304
+ description: `You are trying to initialize a project but you are not in an interactive shell.
305
+
306
+ Please re-run the command inside an interactive shell.`,
307
+ shouldBeReported: false,
308
+ },
291
309
  },
292
310
  NETWORK: {
293
311
  CONFIG_NOT_FOUND: {
@@ -595,8 +613,26 @@ Please double check your task definitions.`,
595
613
  What makes these types special is that they can be represented as strings, so you can write them down in the terminal.`,
596
614
  shouldBeReported: false,
597
615
  },
598
- DEPRECATED_TRANSFORM_IMPORT_TASK: {
616
+ TASK_SCOPE_CLASH: {
599
617
  number: 213,
618
+ message:
619
+ "A clash was found while creating scope '%scopeName%', since a task with that name already exists.",
620
+ title: "Attempted to create a scope with a name already used by a task",
621
+ description: `You can't create a scope if a task with that name already exists.
622
+ Please double check your task definitions.`,
623
+ shouldBeReported: false,
624
+ },
625
+ SCOPE_TASK_CLASH: {
626
+ number: 214,
627
+ message:
628
+ "A clash was found while creating task '%taskName%', since a scope with that name already exists.",
629
+ title: "Attempted to create a task with a name already used by a scope",
630
+ description: `You can't create a task if a scope with that name already exists.
631
+ Please double check your task definitions.`,
632
+ shouldBeReported: false,
633
+ },
634
+ DEPRECATED_TRANSFORM_IMPORT_TASK: {
635
+ number: 215,
600
636
  title: "Use of deprecated remapping task",
601
637
  message:
602
638
  "Task TASK_COMPILE_TRANSFORM_IMPORT_NAME is deprecated. Please update your @nomicfoundation/hardhat-foundry plugin version.",
@@ -636,7 +672,7 @@ Please double check your arguments.`,
636
672
  },
637
673
  UNRECOGNIZED_TASK: {
638
674
  number: 303,
639
- message: "Unrecognized task %task%",
675
+ message: "Unrecognized task '%task%'",
640
676
  title: "Unrecognized task",
641
677
  description: `Tried to run a nonexistent task.
642
678
 
@@ -738,6 +774,24 @@ This is not supported. Please run the help task to see the available options.`,
738
774
  This flag can only be used in typescript projects.`,
739
775
  shouldBeReported: false,
740
776
  },
777
+ UNRECOGNIZED_SCOPE: {
778
+ number: 314,
779
+ message: "Unrecognized scope '%scope%'",
780
+ title: "Unrecognized scope",
781
+ description: `Tried to run a task from a nonexistent scope.
782
+
783
+ Please double check the scope of the task you are trying to run.`,
784
+ shouldBeReported: false,
785
+ },
786
+ UNRECOGNIZED_SCOPED_TASK: {
787
+ number: 315,
788
+ message: "Unrecognized task '%task%' under scope '%scope%'",
789
+ title: "Unrecognized scoped task",
790
+ description: `Tried to run a nonexistent scoped task.
791
+
792
+ Please double check the name of the task you are trying to run.`,
793
+ shouldBeReported: false,
794
+ },
741
795
  },
742
796
  RESOLVER: {
743
797
  FILE_NOT_FOUND: {
@@ -10,13 +10,14 @@ import {
10
10
  HardhatUserConfig,
11
11
  Network,
12
12
  ParamDefinition,
13
+ ProviderExtender,
13
14
  RunSuperFunction,
14
15
  RunTaskFunction,
15
16
  SubtaskArguments,
16
17
  TaskArguments,
17
18
  TaskDefinition,
18
19
  TasksMap,
19
- ProviderExtender,
20
+ ScopesMap,
20
21
  } from "../../types";
21
22
  import { Artifacts } from "../artifacts";
22
23
  import { MessageTrace } from "../hardhat-network/stack-traces/message-trace";
@@ -34,6 +35,7 @@ import {
34
35
  createTaskProfile,
35
36
  TaskProfile,
36
37
  } from "./task-profiling";
38
+ import { parseTaskIdentifier } from "./tasks/util";
37
39
 
38
40
  const log = debug("hardhat:core:hre");
39
41
 
@@ -65,6 +67,7 @@ export class Environment implements HardhatRuntimeEnvironment {
65
67
  * @param config The hardhat's config object.
66
68
  * @param hardhatArguments The parsed hardhat's arguments.
67
69
  * @param tasks A map of tasks.
70
+ * @param scopes A map of scopes.
68
71
  * @param environmentExtenders A list of environment extenders.
69
72
  * @param providerExtenders A list of provider extenders.
70
73
  */
@@ -72,6 +75,7 @@ export class Environment implements HardhatRuntimeEnvironment {
72
75
  public readonly config: HardhatConfig,
73
76
  public readonly hardhatArguments: HardhatArguments,
74
77
  public readonly tasks: TasksMap,
78
+ public readonly scopes: ScopesMap,
75
79
  environmentExtenders: EnvironmentExtender[] = [],
76
80
  experimentalHardhatNetworkMessageTraceHooks: ExperimentalHardhatNetworkMessageTraceHook[] = [],
77
81
  public readonly userConfig: HardhatUserConfig = {},
@@ -122,7 +126,7 @@ export class Environment implements HardhatRuntimeEnvironment {
122
126
  /**
123
127
  * Executes the task with the given name.
124
128
  *
125
- * @param name The task's name.
129
+ * @param taskIdentifier The task or scoped task to be executed.
126
130
  * @param taskArguments A map of task's arguments.
127
131
  * @param subtaskArguments A map of subtasks to their arguments.
128
132
  *
@@ -130,18 +134,39 @@ export class Environment implements HardhatRuntimeEnvironment {
130
134
  * @returns a promise with the task's execution result.
131
135
  */
132
136
  public readonly run: RunTaskFunction = async (
133
- name,
137
+ taskIdentifier,
134
138
  taskArguments = {},
135
139
  subtaskArguments = {},
136
140
  callerTaskProfile?: TaskProfile
137
141
  ) => {
138
- const taskDefinition = this.tasks[name];
142
+ const { scope, task } = parseTaskIdentifier(taskIdentifier);
143
+
144
+ let taskDefinition;
145
+ if (scope === undefined) {
146
+ taskDefinition = this.tasks[task];
147
+ log("Running task %s", task);
148
+ } else {
149
+ const scopeDefinition = this.scopes[scope];
150
+ if (scopeDefinition === undefined) {
151
+ throw new HardhatError(ERRORS.ARGUMENTS.UNRECOGNIZED_SCOPE, {
152
+ scope,
153
+ });
154
+ }
139
155
 
140
- log("Running task %s", name);
156
+ taskDefinition = scopeDefinition.tasks?.[task];
157
+ log("Running scoped task %s %s", scope, task);
158
+ }
141
159
 
142
160
  if (taskDefinition === undefined) {
161
+ if (scope !== undefined) {
162
+ throw new HardhatError(ERRORS.ARGUMENTS.UNRECOGNIZED_SCOPED_TASK, {
163
+ scope,
164
+ task,
165
+ });
166
+ }
167
+
143
168
  throw new HardhatError(ERRORS.ARGUMENTS.UNRECOGNIZED_TASK, {
144
- task: name,
169
+ task,
145
170
  });
146
171
  }
147
172
 
@@ -153,7 +178,7 @@ export class Environment implements HardhatRuntimeEnvironment {
153
178
 
154
179
  let taskProfile: TaskProfile | undefined;
155
180
  if (this.hardhatArguments.flamegraph === true) {
156
- taskProfile = createTaskProfile(name);
181
+ taskProfile = createTaskProfile(task);
157
182
 
158
183
  if (callerTaskProfile !== undefined) {
159
184
  callerTaskProfile.children.push(taskProfile);