NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.04k stars 1.36k forks source link

Hardhat compiles npm packages imports using local package's node_modules #5493

Closed andrewwahid closed 1 week ago

andrewwahid commented 2 weeks ago

Version of Hardhat

2.22.6

What happened?

When I import contract A.sol from "@npm-package/contracts/A.sol" if A.sol imports B.sol from "@npm-package2/contracts/B.sol" I expect that hardhat uses/compiles B.sol using "project/node_modules/@npm-package/node_modules/@npm-package2/contracts/B.sol" But what happens is that hardhat compiles B.sol using "project/node_modules/@npm-package2/contracts/B.sol", which causes conflicts when multiple npm packages imports same contracts but in different versions

Minimal reproduction steps

  1. Create empty hardhat project
  2. Add these to package.json dependencies
    "account-abstraction-06": "git+https://github.com/eth-infinitism/account-abstraction.git#v0.6.0",
    "account-abstraction-07": "git+https://github.com/eth-infinitism/account-abstraction.git#v0.7.0",

    Please note that account-abstraction-06 depends on @openzeppelin/contracts": "^4.2.0 And account-abstraction-07 depends on @openzeppelin/contracts": "^5.0.0

  3. yarn install
  4. add import "account-abstraction-07/contracts/core/BasePaymaster.sol"; to Lock.sol
  5. hardhat compile You'll find that compilation fails because it tries to use OZ's Ownable.sol for a different version than the one located in "project/node_modules/account-abstraction-07/node_modules/openzeppelin/contracts/access/Ownable.sol"

Search terms

conflict, versions, compile, imports

kanej commented 1 week ago

I am closing this as a duplicate of #2811.

This is a known bug that is difficult for us to fix due to the way Hardhat leverages solc in combination with node_modules.

We are currently working on a new major version of Hardhat that properly supports this.