foundry icon indicating copy to clipboard operation
foundry copied to clipboard

forge bind generates extraneous bindings

Open FrankieIsLost opened this issue 2 years ago • 7 comments

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.

FrankieIsLost avatar Feb 17 '23 02:02 FrankieIsLost

@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?

gakonst avatar Feb 17 '23 02:02 gakonst

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?

prestwich avatar Feb 17 '23 02:02 prestwich

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

FrankieIsLost avatar Feb 17 '23 02:02 FrankieIsLost

Can we bump this? @0xjepsen and I find this to be an issue for us as well. We can try to tackle it.

Autoparallel avatar Sep 18 '23 11:09 Autoparallel

Feel free to go for it, happy to review.

gakonst avatar Sep 18 '23 20:09 gakonst

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.

lakshya-sky avatar Feb 28 '24 18:02 lakshya-sky

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

zerosnacks avatar Jun 28 '24 15:06 zerosnacks

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?

grandizzy avatar Jan 14 '25 11:01 grandizzy

optimistically close it per comment above, @FrankieIsLost @Autoparallel please reopen if not a solution for you. thank you

grandizzy avatar Jan 15 '25 10:01 grandizzy