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

increase observability and provide suggestions/clearer errors #444

Merged
merged 5 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 7 additions & 29 deletions crytic_compile/platform/foundry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"""
import logging
import os
import shutil
import subprocess
from pathlib import Path
from typing import TYPE_CHECKING, List

Expand Down Expand Up @@ -51,34 +49,14 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
)

if not ignore_compile:
cmd = [
"forge",
"build",
"--build-info",
]

LOGGER.info(
"'%s' running",
" ".join(cmd),
)

with subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
run(
[
"forge",
"build",
"--build-info",
],
cwd=self._target,
executable=shutil.which(cmd[0]),
) as process:

stdout_bytes, stderr_bytes = process.communicate()
stdout, stderr = (
stdout_bytes.decode(errors="backslashreplace"),
stderr_bytes.decode(errors="backslashreplace"),
) # convert bytestrings to unicode strings

LOGGER.info(stdout)
if stderr:
LOGGER.error(stderr)
)

build_directory = Path(
self._target,
Expand Down
32 changes: 8 additions & 24 deletions crytic_compile/platform/hardhat.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ def hardhat_like_parsing(
InvalidCompilation: If hardhat failed to run

"""
if not os.path.isdir(build_directory):
txt = (
f"Compilation failed. Can you run build command?\n{build_directory} is not a directory."
)
raise InvalidCompilation(txt)

files = sorted(
os.listdir(build_directory), key=lambda x: os.path.getmtime(Path(build_directory, x))
)
files = [str(f) for f in files if str(f).endswith(".json")]
if not files:
txt = f"`compile` failed. Can you run it?\n{build_directory} is empty"
txt = f"Compilation failed. Can you run build command?\n{build_directory} is empty."
raise InvalidCompilation(txt)

for file in files:
Expand Down Expand Up @@ -168,29 +174,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:

if not hardhat_ignore_compile:
cmd = base_cmd + ["compile", "--force"]

LOGGER.info(
"'%s' running",
" ".join(cmd),
)

with subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self._target,
executable=shutil.which(cmd[0]),
) as process:

stdout_bytes, stderr_bytes = process.communicate()
stdout, stderr = (
stdout_bytes.decode(errors="backslashreplace"),
stderr_bytes.decode(errors="backslashreplace"),
) # convert bytestrings to unicode strings

LOGGER.info(stdout)
if stderr:
LOGGER.error(stderr)
run(cmd, cwd=self._target)

hardhat_like_parsing(crytic_compile, self._target, build_directory, hardhat_working_dir)

Expand Down