forge bind generates extraneous bindings
Component
Forge
Have you ensured that all of these are up to date?
- [X] Foundry
- [X] Foundryup
What version of Foundry are you on?
forge 0.2.0 (54e02a7 2023-02-15T19:38:32.464976Z)
What command(s) is the bug in?
forge bind
Operating System
macOS (Apple Silicon)
Describe the bug
Running forge bind tends to generate more bindings than expected, resulting in larger-than-needed crates with irrelevant modules. For example, running forge bind --bindings-path ./bindings --crate-name bindings in the ArtGobblers repo generates a crate with 37 modules. Most users would likely expect only 7 modules to be created here (one for each contract in the src directory).
The issue seems to be that forge bind generates a binding module for every artifact generated by forge build, while excluding certain patterns here. However, it's easy for the list to fall out of date (for example, when new Std contracts are added).
Another issue is that bindings are generated for each contract in the inheritance graph. If I have contract A is ERC20 in my src directory, I likely only want to generate bindings for A, not both A and ERC20.
One possible solution might be generate bindings only for contracts defined in the src directory, instead of the current approach of generating contracts for every artifact in the build output, and then filtering out the test/script contracts.
@mattsse @prestwich should we make it default to only generating the src dir? and have the option to bind everything if the user opts in? not sure if @prestwich you use bindings to deps. maybe it's useful in some weird monorepo setup?
forge bind uses the out/ dir, which doesn't preserve the src layout, and will include extraneous files. we should probably solve this with filtering options?
one option might be to use compilationTarget in the artifact metadata, which seems to contain a path to the source file, but not sure how reliable this is
Can we bump this? @0xjepsen and I find this to be an issue for us as well. We can try to tackle it.
Feel free to go for it, happy to review.
I've raised a PR for this issue. Didn't know any other way to do it. If you have suggestions plz let me know.
We've recently overhauled forge bind to use Alloy: https://github.com/foundry-rs/foundry/pull/7919
Would be great to get feedback on this by using forge bind with the --alloy flag
ethers was removed and alloy is now used by default when forge bind. To filter out undesired bindings one could use
--select <SELECT>
Create bindings only for contracts whose names match the specified filter(s)
option to filter out the generated bindinds, e.g. forge bind --select "^Goo$" will generate single goo.rs binding.
Would this solution be OK?
optimistically close it per comment above, @FrankieIsLost @Autoparallel please reopen if not a solution for you. thank you