fix: raise CompileError when solc returns no contracts to avoid batch crash#160
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens Solidity compilation error handling so that when solc --standard-json returns an errors-only (or otherwise malformed) response, the tool raises a structured CompileError instead of allowing a downstream KeyError to crash batch processing.
Changes:
- Parse
solcstdout defensively incompile_contracts()and raiseCompileErrorfor non-JSON output or missing"contracts". - Surface formatted
solcerror messages (filtering warnings when errors exist) in the raised exception. - Add unit tests covering errors-only output, warning filtering, invalid JSON, and success cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| diffyscan/utils/compiler.py | Adds safer JSON parsing and explicit CompileError paths when solc output is malformed or missing contracts. |
| tests/test_compiler.py | Introduces pytest coverage for the new compile_contracts() error-handling behavior. |
Comments suppressed due to low confidence (1)
diffyscan/utils/compiler.py:123
- This branch assumes
outputis a dict (usesoutput.get(...)), but if solc (or a wrapper) returns valid JSON that isn't an object (e.g.,null, list, or string), this will raiseAttributeErrorand bypassCompileError. Consider validatingisinstance(output, dict)before checking keys / calling.get, and raisingCompileErrorwith a clear diagnostic when the top-level JSON shape is unexpected.
if "contracts" not in output:
errors = output.get("errors") or []
fatal = [e for e in errors if e.get("severity") == "error"] or errors
msgs = "\n".join(
e.get("formattedMessage") or e.get("message") or str(e) for e in fatal
)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
TheDZhon
approved these changes
May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compile_contracts()returnedjson.loads(stdout)without checking for"contracts". When solc returned an errors-only response (e.g. unresolved import from version drift in a dependency),compile_contract_from_explorer()didcompile_contracts(...)["contracts"]and raisedKeyError.KeyErroris not aBaseCustomException, so it bypassed both the per-contract and outer handlers inprocess_config, killing the whole batch — one bad implementation address took down all remaining contracts in the config.compile_contracts()now raises a structuredCompileErrorwith formatted solc error messages when output has no"contracts"key, and also when stdout is non-JSON. The existingexcept BaseCustomExceptioninprocess_configthen catches it and continues with the next contract.Test plan
uv run pytest -q— 92 passedtests/test_compiler.pycovers: errors-only output, warning-only filtering, invalid JSON, happy pathuv run black --check diffyscan/utils/compiler.py tests/test_compiler.py