Skip to content

Commit 826758e

Browse files
committed
fix: use target to get config, drop toml for json
1 parent e4b529b commit 826758e

File tree

2 files changed

+50
-56
lines changed

2 files changed

+50
-56
lines changed

crytic_compile/platform/foundry.py

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pathlib import Path
88
from typing import TYPE_CHECKING, List, Optional, Dict, TypeVar
99

10-
import toml
10+
import json
1111

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

6565
if not compile_all:
66-
foundry_config = self.config(str(crytic_compile.working_dir.absolute()))
66+
foundry_config = self.config(self._target)
6767
if foundry_config:
6868
compilation_command += [
6969
"--skip",
@@ -118,68 +118,62 @@ def is_supported(target: str, **kwargs: str) -> bool:
118118
return os.path.isfile(os.path.join(target, "foundry.toml"))
119119

120120
@staticmethod
121-
def config(working_dir: str) -> Optional[PlatformConfig]:
121+
def config(target: str) -> Optional[PlatformConfig]:
122122
"""Return configuration data that should be passed to solc, such as remappings.
123123
124124
Args:
125-
working_dir (str): path to the working directory
125+
target (str): path to the target
126126
127127
Returns:
128128
Optional[PlatformConfig]: Platform configuration data such as optimization, remappings...
129129
"""
130130
result = PlatformConfig()
131-
result.remappings = (
132-
subprocess.run(["forge", "remappings"], stdout=subprocess.PIPE, check=True)
133-
.stdout.decode("utf-8")
134-
.replace("\n", " ")
135-
.strip()
131+
LOGGER.info("'forge config --json' running")
132+
json_config = json.loads(
133+
subprocess.run(["forge", "config", "--json"], stdout=subprocess.PIPE, check=True).stdout
136134
)
137-
with open("foundry.toml", "r", encoding="utf-8") as f:
138-
foundry_toml = toml.loads(f.read())
139-
default_profile = foundry_toml["profile"]["default"]
140-
141-
def lookup_by_keys(keys: List[str], dictionary: Dict[str, T]) -> Optional[T]:
142-
for key in keys:
143-
if key in dictionary:
144-
return dictionary[key]
145-
return None
146-
147-
# Foundry supports snake and kebab case.
148-
result.solc_version = lookup_by_keys(
149-
["solc", "solc_version", "solc-version"], default_profile
150-
)
151-
via_ir = lookup_by_keys(["via_ir", "via-ir"], default_profile)
152-
if via_ir:
153-
result.via_ir = via_ir
154-
result.allow_paths = lookup_by_keys(["allow_paths", "allow-paths"], default_profile)
155-
156-
if "offline" in default_profile:
157-
result.offline = default_profile["offline"]
158-
if "optimizer" in default_profile:
159-
result.optimizer = default_profile["optimizer"]
160-
else:
161-
# Default to true
162-
result.optimizer = True
163-
optimizer_runs = lookup_by_keys(["optimizer_runs", "optimizer-runs"], default_profile)
164-
if optimizer_runs is None:
165-
# Default to 200
166-
result.optimizer_runs = 200
167-
else:
168-
result.optimizer_runs = optimizer_runs
169-
evm_version = lookup_by_keys(["evm_version", "evm-version"], default_profile)
170-
if evm_version is None:
171-
result.evm_version = evm_version
172-
else:
173-
# Default to london
174-
result.evm_version = "london"
175-
if "src" in default_profile:
176-
result.src_path = default_profile["src"]
177-
if "test" in default_profile:
178-
result.tests_path = default_profile["test"]
179-
if "libs" in default_profile:
180-
result.libs_path = default_profile["libs"]
181-
if "script" in default_profile:
182-
result.scripts_path = default_profile["script"]
135+
result.remappings = json_config["remappings"]
136+
137+
def lookup_by_keys(keys: List[str], dictionary: Dict[str, T]) -> Optional[T]:
138+
for key in keys:
139+
if key in dictionary:
140+
return dictionary[key]
141+
return None
142+
143+
# Foundry supports snake and kebab case.
144+
result.solc_version = lookup_by_keys(["solc", "solc_version", "solc-version"], json_config)
145+
via_ir = lookup_by_keys(["via_ir", "via-ir"], json_config)
146+
if via_ir:
147+
result.via_ir = via_ir
148+
result.allow_paths = lookup_by_keys(["allow_paths", "allow-paths"], json_config)
149+
150+
if "offline" in json_config:
151+
result.offline = json_config["offline"]
152+
if "optimizer" in json_config:
153+
result.optimizer = json_config["optimizer"]
154+
else:
155+
# Default to true
156+
result.optimizer = True
157+
optimizer_runs = lookup_by_keys(["optimizer_runs", "optimizer-runs"], json_config)
158+
if optimizer_runs is None:
159+
# Default to 200
160+
result.optimizer_runs = 200
161+
else:
162+
result.optimizer_runs = optimizer_runs
163+
evm_version = lookup_by_keys(["evm_version", "evm-version"], json_config)
164+
if evm_version is None:
165+
result.evm_version = evm_version
166+
else:
167+
# Default to london
168+
result.evm_version = "london"
169+
if "src" in json_config:
170+
result.src_path = json_config["src"]
171+
if "test" in json_config:
172+
result.tests_path = json_config["test"]
173+
if "libs" in json_config:
174+
result.libs_path = json_config["libs"]
175+
if "script" in json_config:
176+
result.scripts_path = json_config["script"]
183177

184178
return result
185179

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
version="0.3.4",
1515
packages=find_packages(),
1616
python_requires=">=3.8",
17-
install_requires=["pycryptodome>=3.4.6", "cbor2", "solc-select>=v1.0.4", "toml>=0.10.2"],
17+
install_requires=["pycryptodome>=3.4.6", "cbor2", "solc-select>=v1.0.4"],
1818
extras_require={
1919
"test": [
2020
"pytest",

0 commit comments

Comments
 (0)