Skip to content

Commit

Permalink
Merge pull request #515 from crytic/fix/config-path
Browse files Browse the repository at this point in the history
fix: use target to get config, drop toml for json
  • Loading branch information
0xalpharush authored Jan 16, 2024
2 parents bb7af17 + 2b1526a commit ad6dedc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 57 deletions.
80 changes: 25 additions & 55 deletions crytic_compile/platform/foundry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import os
import subprocess
from pathlib import Path
from typing import TYPE_CHECKING, List, Optional, Dict, TypeVar
from typing import TYPE_CHECKING, List, Optional, TypeVar

import toml
import json

from crytic_compile.platform.abstract_platform import AbstractPlatform, PlatformConfig
from crytic_compile.platform.types import Type
Expand Down Expand Up @@ -63,7 +63,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
compile_all = kwargs.get("foundry_compile_all", False)

if not compile_all:
foundry_config = self.config(str(crytic_compile.working_dir.absolute()))
foundry_config = self.config(self._target)
if foundry_config:
compilation_command += [
"--skip",
Expand Down Expand Up @@ -122,64 +122,34 @@ def config(working_dir: str) -> Optional[PlatformConfig]:
"""Return configuration data that should be passed to solc, such as remappings.
Args:
working_dir (str): path to the working directory
working_dir (str): path to the working_dir
Returns:
Optional[PlatformConfig]: Platform configuration data such as optimization, remappings...
"""
result = PlatformConfig()
result.remappings = (
subprocess.run(["forge", "remappings"], stdout=subprocess.PIPE, check=True)
.stdout.decode("utf-8")
.replace("\n", " ")
.strip()
LOGGER.info("'forge config --json' running")
json_config = json.loads(
subprocess.run(
["forge", "config", "--json"], cwd=working_dir, stdout=subprocess.PIPE, check=True
).stdout
)
with open("foundry.toml", "r", encoding="utf-8") as f:
foundry_toml = toml.loads(f.read())
default_profile = foundry_toml["profile"]["default"]

def lookup_by_keys(keys: List[str], dictionary: Dict[str, T]) -> Optional[T]:
for key in keys:
if key in dictionary:
return dictionary[key]
return None

# Foundry supports snake and kebab case.
result.solc_version = lookup_by_keys(
["solc", "solc_version", "solc-version"], default_profile
)
via_ir = lookup_by_keys(["via_ir", "via-ir"], default_profile)
if via_ir:
result.via_ir = via_ir
result.allow_paths = lookup_by_keys(["allow_paths", "allow-paths"], default_profile)

if "offline" in default_profile:
result.offline = default_profile["offline"]
if "optimizer" in default_profile:
result.optimizer = default_profile["optimizer"]
else:
# Default to true
result.optimizer = True
optimizer_runs = lookup_by_keys(["optimizer_runs", "optimizer-runs"], default_profile)
if optimizer_runs is None:
# Default to 200
result.optimizer_runs = 200
else:
result.optimizer_runs = optimizer_runs
evm_version = lookup_by_keys(["evm_version", "evm-version"], default_profile)
if evm_version is None:
result.evm_version = evm_version
else:
# Default to london
result.evm_version = "london"
if "src" in default_profile:
result.src_path = default_profile["src"]
if "test" in default_profile:
result.tests_path = default_profile["test"]
if "libs" in default_profile:
result.libs_path = default_profile["libs"]
if "script" in default_profile:
result.scripts_path = default_profile["script"]

# Solc configurations
result.solc_version = json_config.get("solc")
result.via_ir = json_config.get("via_ir")
result.allow_paths = json_config.get("allow_paths")
result.offline = json_config.get("offline")
result.evm_version = json_config.get("evm_version")
result.optimizer = json_config.get("optimizer")
result.optimizer_runs = json_config.get("optimizer_runs")
result.remappings = json_config.get("remappings")

# Foundry project configurations
result.src_path = json_config.get("src")
result.tests_path = json_config.get("test")
result.libs_path = json_config.get("libs")
result.scripts_path = json_config.get("script")

return result

Expand Down
11 changes: 10 additions & 1 deletion scripts/ci_test_foundry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ forge init --no-commit
crytic-compile .
if [ $? -ne 0 ]
then
echo "foundry test failed"
echo "foundry test 1 failed"
exit 255
fi

mkdir /tmp/forge_test/test_2
rsync -a --exclude='test_2' ./ /tmp/forge_test/test_2/
crytic-compile ./test_2
if [ $? -ne 0 ]
then
echo "foundry test 2 failed"
exit 255
fi
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
version="0.3.4",
packages=find_packages(),
python_requires=">=3.8",
install_requires=["pycryptodome>=3.4.6", "cbor2", "solc-select>=v1.0.4", "toml>=0.10.2"],
install_requires=["pycryptodome>=3.4.6", "cbor2", "solc-select>=v1.0.4"],
extras_require={
"test": [
"pytest",
Expand Down

0 comments on commit ad6dedc

Please sign in to comment.