Skip to content

Commit

Permalink
restore search paths after loading file
Browse files Browse the repository at this point in the history
  • Loading branch information
sandbubbles committed Oct 22, 2024
1 parent e4d6c62 commit ec686f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
10 changes: 10 additions & 0 deletions vyper/compiler/input_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ def search_path(self, path: Optional[PathLike]) -> Iterator[None]:
finally:
self.search_paths.pop()

# temporarily set search paths to a given list
@contextlib.contextmanager
def temporary_search_paths(self, new_paths: list[PathLike]) -> Iterator[None]:
original_paths = self.search_paths
self.search_paths = new_paths
try:
yield
finally:
self.search_paths = original_paths


# regular input. takes a search path(s), and `load_file()` will search all
# search paths for the file and read it from the filesystem
Expand Down
22 changes: 13 additions & 9 deletions vyper/semantics/analysis/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,19 @@ def _load_import(
search_paths = self.input_bundle.search_paths.copy() # noqa: F841
raise ModuleNotFound(module_str, hint=hint) from err

def _load_file(self, path: PathLike, level: int):
if level == 0:
self.input_bundle.search_paths = self.absolute_search_paths
else:
ast = self.graph.current_module
current_search_path = Path(ast.resolved_path).parent
self.input_bundle.search_paths = [current_search_path]

return self.input_bundle.load_file(path)
def _load_file(self, path: PathLike, level: int) -> CompilerInput:
ast = self.graph.current_module
current_search_path = Path(ast.resolved_path).parent

search_paths = self.absolute_search_paths if level == 0 else [current_search_path]

with self.input_bundle.temporary_search_paths(search_paths):
res = self.input_bundle.load_file(path)

if level != 0:
self.input_bundle.search_paths += [current_search_path]

return res

def _ast_from_file(self, file: FileInput) -> vy_ast.Module:
# cache ast if we have seen it before.
Expand Down

0 comments on commit ec686f8

Please sign in to comment.