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

guess solc if pragma is avail, better error if file is a dir #411

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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
23 changes: 21 additions & 2 deletions crytic_compile/platform/solc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Union, Any

from packaging import version
from solc_select import solc_select

from crytic_compile.compilation_unit import CompilationUnit
from crytic_compile.compiler.compiler import CompilerVersion
from crytic_compile.platform.abstract_platform import AbstractPlatform
Expand Down Expand Up @@ -479,16 +482,32 @@ def _run_solc(
Returns:
Dict: Json compilation artifacts
"""

if not os.path.isfile(filename) and (
not working_dir or not os.path.isfile(os.path.join(str(working_dir), filename))
):
raise InvalidCompilation(f"{filename} does not exist (are you in the correct directory?)")
if os.path.isdir(filename):
raise InvalidCompilation(
f"{filename} is a directory. Expected a Solidity file when not using a compilation framework."
)
else:
raise InvalidCompilation(
f"{filename} does not exist. Are you in the correct working directory?"
)

if not filename.endswith(".sol"):
raise InvalidCompilation("Incorrect file format")

env_version = get_version(solc, env)
pragma_matches = _guess_solc(filename, working_dir)
if len(pragma_matches):
guessed_version = pragma_matches[0]
if version.parse(env_version) != version.parse(guessed_version):
solc_select.switch_global_version(guessed_version, always_install=True)
env_version = guessed_version

compilation_unit.compiler_version = CompilerVersion(
compiler="solc", version=get_version(solc, env), optimized=is_optimized(solc_arguments)
compiler="solc", version=env_version, optimized=is_optimized(solc_arguments)
)

compiler_version = compilation_unit.compiler_version
Expand Down