Skip to content

Commit

Permalink
fix: issues with 0.4 imports (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jul 2, 2024
1 parent 7d6afeb commit e2636e1
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 11 deletions.
14 changes: 11 additions & 3 deletions ape_vyper/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,22 @@ def _get_imports(
dots += prefix[0]
prefix = prefix[1:]

is_relative = dots != ""

# Replace rest of dots with slashes.
prefix = prefix.replace(".", os.path.sep)

if prefix.startswith("vyper/"):
if prefix.startswith("vyper/") or prefix.startswith("ethereum/"):
if f"{prefix}.json" not in import_map[source_id]:
import_map[source_id].append(f"{prefix}.json")

continue

local_path = (path.parent / dots / prefix.lstrip(os.path.sep)).resolve()
local_path = (
(path.parent / dots / prefix.lstrip(os.path.sep)).resolve()
if is_relative
else (pm.path / prefix.lstrip(os.path.sep)).resolve()
)
local_prefix = str(local_path).replace(f"{pm.path}", "").lstrip(os.path.sep)

import_source_id = None
Expand Down Expand Up @@ -1235,12 +1241,14 @@ def _to_src_id(s):
search_paths = [*getsitepackages()]
if pm.path == Path.cwd():
search_paths.append(".")
else:
search_paths.append(str(pm.path))
# else: only seem to get absolute paths to work (for compiling deps alone).

version_settings[settings_key] = {
"optimize": optimization,
"outputSelection": selection_dict,
"search_paths": [".", *getsitepackages()],
"search_paths": search_paths,
}
if evm_version and evm_version not in ("none", "null"):
version_settings[settings_key]["evmVersion"] = f"{evm_version}"
Expand Down
4 changes: 2 additions & 2 deletions tests/contracts/passing_contracts/flatten_me.vy
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from vyper.interfaces import ERC20

from interfaces import IFace2 as IFaceTwo
import interfaces.IFace as IFace
from .interfaces import IFace2 as IFaceTwo
from .interfaces import IFace as IFace
import exampledependency.Dependency as Dep


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Show we can import from the root of the project w/o needing relative imports
from contracts.passing_contracts import zero_four_module as zero_four_module

@external
def callModuleFunctionFromSubdir(role: bytes32) -> bool:
return zero_four_module.moduleMethod()
4 changes: 2 additions & 2 deletions tests/contracts/passing_contracts/use_iface.vy
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# @version ^0.3.3

# Import a local interface.
import interfaces.IFace as IFace
from .interfaces import IFace as IFace

# Import from input JSON (ape-config.yaml).
import exampledependency.Dependency as Dep

from interfaces import IFace2 as IFace2
from .interfaces import IFace2 as IFace2


@external
Expand Down
2 changes: 1 addition & 1 deletion tests/contracts/passing_contracts/use_iface2.vy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @version ^0.3.3

from interfaces import IFace as IFace
from .interfaces import IFace as IFace


@external
Expand Down
6 changes: 5 additions & 1 deletion tests/contracts/passing_contracts/zero_four.vy
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# pragma version ~=0.4.0

import interfaces.IFaceZeroFour as IFaceZeroFour
from .interfaces import IFaceZeroFour as IFaceZeroFour
implements: IFaceZeroFour

from . import zero_four_module as zero_four_module

from snekmate.auth import ownable

# Also show we can import from ethereum namespace.
# (new in Vyper 0.4).
from ethereum.ercs import IERC20

@external
@view
def implementThisPlease(role: bytes32) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/contracts/templates/reverts.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @version {{VYPER_VERSION}}

import interfaces.ISubReverts as ISubReverts
from .interfaces import ISubReverts as ISubReverts

sub_reverts: public(ISubReverts)
MAX_NUM: constant(uint256) = 32
Expand Down
2 changes: 1 addition & 1 deletion tests/contracts/templates/traceback_contract.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @version {{VYPER_VERSION}}

import interfaces.IRegistry as IRegistry
from .interfaces import IRegistry as IRegistry

_balance: public(uint256)
registry: public(IRegistry)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import subprocess

import pytest
from ape.utils import create_tempdir

Expand Down Expand Up @@ -31,3 +33,17 @@ def test_cli_flatten(project, contract_name, expected, cli_runner):
output = file.read_text()
for expect in expected:
assert expect in output


def test_compile():
"""
Integration: Testing the CLI using an actual subprocess because
it is the only way to test compiling the project such that it
isn't treated as a tempdir project.
"""
# Use a couple contracts
cmd_ls = ("ape", "compile", "subdir", "--force")
completed_process = subprocess.run(cmd_ls, capture_output=True)
output = completed_process.stdout.decode(encoding="utf8")
assert "SUCCESS" in output
assert "zero_four_in_subdir.vy" in output
13 changes: 13 additions & 0 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ def test_compile_failures(contract_name, compiler):
assert isinstance(err.value.base_err, VyperError)


def test_compile_zero_four(compiler, project):
"""
An easy way to test only Vyper 0.4 changes.
"""
paths = (
project.contracts_folder / "subdir" / "zero_four_in_subdir.vy",
project.contracts_folder / "zero_four.vy",
)
result = [x.name for x in compiler.compile(paths, project=project)]
assert "zero_four" in result
assert "zero_four_in_subdir" in result


def test_install_failure(compiler):
failing_project = ape.Project(FAILING_BASE)
path = FAILING_BASE / "contract_unknown_pragma.vy"
Expand Down

0 comments on commit e2636e1

Please sign in to comment.