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

fix: issues with 0.4 imports #122

Merged
merged 6 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 0 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ def all_versions():
return ALL_VERSIONS


@pytest.fixture
def cli_runner():
return CliRunner()


def _get_tb_contract(version: str, project, account):
project.load_contracts()

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was only working because of a weird glitch with non-local projects, as the test uses.
To ensure testing makes sense, I added an integration test to test_cli.py which uses the local project


# 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
15 changes: 15 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,16 @@ 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)
assert "SUCCESS" in completed_process.stdout
assert "zero_four_in_subdir.vy" in completed_process.stdout
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
Loading