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

feat: add a built-in console task #5592

Merged
merged 13 commits into from
Aug 14, 2024
Merged

feat: add a built-in console task #5592

merged 13 commits into from
Aug 14, 2024

Conversation

galargh
Copy link
Member

@galargh galargh commented Aug 9, 2024

  • Because this PR includes a bug fix, relevant tests have been included.
  • Because this PR includes a new feature, the change was previously discussed on an Issue or with someone from the team.
  • I didn't do anything of this.

Resolves #5575

This PR adds a console task to the next version of hardhat. Here's how it looks like:

$ # Show console task's help
$ 
$ pnpm hardhat console --help
Opens a hardhat console

Usage: hardhat [GLOBAL OPTIONS] console [--history <STRING>] [--no-compile] [--] [commands]

OPTIONS:

  --history         Path to a history file
  --no-compile      Don't compile before running this task

POSITIONAL ARGUMENTS:

  commands          Commands to run in the console

For global options help run: hardhat --help
$ 
$ # Run the console task and preview the `hre` context variable
$ 
$ pnpm hardhat console
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Break, and also clear the local context
.editor   Enter editor mode
.exit     Exit the REPL
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file

Press Ctrl+C to abort current expression, Ctrl+D to exit the REPL
> hre
HardhatRuntimeEnvironmentImplementation { userConfig: { tasks: [Array], plugins: [Array], privateKey: [Object] }, config: { plugins: [Array], tasks: [Array], path...

The task includes the following options:

  • history - allows specifying the path where REPL history file should be stored. If it's relative, it will be resolved in the context of global hardhat cache dir. If it is explicitly set to an empty string, the history will not be preserved. It defaults to console-history.txt in the global hardhat cache dir. This is very helpful during testing, but I thought it might be useful to the users, too.
  • noCompile - disables compilation task. This is not implemented yet.
  • options - This is a hidden argument. It is not exposed to the users. It is only used during testing to set custom REPL options. In particular, we override the IO streams during testing because otherwise the test runner hangs indefinitely.
  • commands - This is a variadic argument. One can provide a list of commands that should be executed in the REPL after the startup. It defaults to running the .help command after startup. This is very helpful during testing because we want to run the commands after the entire REPL setup fully complete there, but I thought it might be useful to the users, too.

Testing

  • Added tests for the newly created task action; they are passing in CI
  • Manually run the newly added task in the example-project; pnpm hardhat console

Copy link

changeset-bot bot commented Aug 9, 2024

⚠️ No Changeset found

Latest commit: 1870719

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link

vercel bot commented Aug 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 14, 2024 2:00pm

@github-actions github-actions bot added the status:ready This issue is ready to be worked on label Aug 9, 2024
@schaable schaable added the v-next A Hardhat v3 development task label Aug 9, 2024
@alcuadrado
Copy link
Member

I did an initial review despite this being a draft. Looking good! I just left some minor comments.

Copy link
Member Author

@galargh galargh left a comment

Choose a reason for hiding this comment

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

Thanks for the initial review! I usually tend to use drafts for work-in-progress proposals which is pending my self-review. Here, it turned out not to be too far off from the final "ready for review" state ;)

Comment on lines +158 to +165
// We try to remove the temporary cache dir after each test, but we don't
// fail the test if it fails. For example, we have observed that in GHA
// on Windows, the temp dir cannot be removed due to permission issues.
try {
await remove(cacheDir);
} catch (error) {
log("Failed to remove temporary cache dir", error);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I added a try-catch and a comment about an issue I observed in https://github.com/NomicFoundation/hardhat/actions/runs/10332073748/job/28602920453, for example. Have you seen something similar before? Do you think it's worth ensuring we can always delete the temp dirs in CI? In this particular case I think it might have something to do that the temp dir is on C: while the runner is set up to have perms and execute in the context of drive D:.

Copy link
Member

Choose a reason for hiding this comment

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

that's actually sirprising, but I think we can live with it.

Maybe we should move it to the test utils package so that we don't hit this edge case again.

Copy link
Member Author

Choose a reason for hiding this comment

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

I created an issue for this - #5602 If I cannot resolve it, I'm going to add the utility function for this.

@galargh galargh marked this pull request as ready for review August 12, 2024 08:55
@galargh galargh requested a review from alcuadrado August 12, 2024 08:55
Comment on lines +16 to +17
// We accept ReplOptions as an argument to allow tests overriding the IO streams
options?: repl.ReplOptions;
Copy link
Member

Choose a reason for hiding this comment

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

This is really interesting! I never thought about it.

Copy link
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.

This looks good! :)

I left a few comments, mostly about things that can be done later. Do you mind creating issues so that we don't loose track of them?

Copy link
Member

@schaable schaable left a comment

Choose a reason for hiding this comment

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

Nothing to add to Pato's comments. This looks great, nice work!

@galargh galargh added this pull request to the merge queue Aug 14, 2024
Merged via the queue into v-next with commit 98e98bf Aug 14, 2024
12 checks passed
@galargh galargh deleted the galargh/console branch August 14, 2024 14:05
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status:ready This issue is ready to be worked on v-next A Hardhat v3 development task
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants