|
7 | 7 | import shutil
|
8 | 8 | import subprocess
|
9 | 9 | from pathlib import Path
|
10 |
| -from typing import TYPE_CHECKING, List |
| 10 | +from typing import TYPE_CHECKING, Dict, List, Optional, Union |
11 | 11 |
|
12 | 12 | from crytic_compile.compiler.compiler import CompilerVersion
|
13 | 13 | from crytic_compile.platform.exceptions import InvalidCompilation
|
@@ -49,7 +49,7 @@ def hardhat_like_parsing(
|
49 | 49 | )
|
50 | 50 | files = [str(f) for f in files if str(f).endswith(".json")]
|
51 | 51 | if not files:
|
52 |
| - txt = f"`hardhat compile` failed. Can you run it?\n{build_directory} is empty" |
| 52 | + txt = f"`compile` failed. Can you run it?\n{build_directory} is empty" |
53 | 53 | raise InvalidCompilation(txt)
|
54 | 54 |
|
55 | 55 | for file in files:
|
@@ -157,16 +157,20 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
|
157 | 157 | "ignore_compile", False
|
158 | 158 | )
|
159 | 159 |
|
160 |
| - build_directory = Path( |
161 |
| - self._target, kwargs.get("hardhat_artifacts_directory", "artifacts"), "build-info" |
162 |
| - ) |
163 |
| - |
164 |
| - hardhat_working_dir: str = kwargs.get("hardhat_working_dir", self._target) |
165 |
| - |
166 | 160 | base_cmd = ["hardhat"]
|
167 | 161 | if not kwargs.get("npx_disable", False):
|
168 | 162 | base_cmd = ["npx"] + base_cmd
|
169 | 163 |
|
| 164 | + detected_paths = self._get_hardhat_paths(base_cmd, kwargs) |
| 165 | + |
| 166 | + build_directory = Path( |
| 167 | + self._target, |
| 168 | + detected_paths["artifacts"], |
| 169 | + "build-info", |
| 170 | + ) |
| 171 | + |
| 172 | + hardhat_working_dir = str(Path(self._target, detected_paths["root"])) |
| 173 | + |
170 | 174 | if not hardhat_ignore_compile:
|
171 | 175 | cmd = base_cmd + ["compile", "--force"]
|
172 | 176 |
|
@@ -235,3 +239,76 @@ def _guessed_tests(self) -> List[str]:
|
235 | 239 | List[str]: The guessed unit tests commands
|
236 | 240 | """
|
237 | 241 | return ["hardhat test"]
|
| 242 | + |
| 243 | + def _get_hardhat_paths( |
| 244 | + self, base_cmd: List[str], args: Dict[str, str] |
| 245 | + ) -> Dict[str, Union[Path, str]]: |
| 246 | + """Obtain hardhat configuration paths, defaulting to the |
| 247 | + standard config if needed. |
| 248 | +
|
| 249 | + Args: |
| 250 | + base_cmd ([str]): hardhat command |
| 251 | + args (Dict[str, str]): crytic-compile options that may affect paths |
| 252 | +
|
| 253 | + Returns: |
| 254 | + Dict[str, str]: hardhat paths configuration |
| 255 | + """ |
| 256 | + target_path = Path(self._target) |
| 257 | + default_paths = { |
| 258 | + "root": target_path, |
| 259 | + "configFile": target_path.joinpath("hardhat.config.js"), |
| 260 | + "sources": target_path.joinpath("contracts"), |
| 261 | + "cache": target_path.joinpath("cache"), |
| 262 | + "artifacts": target_path.joinpath("artifacts"), |
| 263 | + "tests": target_path.joinpath("test"), |
| 264 | + } |
| 265 | + override_paths = {} |
| 266 | + |
| 267 | + if args.get("hardhat_cache_directory", None): |
| 268 | + override_paths["cache"] = Path(target_path, args["hardhat_cache_directory"]) |
| 269 | + |
| 270 | + if args.get("hardhat_artifacts_directory", None): |
| 271 | + override_paths["artifacts"] = Path(target_path, args["hardhat_artifacts_directory"]) |
| 272 | + |
| 273 | + if args.get("hardhat_working_dir", None): |
| 274 | + override_paths["root"] = Path(target_path, args["hardhat_working_dir"]) |
| 275 | + |
| 276 | + print_paths = "console.log(JSON.stringify(config.paths))" |
| 277 | + config_str = self._run_hardhat_console(base_cmd, print_paths) |
| 278 | + |
| 279 | + try: |
| 280 | + paths = json.loads(config_str or "{}") |
| 281 | + return {**default_paths, **paths, **override_paths} |
| 282 | + except ValueError as e: |
| 283 | + LOGGER.info("Problem deserializing hardhat configuration: %s", e) |
| 284 | + return {**default_paths, **override_paths} |
| 285 | + |
| 286 | + def _run_hardhat_console(self, base_cmd: List[str], command: str) -> Optional[str]: |
| 287 | + """Run a JS command in the hardhat console |
| 288 | +
|
| 289 | + Args: |
| 290 | + base_cmd ([str]): hardhat command |
| 291 | + command (str): console command to run |
| 292 | +
|
| 293 | + Returns: |
| 294 | + Optional[str]: command output if execution succeeds |
| 295 | + """ |
| 296 | + with subprocess.Popen( |
| 297 | + base_cmd + ["console", "--no-compile"], |
| 298 | + stdin=subprocess.PIPE, |
| 299 | + stdout=subprocess.PIPE, |
| 300 | + stderr=subprocess.PIPE, |
| 301 | + cwd=self._target, |
| 302 | + executable=shutil.which(base_cmd[0]), |
| 303 | + ) as process: |
| 304 | + stdout_bytes, stderr_bytes = process.communicate(command.encode("utf-8")) |
| 305 | + stdout, stderr = ( |
| 306 | + stdout_bytes.decode(), |
| 307 | + stderr_bytes.decode(), |
| 308 | + ) |
| 309 | + |
| 310 | + if stderr: |
| 311 | + LOGGER.info("Problem executing hardhat: %s", stderr) |
| 312 | + return None |
| 313 | + |
| 314 | + return stdout |
0 commit comments