eth-brownie / brownie

A Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine.
https://eth-brownie.readthedocs.io
MIT License
2.65k stars 553 forks source link

Flattener remapping relative to source file instead of project root #1474

Closed mliudev closed 7 months ago

mliudev commented 2 years ago

Environment information

What was wrong?

In order to debug why I couldn't publish the source of my contracts to Etherscan I ran:

brownie console --network rinkeby

>>> MyContract.get_verification_info()
  File "<console>", line 1, in <module>
  File "/Users/foo/git/brownie-fork/brownie/network/contract.py", line 299, in get_verification_info
    self._flattener = Flattener(source_fp, self._name, remaps, compiler_settings)
  File "/Users/foo/git/brownie-fork/brownie/project/flattener.py", line 30, in __init__
    self.traverse(primary_source_fp)
  File "/Users/foo/git/brownie-fork/brownie/project/flattener.py", line 71, in traverse
    self.traverse(import_path)
  File "/Users/foo/git/brownie-fork/brownie/project/flattener.py", line 50, in traverse
    source = fp_obj.read_text()
  File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 1266, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 1252, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 1120, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/<redacted>/backend/contracts/manifold-extensions/lib/manifoldxyz/libraries-solidity/contracts/access/AdminControlUpgradeable.sol'

I have the following remappings:

brownie-config.yaml

compiler:
    evm_version: london
    solc:
        remappings:
            - "@openzeppelin/=lib/openzeppelin-contracts@4.5.0/"
            - "@manifoldxyz/=lib/manifoldxyz/"
            - "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable@4.5.2/contracts/"

My folder structure is:

.
├── build
│   ├── contracts
│   ├── deployments
│   │   └── 4
│   └── interfaces
├── contracts
│   ├── BS.sol
│   ├── ERC721Creator.sol
│   └── manifold-extensions
│       └── LazyMint.sol
├── git-ignore
├── interfaces
├── lib
│   ├── manifoldxyz
│   │   ├── creator-core-extensions-solidity
│   │   ├── creator-core-solidity
│   │   └── libraries-solidity
│   ├── openzeppelin-contracts-upgradeable@4.5.2
│   │   └── contracts
│   └── openzeppelin-contracts@4.5.0
│       ├── contracts
│       └── contracts-upgradeable

Brownie successfully compiles the contracts given the same remappings so it seems to me that the Flattener remaps imports relative to the source file instead of the project root like the compiler does. In my case the Flattener tries to traverse an import that's supposed to be remapped to ./lib/manifoldxyz instead of ./contracts/manifold-extensions/lib/manifoldxyz.

Relevant code appears to be

https://github.com/eth-brownie/brownie/blob/master/brownie/project/flattener.py#L127.

How can it be fixed?

Presumably by fixing the flattener to flatten the same way the compiler does. While looking through the tests, it seems that the verification test passes with imports that aren't remapped elsewhere in the project structure.

mliudev commented 2 years ago

Possibly related:

Found similar issue and fix:

mliudev commented 7 months ago

No longer using this.