Skip to content

Expose Result type for task action success/failure signaling#8012

Merged
schaable merged 13 commits intomainfrom
task-result
Mar 2, 2026
Merged

Expose Result type for task action success/failure signaling#8012
schaable merged 13 commits intomainfrom
task-result

Conversation

@schaable
Copy link
Copy Markdown
Member

@schaable schaable commented Feb 26, 2026

Summary

  • Expose Result interface that task actions can optionally return to signal success or failure to the CLI
  • When task.run() returns a Result with success: false, the CLI sets process.exitCode = 1
  • Both success and failure can optionally carry a value
  • Backward compatible: tasks that return undefined or non-Result values continue to work unchanged
  • Exports the isResult() type guard and successfulResult() / errorResult() factory helpers from hardhat/utils/result

Usage

import type { Result } from "hardhat/types/result";
import { successfulResult, errorResult } from "hardhat/utils/result";

// Success without data (e.g. keystore, telemetry)
async function configureAction(): Promise<Result<void, void>> {
  if (conflictingFlags) {
    return errorResult();
  }
  // ...
  return successfulResult();
}

// Success/failure with data (e.g. test runners)
interface TestSummary {
  failed: number;
  passed: number;
}

async function testAction(): Promise<Result<TestSummary, TestSummary>> {
  const summary = await runTests();

  if (summary.failed > 0) {
    return errorResult(summary); // CLI sets exitCode=1, caller still gets data
  }

  return successfulResult(summary);
}

Docs: NomicFoundation/hardhat-website#228

@schaable schaable requested a review from alcuadrado February 26, 2026 19:50
@schaable schaable self-assigned this Feb 26, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 26, 2026

🦋 Changeset detected

Latest commit: db636ad

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
hardhat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread v-next/hardhat/package.json Outdated
@schaable schaable added no changeset needed This PR doesn't require a changeset no docs needed This PR doesn't require links to documentation no peer bump needed and removed no changeset needed This PR doesn't require a changeset labels Feb 26, 2026
@alcuadrado
Copy link
Copy Markdown
Member

I'll review this better tomorrow, but I think using taskSuccess and the other helpers from non-builtin plugins would force us to peer-bump all of them, for little user-value. Other than that, I like the general idea. They can achieve the same type-safety without the helpers though.

Comment thread v-next/hardhat/src/types/tasks.ts Outdated
@schaable schaable changed the title Add TaskResult type for task action success/failure signaling Expose Result type for task action success/failure signaling Feb 28, 2026
Comment thread v-next/hardhat/package.json Outdated
@schaable schaable requested a review from alcuadrado February 28, 2026 17:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a public Result type (plus isResult, successResult, and errorResult) so task actions can explicitly signal success/failure to the Hardhat CLI, enabling the CLI to set process.exitCode = 1 when a task returns a failing result.

Changes:

  • Add Result type + isResult() guard and successResult() / errorResult() helpers, and export them via package subpaths.
  • Update CLI main to detect failing Result returns from tasks and set process.exitCode = 1.
  • Refactor Solidity resolver code/tests to use the shared Result type and add new tests/fixture coverage for CLI behavior.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
v-next/hardhat/src/types/result.ts Adds the new public Result type.
v-next/hardhat/src/utils/result.ts Adds successResult, errorResult, and isResult.
v-next/hardhat/src/types/index.ts Re-exports the new Result type from the types barrel.
v-next/hardhat/src/internal/cli/main.ts Sets process.exitCode = 1 when a task returns a failing Result.
v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/resolver/types.ts Removes local Result definition and imports shared Result.
v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/resolver/utils.ts Updates Result import to shared public type.
v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/resolver/remapped-npm-packages-graph.ts Updates Result import to shared public type.
v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/resolver/dependency-resolver.ts Updates Result import to shared public type.
v-next/hardhat/package.json Exposes ./types/result and ./utils/result export subpaths.
v-next/hardhat/test/utils/result.ts Adds unit tests for factories and isResult.
v-next/hardhat/test/internal/cli/main.ts Adds CLI tests asserting process.exitCode behavior based on returned Result.
v-next/hardhat/test/fixture-projects/cli/result/hardhat.config.ts Adds fixture tasks returning various Result/non-Result values.
v-next/hardhat/test/internal/builtin-plugins/solidity/build-system/resolver/dependency-resolver.ts Updates tests to use shared Result type.
.changeset/nice-parrots-do.md Adds a changeset entry for the patch release.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat/src/utils/result.ts
Comment thread v-next/hardhat/test/utils/result.ts Outdated
Comment thread v-next/hardhat/src/internal/cli/main.ts
Comment thread v-next/hardhat/test/internal/cli/main.ts
Comment thread v-next/hardhat/src/utils/result.ts Outdated
Copy link
Copy Markdown
Member

@alcuadrado alcuadrado left a comment

Choose a reason for hiding this comment

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

There are some minor comments about naming, exports and a test, but pre-approving.

@alcuadrado
Copy link
Copy Markdown
Member

Also: on could create an issue to change all the builtin plugins' tasks to use Result, and update their tests so that they don't depend on process.exitCode

@alcuadrado
Copy link
Copy Markdown
Member

And we should update the docs of setAction and setInlineAction, both in the website and the jsdoc.

Comment thread v-next/hardhat/test/internal/cli/main.ts
Comment thread v-next/hardhat/src/types/tasks.ts
@schaable
Copy link
Copy Markdown
Member Author

schaable commented Mar 2, 2026

Also: on could create an issue to change all the builtin plugins' tasks to use Result, and update their tests so that they don't depend on process.exitCode

This is already done in #8015

And we should update the docs of setAction and setInlineAction, both in the website and the jsdoc

Done in db636ad and NomicFoundation/hardhat-website#228

@schaable schaable removed the no docs needed This PR doesn't require links to documentation label Mar 2, 2026
@schaable schaable enabled auto-merge March 2, 2026 21:42
@schaable schaable added this pull request to the merge queue Mar 2, 2026
Merged via the queue into main with commit 49e4776 Mar 2, 2026
216 of 218 checks passed
@schaable schaable deleted the task-result branch March 2, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants