Skip to content

Commit

Permalink
fix: path resolution issues
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 20, 2024
1 parent ceeaebf commit a6a2eb7
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions ape_vyper/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def get_pcmap(
):
return _get_pcmap(bytecode)

def parse_source_map(self, raw_source_map: Any) -> SourceMap:
# All versions < 0.4 use this one
return SourceMap(root=raw_source_map)

def get_default_optimization(self, vyper_version: Version) -> Optimization:
return True

Expand Down Expand Up @@ -216,16 +220,17 @@ def get_sources_dictionary(
continue

imp_path = Path(imp)
abs_import = Path(imp).is_absolute()

if (pm.path / imp).is_file():
if not abs_import and (pm.path / imp).is_file():
# Is a local file.
imp_path = pm.path / imp
if not imp_path.is_file():
continue

src_dict[imp] = {"content": imp_path.read_text(encoding="utf8")}

else:
elif not abs_import:
# Is from a dependency.
specified = {d.name: d for d in pm.dependencies.specified}
for parent in imp_path.parents:
Expand All @@ -242,7 +247,7 @@ def get_sources_dictionary(

# Likely from a dependency. Exclude absolute prefixes so Vyper
# knows what to do.
if imp_path.is_file():
if imp_path.is_file() and not Path(src_id).is_absolute():
src_dict[src_id] = {"content": imp_path.read_text(encoding="utf8")}

return src_dict
Expand All @@ -255,6 +260,9 @@ def get_compile_kwargs(
def get_default_optimization(self, vyper_version: Version) -> Optimization:
return "gas"

def parse_source_map(self, raw_source_map: dict) -> SourceMap:
return SourceMap(root=raw_source_map["pc_pos_map_compressed"])


class VyperCompiler(CompilerAPI):
@property
Expand Down Expand Up @@ -303,12 +311,14 @@ def _get_imports(
contract_filepaths: Iterable[Path],
project: Optional[ProjectManager] = None,
handled: Optional[set[str]] = None,
use_absolute_paths: Optional[None] = None,
):
pm = project or self.local_project

# When compiling projects outside the cwd, we must
# use absolute paths.
use_absolute_paths = pm.path != Path.cwd()
if use_absolute_paths is None:
# When compiling projects outside the cwd, we must
# use absolute paths.
use_absolute_paths = pm.path != Path.cwd()

import_map: defaultdict = defaultdict(list)
handled = handled or set()
Expand Down Expand Up @@ -441,6 +451,7 @@ def _get_imports(
(source_path,),
project=imported_project,
handled=handled,
use_absolute_paths=use_absolute_paths,
)
for sub_import_ls in sub_imports.values():
import_map[source_id].extend(sub_import_ls)
Expand Down Expand Up @@ -491,6 +502,7 @@ def _get_imports(
(import_path,),
project=dep_project,
handled=handled,
use_absolute_paths=use_absolute_paths,
)
for sub_import_ls in sub_imports.values():
import_map[source_id].extend(sub_import_ls)
Expand All @@ -509,7 +521,13 @@ def _get_imports(
full_path = local_path.parent / f"{local_path.stem}{ext}"

# Also include imports of imports.
sub_imports = self._get_imports((full_path,), project=pm, handled=handled)
sub_imports = self._get_imports(
(full_path,),
project=pm,
handled=handled,
use_absolute_paths=use_absolute_paths,
)

for sub_import_ls in sub_imports.values():
import_map[source_id].extend(sub_import_ls)

Expand Down Expand Up @@ -734,10 +752,8 @@ def compile(
)
log_str = f"Compiling using Vyper compiler '{vyper_version}'.\nInput:\n\t{keys}"
logger.info(log_str)

vyper_binary = compiler_data[vyper_version]["vyper_binary"]
comp_kwargs = sub_compiler.get_compile_kwargs(
vyper_binary, compiler_data, project=pm
vyper_version, compiler_data, project=pm
)
try:
result = vvm_compile_standard(input_json, **comp_kwargs)
Expand Down Expand Up @@ -770,17 +786,7 @@ def compile(
evm = output["evm"]
bytecode = evm["deployedBytecode"]
opcodes = bytecode["opcodes"].split(" ")

src_map_raw = bytecode["sourceMap"]
if isinstance(src_map_raw, str):
# <0.4 range.
compressed_src_map = SourceMap(root=src_map_raw)
else:
# >=0.4 range.
compressed_src_map = SourceMap(
root=src_map_raw["pc_pos_map_compressed"]
)

compressed_src_map = sub_compiler.parse_source_map(bytecode["sourceMap"])
src_map = list(compressed_src_map.parse())[1:]
pcmap = sub_compiler.get_pcmap(
vyper_version, ast, src_map, opcodes, bytecode
Expand Down

0 comments on commit a6a2eb7

Please sign in to comment.