hardhat-tracer: Checking harhat version by requiring `@nomicfoundation/ethereumjs-vm` package leads to errors

hardhat version: 2.18.0 hardhat-tracer version: 2.7.0

Problem

No matter the version of HardHat, I keep having the following error:

ERROR

Please upgrade your hardhat version to 2.11 or above.
This error is generated by plugin "hardhat-tracer" because it is 
dependent on some features available in hardhat >=2.11.0 <3.0.0.

npm i hardhat@latest

or

yarn add hardhat@latest

What I understand

I understand that this is comming from this part of the code:

try {
  // Try to check if hardhat version is compatible by checking if the package is available
  require("@nomicfoundation/ethereumjs-vm");
} catch {
...

Since the package is not defined in hardhat-tracer’s dependencies, I kinda expect ts to throw the following error:

Your application tried to access @nomicfoundation/ethereumjs-vm, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound

Which leads to the error

Potential solution

Adding the package as a dependency might be a solution

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 20 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for reporting. The issue should be fixed in 3.2.1.

More details: I was using an internal dependency used by hardhat “@nomicfoundation/ethereumjs-util”, since I am depending on some of the internal modules in hardhat. At some point hardhat moved to importing same things from “@ethereumjs/util”, but since code in this repo wasn’t updated, it started giving the import issue.

Adding that as a peer dependency is not a solution as first it is not suitable in this case, and even if it were suitable, dependency renames like this in hardhat would also require change in plugin. Ideal solution would have been hardhat re-exporting those things. If I come across the same issue in hardhat v3 then I’ll make a PR for it or something.

Just wanted to add that I’m experiencing the same issue, but with @nomicfoundation/ethereumjs-util.

I’m using hardhat-tracer 3.2.0 (checked for all versions >=3.0.0) in a project, and got a runtime error:

Error: Cannot find module '@nomicfoundation/ethereumjs-util'

Adding it manually fixes the problem:

yarn add -D @nomicfoundation/ethereumjs-util

Maybe worth checking if it’s used in your code and adding it conditionally like for ethereumjs-vm, or at least documenting it in the README as a peer dependency or workaround.

Just released hardhat-tracer@3.0.1, that should work with hardhat>=2.22.5.

Oh sorry, it will work with 2.23 (unreleased), this is a mistake from my side for releasing early, they actually have a pre-release of that minor version at hardhat@2.23.0-dev.2.

I also need to add a check ensuring hardhat version should be >2.22. Will release a fix once hardhat dev branch is released. Will update here once done.

Just released hardhat-tracer@3.0.0. Hardhat now officially exposes APIs which this plugin needed, so this issue should be resolved. Whenever you get time please let me know if that is true.

Right, thanks for taking time to explain this, I learned something!

For now, let’s consider this solution enough then!

When single @nomicfoundation/ethereumjs-vm exists, then npm puts it on the top level node_modules.

├── node_modules
│   ├── @nomicfoundation/ethereumjs-vm
│   ├── hardhat
│   ├── hardhat-tracer
├── package.json

This way, hardhat-tracer when it requires @nomicfoundation/ethereumjs-vm, the module resolution still works, even if it’s actually not declared as a dependency. I understand that is causing issues with modern module resolutions which work differently.

If the suggestion is implemented, it is possible that versions differ and that would cause the following (if user is upgrading hardhat and there’s a new patch version available for VM):

├── node_modules
│   ├── hardhat
│   │   ├── node_modules
│   │       ├── @nomicfoundation/ethereumjs-vm (v7.0.4)
│   ├── hardhat-tracer
│       ├── node_modules
│           ├── @nomicfoundation/ethereumjs-vm (v7.0.3)
├── package.json

Since VM object of v7.0.3 is updated by hardhat-tracer, it would not effect the VM v7.0.4 (for example) used by hardhat.

Also note that after adding a dependency, the require check would pass 100% of the time, because a @nomicfoundation/ethereumjs-vm would exist in the node_modules, even if user had hardhat 2.10 or lower (which used a different package for VM) or the new hardhat with EDR.

Does this make sense?

Ideally, you define @nomicfoundation/ethereumjs-vm as a dependency because you import Vm from it (and any package you’re importing from should be declared as a dependency of this package)

The v1 of this plugin was slow due to debug_tt overheads. In the current v2 of this plugin I had to use a non-standard apis from hardhat, basically hijack the JS VM that hardhat is using. Unfortunately I cannot add it as a dependency because it does not allow me to do some hacks.

But I am taking help from hardhat team to officially expose the apis that are required for this plugin to work and if it works out well, it will solve this problem.

Same issue here. I wanted to use this project where I also use huff-deployer.

The huff-deployer package doesn’t work with the latest Hardhat version (since they switched to Ethers 6) so I am using Hardhat 2.14 which repends on ethers@5.7.0

@oumar-fall is this the case for you too?

Sure, if you find any patch that allows this plugin to work with yarn v3/v4, happy to add it here / merge your PR!