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[lang]: disallow absolute relative imports #4268

Merged
merged 36 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
84f9c70
fix absolute relative paths
sandbubbles Oct 1, 2024
0a69fca
lint
sandbubbles Oct 1, 2024
57f7abd
fix path from absolute to relative
sandbubbles Oct 3, 2024
0db6330
add test case for correct relative path
sandbubbles Oct 3, 2024
b1dee66
make relative paths work again
sandbubbles Oct 3, 2024
bc7d569
fix that relatives paths could break out of current directory
sandbubbles Oct 5, 2024
64ed8c3
add test for relative paths breaking out of current directory
sandbubbles Oct 5, 2024
7e3e20a
Merge branch 'master' into fix/relative-paths
sandbubbles Oct 5, 2024
6207819
merge master
sandbubbles Oct 15, 2024
0463a79
add _load_file with modifies search paths to a relative path or all a…
sandbubbles Oct 15, 2024
932e09a
add tests
sandbubbles Oct 15, 2024
67cb7d3
add resolved paths to import tests
sandbubbles Oct 15, 2024
052e895
lint
sandbubbles Oct 15, 2024
a4edbe9
revert back to old load_file
sandbubbles Oct 15, 2024
32eb376
fuse _load_import with _load_import_helper
sandbubbles Oct 15, 2024
e20b381
lint
sandbubbles Oct 15, 2024
a843639
fix typo and remove multiline expr
sandbubbles Oct 15, 2024
b98ea67
remove newly redundant methods
sandbubbles Oct 15, 2024
d75c292
lint
sandbubbles Oct 15, 2024
141ed2f
Merge branch 'master' into fix/relative-paths
charles-cooper Oct 16, 2024
f2ca3c8
Merge branch 'master' into fix/relative-paths
charles-cooper Oct 17, 2024
5ddc9b5
fix tests
sandbubbles Oct 21, 2024
6a7d5cc
remove redundancy and fixup
sandbubbles Oct 21, 2024
de79ba5
move function down
sandbubbles Oct 21, 2024
e4d6c62
remove newline
sandbubbles Oct 21, 2024
ec686f8
restore search paths after loading file
sandbubbles Oct 22, 2024
85e7eea
merge master
sandbubbles Nov 7, 2024
f0f325a
change "+=" to "append"
sandbubbles Dec 11, 2024
c91f13e
Merge branch 'master' into fix/relative-paths
sandbubbles Dec 11, 2024
a140226
clean up recursion
charles-cooper Dec 13, 2024
99f0af2
fix lint
charles-cooper Dec 13, 2024
3359eae
clean up test
charles-cooper Dec 13, 2024
f67dee8
minor clean up for some tests
charles-cooper Dec 13, 2024
69ce28e
some more test cleanup
charles-cooper Dec 13, 2024
0ad5e7f
clean up tests some more
sandbubbles Dec 14, 2024
be01034
Merge branch 'master' into fix/relative-paths
charles-cooper Dec 17, 2024
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
88 changes: 88 additions & 0 deletions tests/functional/syntax/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import pytest

from vyper import compiler
from vyper.exceptions import ModuleNotFound


def test_implicitly_relative_import_crashes(make_input_bundle):
top = """
import subdir0.lib0 as lib0

@external
def foo():
lib0.foo()
"""

lib0 = """
import subdir1.lib1 as lib1

def foo():
lib1.foo()
"""

lib1 = """

def foo():
pass
"""

input_bundle = make_input_bundle(
{"top.vy": top, "subdir0/lib0.vy": lib0, "subdir0/subdir1/lib1.vy": lib1}
)

with pytest.raises(ModuleNotFound):
compiler.compile_code(top, input_bundle=input_bundle)

lib0 = """
from subdir1 import lib1 as lib1

def foo():
lib1.foo()
"""

input_bundle = make_input_bundle(
{"top.vy": top, "subdir0/lib0.vy": lib0, "subdir0/subdir1/lib1.vy": lib1}
)

with pytest.raises(ModuleNotFound):
compiler.compile_code(top, input_bundle=input_bundle)


def test_absolute_path_passes(make_input_bundle):
top = """
import subdir0.lib0 as lib0

@external
def foo():
lib0.foo()
"""

lib0 = """
import subdir0.subdir1.lib1 as lib1

def foo():
lib1.foo()
"""

lib1 = """

def foo():
pass
"""

input_bundle = make_input_bundle(
{"top.vy": top, "subdir0/lib0.vy": lib0, "subdir0/subdir1/lib1.vy": lib1}
)
compiler.compile_code(top, input_bundle=input_bundle)

lib0 = """
from .subdir1 import lib1 as lib1

def foo():
lib1.foo()
"""

input_bundle = make_input_bundle(
{"top.vy": top, "subdir0/lib0.vy": lib0, "subdir0/subdir1/lib1.vy": lib1}
)
compiler.compile_code(top, input_bundle=input_bundle)
2 changes: 1 addition & 1 deletion tests/unit/cli/vyper_json/test_compile_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,5 @@ def get(filename, contractname):
def test_relative_import_paths(input_json):
input_json["sources"]["contracts/potato/baz/baz.vy"] = {"content": "from ... import foo"}
input_json["sources"]["contracts/potato/baz/potato.vy"] = {"content": "from . import baz"}
input_json["sources"]["contracts/potato/footato.vy"] = {"content": "from baz import baz"}
input_json["sources"]["contracts/potato/footato.vy"] = {"content": "from .baz import baz"}
compile_from_input_dict(input_json)
5 changes: 4 additions & 1 deletion vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import os
from pathlib import Path, PurePath
from typing import Any, Optional
Expand Down Expand Up @@ -792,7 +793,9 @@ def _load_import(self, node: vy_ast.VyperNode, level: int, module_str: str, alia
# the directory this (currently being analyzed) module is in
self_search_path = Path(self.ast.resolved_path).parent

with self.input_bundle.poke_search_path(self_search_path):
with self.input_bundle.search_path(
self_search_path
) if level != 0 else contextlib.nullcontext():
return self._load_import_helper(node, level, module_str, alias)

def _load_import_helper(
Expand Down
Loading