NomicFoundation / hardhat

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

Supporting circular npm dependencies #3388

Open alcuadrado opened 1 year ago

alcuadrado commented 1 year ago

If you import a contract that belongs to the project from another package, Hardhat fails.

1509 makes Hardhat throw a clear error in that case, but I found a way to actually support it.

If when compiling an npm/hardhat project P, instead of throwing here, we validate that this resolves to the same package P (i.e. there aren't multiple versions of P installed) and resolve to the local source file, we can then make it work by using remappings.

In my test I got this to compile:

└── packages
    ├── main
    │   ├── contracts
    │   │   ├── Entry.sol // imports "other/contracts/O.sol"
    │   │   └── M.sol
    │   ├── hardhat.config.js
    │   └── package.json
    └── other
        ├── contracts
        │   └── O.sol // imports "main/contracts/M.sol"
        ├── hardhat.config.js
        └── package.json

using the remapping "main/=".

/cc @nventuro @fvictorio

github-actions[bot] commented 1 year ago

This issue is also being tracked on Linear.

We use Linear to manage our development process, but we keep the conversations on Github.

LINEAR-ID: 64af9cf5-0cb3-4ad8-b2ed-0ba7eca9910f

alcuadrado commented 1 year ago

The same technique allows us to support importing a contract from its own package using the package name. For example, packages/main/contracts/Entry.sol importing "main/contract/M.sol", and even itself.

alcuadrado commented 1 year ago

@KaanKC you may find this interesting too!