-
Notifications
You must be signed in to change notification settings - Fork 795
Conversation
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.
while this approach looks like it could work, I have some suggestions how to improve this a bit.
we're already parsing the solidity file and detecting import statements:
ethers-rs/ethers-solc/src/resolver.rs
Lines 429 to 434 in 1742630
SourceUnitPart::ImportDirective(_, import) => { | |
let import = match import { | |
Import::Plain(s) => s, | |
Import::GlobalSymbol(s, _) => s, | |
Import::Rename(s, _) => s, | |
}; |
all these import variants include a Loc
value which has the start and end of the whole statement.
So ideally we want to use this data so we can later simply replace that slice [start..end]
with the content of the file that's imported here.
however since solang
only targets >0.7.0, it can happen that need to use the regex fall back, so ideally the utils::find_import_paths
will also return them, which should be possible since the regex::Match
type has start
and end
https://docs.rs/regex/latest/regex/struct.Match.html
If we roll a struct Import {path, start, end}
for the SolData
type we should be able to flatten the entire graph.
This depth-first operation where you start with the targeted file and then recursively loop through all imports and their imports... and replace every import statement with content. That's probably a bit tricky since we need to keep track of updated locations because after we replaced [start..end] with the first import the start and end locations of the next import are no longer valid
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.
great progress!
some suggestions and comments
ethers-solc/src/config.rs
Outdated
let result = String::from_utf8(extended).map_err(|err| { | ||
SolcError::msg(format!("failed to convert extended bytes to string: {}", err)) | ||
})?; |
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.
we can definitely do to_string_lossy
here?
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.
few nits,
overall this is looking good,
I'd like to see couple more tests, for the dapp/hardhat examples
and perhaps you could try manually try this with some real repos and see if that works
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.
lgtm!
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.
test failure is unrelated, merging and we can iterate as we close the feedback loop with usage in foundry.
is there any follow up that needs to be done which we know we need? probably need to get the solidity compiler version saved in the artifacts (#755)
Motivation
Implementation of flatten feature discussed in foundry #312
Solution
Use the dependency graph to recursively resolve the file imports into a single string.
PR Checklist