-
Notifications
You must be signed in to change notification settings - Fork 91
fix: sort Foundry sources by ID for correct sourceList ordering #653
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
Open
Mike4751
wants to merge
4
commits into
crytic:master
Choose a base branch
from
Mike4751:fix-foundry-sourcelist-order
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a40c132
Fix Foundry source map index mismatch in sourceList export
Mike4751 4a9ab22
style: fix ruff formatting in hardhat.py
Mike4751 57e6f5c
address review: size array for all filenames, raise on source ID gaps
Mike4751 7ad7739
fix ty type check: cast result to list[Filename]
Mike4751 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,10 @@ def __init__(self, crytic_compile: "CryticCompile", unique_id: str): | |
| # set containing all the filenames of this compilation unit | ||
| self._filenames: list[Filename] = [] | ||
|
|
||
| # mapping from source ID to filename (for Foundry/Hardhat source map compatibility) | ||
| # When set, this takes precedence over _filenames for export ordering | ||
| self._source_id_to_filename: dict[int, Filename] = {} | ||
|
|
||
| # mapping from absolute/relative/used to filename | ||
| self._filenames_lookup: dict[str, Filename] | None = None | ||
|
|
||
|
|
@@ -181,6 +185,53 @@ def filenames(self, all_filenames: list[Filename]) -> None: | |
| """ | ||
| self._filenames = all_filenames | ||
|
|
||
| @property | ||
| def filenames_for_export(self) -> list[Filename]: | ||
| """Return filenames in the correct order for export (matching source map indices). | ||
|
|
||
| If source ID mapping is available (from Foundry/Hardhat build-info), returns | ||
| filenames ordered by source ID. Otherwise, returns filenames in append order. | ||
|
|
||
| Returns: | ||
| list[Filename]: Filenames ordered for export | ||
| """ | ||
| if not self._source_id_to_filename: | ||
| return self._filenames | ||
|
|
||
| # Build list indexed by source ID | ||
| max_id = max(self._source_id_to_filename.keys()) | ||
| result: list[Filename | None] = [None] * (max_id + 1) | ||
|
|
||
| for source_id, filename in self._source_id_to_filename.items(): | ||
| result[source_id] = filename | ||
|
|
||
| # Fill gaps with filenames from _filenames that aren't in the mapping | ||
| mapped_filenames = set(self._source_id_to_filename.values()) | ||
| unmapped = [f for f in self._filenames if f not in mapped_filenames] | ||
| unmapped_iter = iter(unmapped) | ||
|
|
||
| for i, entry in enumerate(result): | ||
| if entry is None: | ||
| try: | ||
| result[i] = next(unmapped_iter) | ||
| except StopIteration: | ||
| # No more unmapped filenames, leave as None (will be filtered) | ||
| pass | ||
|
|
||
| # Filter out None entries and return | ||
| return [f for f in result if f is not None] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filtering out None elements can move indices, i think is better if there would be a warning or error if a None is present
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll change to an error. |
||
|
|
||
| def set_source_id(self, source_id: int, filename: Filename) -> None: | ||
| """Set the source ID for a filename. | ||
|
|
||
| This is used by Foundry/Hardhat parsers to maintain correct source map indices. | ||
|
|
||
| Args: | ||
| source_id (int): The source ID from the build-info | ||
| filename (Filename): The filename associated with this ID | ||
| """ | ||
| self._source_id_to_filename[source_id] = filename | ||
|
|
||
| @property | ||
| def filename_to_contracts(self) -> dict[Filename, set[str]]: | ||
| """Return a dict mapping the filename to a list of contract declared | ||
|
|
||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me why 1 is added, also shouldn't be
max(max_id, len(self._filenames))to handle the case of self._filenames > max_id?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay! The Solidity compiler assigns source IDs starting at 0. Also, you're right I included this change.