Skip to content

Add --gas-stats-json global option for JSON gas stats export#8064

Merged
schaable merged 5 commits intomainfrom
gas-stats-json-output
Mar 18, 2026
Merged

Add --gas-stats-json global option for JSON gas stats export#8064
schaable merged 5 commits intomainfrom
gas-stats-json-output

Conversation

@schaable
Copy link
Copy Markdown
Member

@schaable schaable commented Mar 16, 2026

This PR adds a new --gas-stats-json <path> global option that writes gas usage statistics to a JSON file at the specified path. It works independently of --gas-stats (terminal table output), so either or both flags can be used together.

Docs: NomicFoundation/hardhat-website#238

Closes #7990.

@schaable schaable self-assigned this Mar 16, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 16, 2026

🦋 Changeset detected

Latest commit: 52b1a79

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

This PR includes changesets to release 4 packages
Name Type
@nomicfoundation/hardhat-node-test-runner Patch
@nomicfoundation/hardhat-mocha Patch
@nomicfoundation/hardhat-errors Patch
hardhat Minor

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

@schaable schaable requested a review from Copilot March 16, 2026 13:12
@schaable schaable review requested due to automatic review settings March 16, 2026 13:16
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

Adds support for exporting gas usage statistics from test runs to a JSON file via a new global CLI option, enabling CI/custom tooling workflows alongside (or instead of) the existing terminal table output.

Changes:

  • Introduces --gas-stats-json <path> global option and wires it into test flows (built-in test task, mocha runner, node test runner, solidity test runner).
  • Implements JSON generation and file writing in the gas analytics manager (including directory creation and invalid path handling).
  • Adds/updates tests, error descriptor, and release metadata (.changeset + peer bumps).

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
v-next/hardhat/test/internal/hre-initialization.ts Asserts the new global option is present in resolved HRE global options
v-next/hardhat/test/internal/cli/main.ts Updates expected global help output to include --gas-stats-json
v-next/hardhat/test/internal/builtin-plugins/gas-analytics/gas-analytics-manager.ts Adds unit tests for JSON generation and JSON file writing behavior
v-next/hardhat/src/internal/builtin-plugins/test/task-action.ts Ensures gas analytics collection/reporting can run for JSON export even when table output is disabled
v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts Enables gas measurement collection when JSON export is requested
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/types.ts Defines JSON output types and extends the manager interface with writeGasStatsJson
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/type-extensions.ts Extends GlobalOptions with gasStatsJson
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/index.ts Registers the new global option definition
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/test.ts Saves measurements and emits JSON output at test run end when requested
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/hre.ts Instantiates the gas analytics manager when either gas stats output is enabled
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/gas-analytics-manager.ts Implements JSON generation + writing with validation
v-next/hardhat-node-test-runner/src/task-action.ts Ensures worker-done hook is loaded when JSON export is requested
v-next/hardhat-mocha/src/task-action.ts Ensures worker-done hook is loaded when JSON export is requested
v-next/hardhat-errors/src/descriptors.ts Adds INVALID_FILE_PATH error used when output path is a directory
.peer-bumps.json Adds peer bump reasons for packages now reading gasStatsJson
.changeset/wet-apes-kick.md Declares release bumps for affected packages

Comment thread v-next/hardhat/src/internal/builtin-plugins/gas-analytics/type-extensions.ts Outdated
Base automatically changed from deployment-gas-stats to main March 18, 2026 14:17
Copilot AI review requested due to automatic review settings March 18, 2026 14:46
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 new global CLI option --gas-stats-json <path> to export gas usage statistics as JSON, enabling CI/custom tooling to consume the same data currently shown via --gas-stats terminal output.

Changes:

  • Add --gas-stats-json global option wiring (types, CLI help text, hooks/task actions).
  • Implement JSON generation and file writing in the gas analytics manager (including path validation).
  • Add/update tests and error descriptors to cover JSON export behavior and invalid paths.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated no comments.

Show a summary per file
File Description
v-next/hardhat/test/internal/hre-initialization.ts Updates expected default global options to include gasStatsJson.
v-next/hardhat/test/internal/cli/main.ts Updates CLI help output snapshots to include --gas-stats-json.
v-next/hardhat/test/internal/builtin-plugins/gas-analytics/gas-analytics-manager.ts Adds test coverage for JSON generation, sorting, and file output/validation behavior.
v-next/hardhat/src/internal/builtin-plugins/test/task-action.ts Ensures gas stats collection/reporting is enabled when JSON output is requested; writes JSON at end of aggregated test run.
v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts Enables gas report generation for Solidity tests when --gas-stats-json is used.
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/types.ts Defines JSON output types and extends manager interface with writeGasStatsJson.
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/type-extensions.ts Extends GlobalOptions with gasStatsJson?: string.
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/index.ts Registers the new global option with ArgumentType.FILE_WITHOUT_DEFAULT.
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/test.ts Collects measurements when JSON is requested; writes JSON at test-run completion.
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/hre.ts Instantiates gas analytics manager when either gas stats flag is set.
v-next/hardhat/src/internal/builtin-plugins/gas-analytics/gas-analytics-manager.ts Implements JSON generation and file writing with directory-path validation.
v-next/hardhat-utils/src/env.ts Expands env-global-options typing to allow undefined values.
v-next/hardhat-node-test-runner/src/task-action.ts Treats gasStatsJson as a trigger for test-worker wiring (like gasStats).
v-next/hardhat-mocha/src/task-action.ts Treats gasStatsJson as a trigger for test-worker wiring (like gasStats).
v-next/hardhat-errors/src/descriptors.ts Adds INVALID_FILE_PATH error used when output path is a directory.
.peer-bumps.json Records peer bumps required due to new global option usage in related packages.
.changeset/wet-apes-kick.md Adds release notes/versions for the new option across packages.

@schaable schaable added this pull request to the merge queue Mar 18, 2026
Merged via the queue into main with commit 6f37801 Mar 18, 2026
262 checks passed
@schaable schaable deleted the gas-stats-json-output branch March 18, 2026 15:05
@github-actions github-actions Bot mentioned this pull request Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support --json output for --gas-stats

3 participants