Skip to content

Commit ad6dedc

Browse files
authored
Merge pull request #515 from crytic/fix/config-path
fix: use target to get config, drop toml for json
2 parents bb7af17 + 2b1526a commit ad6dedc

File tree

3 files changed

+36
-57
lines changed

3 files changed

+36
-57
lines changed

crytic_compile/platform/foundry.py

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import os
66
import subprocess
77
from pathlib import Path
8-
from typing import TYPE_CHECKING, List, Optional, Dict, TypeVar
8+
from typing import TYPE_CHECKING, List, Optional, 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",
@@ -122,64 +122,34 @@ def config(working_dir: 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+
working_dir (str): path to the working_dir
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(
134+
["forge", "config", "--json"], cwd=working_dir, stdout=subprocess.PIPE, check=True
135+
).stdout
136136
)
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"]
137+
138+
# Solc configurations
139+
result.solc_version = json_config.get("solc")
140+
result.via_ir = json_config.get("via_ir")
141+
result.allow_paths = json_config.get("allow_paths")
142+
result.offline = json_config.get("offline")
143+
result.evm_version = json_config.get("evm_version")
144+
result.optimizer = json_config.get("optimizer")
145+
result.optimizer_runs = json_config.get("optimizer_runs")
146+
result.remappings = json_config.get("remappings")
147+
148+
# Foundry project configurations
149+
result.src_path = json_config.get("src")
150+
result.tests_path = json_config.get("test")
151+
result.libs_path = json_config.get("libs")
152+
result.scripts_path = json_config.get("script")
183153

184154
return result
185155

scripts/ci_test_foundry.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ forge init --no-commit
1212
crytic-compile .
1313
if [ $? -ne 0 ]
1414
then
15-
echo "foundry test failed"
15+
echo "foundry test 1 failed"
1616
exit 255
1717
fi
18+
19+
mkdir /tmp/forge_test/test_2
20+
rsync -a --exclude='test_2' ./ /tmp/forge_test/test_2/
21+
crytic-compile ./test_2
22+
if [ $? -ne 0 ]
23+
then
24+
echo "foundry test 2 failed"
25+
exit 255
26+
fi

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)