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

compiler remappings fails if same name path #893

Open
eyooooo opened this issue Dec 16, 2020 · 6 comments
Open

compiler remappings fails if same name path #893

eyooooo opened this issue Dec 16, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@eyooooo
Copy link

eyooooo commented Dec 16, 2020

Environment information

  • brownie Version: 1.12.1
  • ganache-cli Version: x.x.x
  • solc Version: x.x.x
  • Python Version: 2.7.17 or 3.6.9
  • OS: Ubuntu 18.04.5 LTS

What was wrong?

  1. forked ESD
  2. init new brownie dir
  3. moved contracts
  4. ESD uses truffle npm style imports so need to perform remappings
  5. install packages: brownie pm install OpenZeppelin/[email protected] brownie pm install uniswap/[email protected] brownie pm install uniswap/[email protected]
  6. set remappings:
        remappings: 
            - "@openzeppelin=OpenZeppelin/[email protected]"
            - "@uniswap/lib=uniswap/[email protected]"
            - "@uniswap/v2-core=uniswap/[email protected]"
  1. error on compile
contracts/oracle/Oracle.sol:21:1: ParserError: Source "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol" not found: File outside of allowed directories.
import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';

If I flip the two uniswap remappings, the 2nd one always fails. eg)

        remappings: 
            - "@openzeppelin=OpenZeppelin/[email protected]"
            - "@uniswap/v2-core=uniswap/[email protected]"
            - "@uniswap/lib=uniswap/[email protected]"

will produce errors

contracts/mock/MockUniswapV2PairTrade.sol:20:1: ParserError: Source "uniswap/[email protected]/contracts/libraries/FixedPoint.sol" not found: File not found.

How can it be fixed?

regex? it appears the remapping ignores the info after the / so uniswap/lib and uniswap/v2-core are just treated identically. the 2nd entry will always fail.

@eyooooo
Copy link
Author

eyooooo commented Dec 17, 2020

the issue is in /usr/local/lib/python3.6/dist-packages/brownie/project/compiler/__init__.py in func _get_solc_remappings. im seeing if i can patch it.

@eyooooo
Copy link
Author

eyooooo commented Dec 17, 2020

This is a hacky workaround

def _get_solc_remappings(remappings: Optional[list]) -> list:
    if remappings is None:
        remap_dict: Dict = {}
    elif isinstance(remappings, str):
        remap_dict = dict([remappings.split("=")])
    else:
        remap_dict = dict(i.split("=") for i in remappings)

    #hacky crap here
    for path in _get_data_folder().joinpath("packages").iterdir():
        for k,v in remap_dict.items():
            remap_dict[k] = path.parent.joinpath(v).as_posix()
    #print([f"{k}={v}" for k, v in remap_dict.items()])       

    for path in _get_data_folder().joinpath("packages").iterdir():
        key = next((k for k, v in remap_dict.items() if v.startswith(path.name)), None) 
        if key:
            remap_dict[key] = path.parent.joinpath(remap_dict[key]).as_posix()
        else:
            remap_dict[path.name] = path.as_posix()

    return [f"{k}={v}" for k, v in remap_dict.items()]

@iamdefinitelyahuman iamdefinitelyahuman added the bug Something isn't working label Dec 17, 2020
@iamdefinitelyahuman
Copy link
Member

Thanks for the workaround, I'll have a look here shortly.

For reference: https://docs.soliditylang.org/en/latest/using-the-compiler.html#path-remapping

@Pavel-Blaize
Copy link

I have reproduced the same bug. I think can formalize it in the next way:

  • add 2 (or more) repositories from the same git account (root of the path) into dependecies and/or remappings. Also at least one contract should use items from the first repository and at least one more contract should use items from the second repository. After the compilation, items from all repositories except the first will not be found and will fail the compilation.

For example I have:

    remappings:
      - "@openzeppelinV3=OpenZeppelin/[email protected]"
      - "@ozUpgradesV3=OpenZeppelin/[email protected]"

So both packages has the same OpenZeppelin/ root.
One contract of mine imports Ownable from the regular package, and another contract imports OwnableUpgradeable from the upgradable package. After the compilation I get an error:

ParserError: Source "OpenZeppelin/[email protected]/contracts/access/OwnableUpgradeable.sol" not found: File not found.
import "@ozUpgradesV3/contracts/access/OwnableUpgradeable.sol";
^-------------------------------------------------------------^

Issue reproduces with different order of packages, different imports, different truffle or solc versions. Brownie version 1.12.2

Though, issue does not reproduce, when I manually clone all the packages except the first (brownie pm clone "OpenZeppelin/[email protected]").

@utgarda
Copy link

utgarda commented Jul 7, 2021

Totally reproducing:

compiler:
  solc:
    version: 0.8.3
    remappings:
      - "@openzeppelin-upgradeable=/home/username/.brownie/packages/OpenZeppelin/[email protected]"
      - "@openzeppelin=OpenZeppelin/[email protected]"

the above works, but if you remap as just

compiler:
  solc:
    version: 0.8.3
    remappings:
      - "@openzeppelin-upgradeable=OpenZeppelin/[email protected]"
      - "@openzeppelin=OpenZeppelin/[email protected]"

i.e. if 2 remappings have the same path prefix, it's compiler errors "File not found" all over the place, like

ParserError: Source "OpenZeppelin/[email protected]/contracts/access/Ownable.sol" not found: File not found.

@veloboot
Copy link

This worked for me:

  - "@openzeppelin/contracts=OpenZeppelin/[email protected]/contracts"
  - "@openzeppelin/contracts-upgradeable=OpenZeppelin/[email protected]/contracts"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants