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

Generalize implementation of ImportsResolver to allow imports from arbitrary npm packages #78

Merged
merged 2 commits into from
Dec 18, 2024

Conversation

jjohannes
Copy link
Contributor

Why is it needed?

This came out of a discussion with @rbair23. We would like to use this plugin to compile contracts like this one:

pragma solidity ^0.8.22;

import { OFT } from "@layerzerolabs/oft-evm/contracts/OFT.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";

contract MyOFT is OFT {
    constructor(
        string memory _name,
        string memory _symbol,
        address _lzEndpoint,
        address _delegate
    ) OFT(_name, _symbol, _lzEndpoint, _delegate) Ownable(_delegate) {}
} 

This uses sol files from @layerzerolabs/oft-evm which in turn uses files from other npm packages (transitive dependencies/imports). This could be solved by somehow allowing to add more import providers as suggested in #77. But then you would need to configure all packages manually that are already declared in the SOL files.

What does this PR do?

The solution proposed in this PR automatically reads all required npm packages, including transitive ones. This is done by changing the setup that is handled by the npmInstall and resolveSolidity tasks as follows:

  • Add a task extractSolidityImports that extracts all packages required by imports from the SOL files in the project and generates the package.json based on this information. The package.json is the output of this task and it only changes if modules required for the imports change. Which is not often the case. Thus, the next two tasks run seldomly and are UP-TO-DATE most of the time.
  • npmInstall downloads the packages and their transitive dependencies.
  • resolveSolidity searches through the SOL files downloaded by npm for additional packages that are used to get a complete list for the allowed paths and the path remappings in compileSolidity.

Where should the reviewer start?

This only a proposal at this point. Maybe there are reasons not to do this. I am happy to work on another solution that would allow us to compile contracts as the one shown above.

I haven't changed the documentation in the Readme on this topic yet, but if this is a solution that would be accepted, I will update it as well.

Checklist

  • I've read the contribution guidelines.
  • I've added tests (if applicable).
  • I've added a changelog entry if necessary.

@jjohannes jjohannes force-pushed the generic_imports_resolver branch 2 times, most recently from d9b3b8d to 7e0f729 Compare December 5, 2024 14:00
* </ul>
*/
@Singleton
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Singleton and @Memoized are problematic in Gradle plugins. Plugin classes are loaded in the Gradle daemon process and are then reused between build runs. Static state is then preserved. I observed here that extractImports still returned old values, even though I changed the source file and the task did run again.

...to allow imports from arbitrary npm packages.

Signed-off-by: Jendrik Johannes <[email protected]>
@jjohannes jjohannes force-pushed the generic_imports_resolver branch from 7e0f729 to 068dbe7 Compare December 10, 2024 19:28
Signed-off-by: gtebrean <[email protected]>
@gtebrean gtebrean merged commit f434015 into hyperledger-web3j:main Dec 18, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants