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

[Cli parsing] When you run npx hardhat task unexpected-argument it doesn't fail if task is an empty task #6401

Open
wants to merge 7 commits into
base: v-next
Choose a base branch
from
6 changes: 6 additions & 0 deletions .changeset/short-beans-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need a double check on this changeset, is this format acceptable for the v-next?

"@nomicfoundation/hardhat-errors": patch
"hardhat": patch
---

Added a check to throw an error if a non-existing subtask is executed
9 changes: 9 additions & 0 deletions v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ Please double check your task arguments.`,

Please double check your arguments.`,
},
UNRECOGNIZED_SUBTASK: {
number: 416,
messageTemplate:
'Invalid subtask "{invalidSubtask}" for the task "{task}"',
websiteTitle: "Invalid subtask value",
websiteDescription: `The subtask for the task you provided is invalid.

Please double check your subtask.,`,
},
},
ARGUMENTS: {
INVALID_VALUE_FOR_TYPE: {
Expand Down
12 changes: 12 additions & 0 deletions v-next/hardhat/src/internal/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ export async function main(

const task = taskOrId;

if (task.isEmpty && usedCliArguments.includes(false)) {
const invalidSubtask = cliArguments[usedCliArguments.indexOf(false)];

throw new HardhatError(
HardhatError.ERRORS.TASK_DEFINITIONS.UNRECOGNIZED_SUBTASK,
{
task: task.id.join(" "),
invalidSubtask,
},
);
}

if (builtinGlobalOptions.help || task.isEmpty) {
const taskHelp = await getHelpString(task);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HardhatUserConfig } from "../../../../../src/config.js";

import { task } from "../../../../../src/config.js";
import { emptyTask, task } from "../../../../../src/config.js";

export const tasksResults = {
wasArg1Used: false,
Expand Down Expand Up @@ -42,6 +42,8 @@ const customTask2 = task("task-default")
})
.build();

const customTask3 = emptyTask("task-default-3", "description").build();

const customSubtask = task(["task", "subtask"])
.addOption({ name: "arg1", defaultValue: "<default-value1>" })
.addPositionalArgument({ name: "arg2" })
Expand Down Expand Up @@ -70,8 +72,19 @@ const customSubtask2 = task(["task-default", "subtask-default"])
})
.build();

const customSubtask3 = task(["task-default-3", "subtask-default-3"])
.setAction(() => {})
.build();

const config: HardhatUserConfig = {
tasks: [customTask, customTask2, customSubtask, customSubtask2],
tasks: [
customTask,
customTask2,
customTask3,
customSubtask,
customSubtask2,
customSubtask3,
],
};

export default config;
34 changes: 34 additions & 0 deletions v-next/hardhat/test/internal/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ describe("main", function () {
});
});

describe("task wit non existing subtask", function () {
useFixtureProject("cli/parsing/tasks-and-subtasks");

it("should throw because the subtask does not exist", async function () {
const command = "npx hardhat task-default-3 nonExistingTask";

await assertRejectsWithHardhatError(
() => runMain(command),
HardhatError.ERRORS.TASK_DEFINITIONS.UNRECOGNIZED_SUBTASK,
{
task: "task-default-3",
invalidSubtask: "nonExistingTask",
},
);
});
});

describe("global help", function () {
useFixtureProject("cli/parsing/base-project");

Expand Down Expand Up @@ -272,6 +289,23 @@ Usage: hardhat [GLOBAL OPTIONS] empty-task <SUBTASK> [SUBTASK OPTIONS] [--] [SUB
});
});

describe("subtask does not exist", () => {
useFixtureProject("cli/parsing/tasks-and-subtasks");

it("should throw because the help option cannot be used on a non-existent subtask", async function () {
const command = "npx hardhat task-default-3 nonExistingTask --help";

await assertRejectsWithHardhatError(
() => runMain(command),
HardhatError.ERRORS.TASK_DEFINITIONS.UNRECOGNIZED_SUBTASK,
{
task: "task-default-3",
invalidSubtask: "nonExistingTask",
},
);
});
});

describe("task with default values", () => {
useFixtureProject("cli/parsing/default-values");

Expand Down
Loading