matter-labs / era-compiler-solidity

Solidity compiler for ZKsync.
Apache License 2.0
62 stars 26 forks source link

Add support for inputs loaded via solc's import callbacks #98

Closed elfedy closed 4 months ago

elfedy commented 5 months ago

🌟 Feature Request

πŸ“ Description

When compiling a source file and finding an import that is not provided as input, solc will try to resolve the source via an import callback. If resolved successfully, the source will be compiled and be part of the output. This is not something that zksolc apparently expects, as it fails with this error. You can reproduce the behavior by following these examples. Ideally zksolc would be able to work just fine when it finds compiled outputs from solc that come from sources that are not part of the initial input. At least an attempt to reproduce the deafult Host Filesystem Loader that tries to get the file from the path derived from the source unit name would probably fit lots of use cases. If this is not possible or not planned to be supported, it would be nice to either use solc internally with --no-import-callback or give a clearer error explaining the user what happened a bit better (solc produced an output from a resolved import vs from a source provided on compiler input and this is not supported).

πŸ€” Rationale

Benefits from the feature would be:

  1. Get closer to feature parity with solc.
  2. User would not have to specify all sources as input to the compiler and just provide the "root" files.
  3. Prevent some confusion on errors. There are cases that solc will add an already provided input but with a different name as a result of import resolution (It may have home/user/Contract.sol and ./Contract.sol as different source units), in which case zksolc will fail when looking to the sources map with Source code for path not found even though a source code matching that path was provided as input.

πŸ“‹ Additional Context

This request comes as a result of investigating an issue in foundry: https://github.com/matter-labs/foundry-zksync/issues/411

After patching forge with an issue that did not add the correct allow_paths, using forge build --zksync on the provided example will result in the issue described in .3 on the Rationale, where solc will resolve an import of an already provided source with a different source unit name, and then zksolc won't find the source by the path string of solc's output in the input provided by foundry.

hedgar2017 commented 5 months ago

Hey @elfedy, thank you for the extensive summary. It is a known issue and we're investigating how to resolve it. The only way is probably parsing the AST and extracting imports from there. We'll try that.

hedgar2017 commented 4 months ago

Will be released in v1.5.2!