foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.31k stars 1.75k forks source link

add option to `forge build` to only get the AST without compiling to bytecode #7212

Open 0xalpharush opened 8 months ago

0xalpharush commented 8 months ago

Component

Forge

Describe the feature you would like

UPDATE see https://github.com/foundry-rs/foundry/issues/7212#issuecomment-2075291224

I would like the forge build command to expose this, so one can pass --stop-after parsing solc and only get the AST without compiling to bytecode

As a potential optimization, foundry could look if the solidity files' content is unchanged and reuse the AST on disk for compilation by giving them to solc when forge build --stop-after parsing && forge build/test is run.

Additional context

from https://github.com/ethereum/solidity/blob/develop/docs/using-the-compiler.rst#compiler-input-and-output-json-description:

{
      // If language is set to "SolidityAST", an AST needs to be supplied under the "ast" key
      // and there can be only one source file present.
      // The format is the same as used by the `ast` output.
      // Note that importing ASTs is experimental and in particular that:
      // - importing invalid ASTs can produce undefined results and
      // - no proper error reporting is available on invalid ASTs.
      // Furthermore, note that the AST import only consumes the fields of the AST as
      // produced by the compiler in "stopAfter": "parsing" mode and then re-performs
      // analysis, so any analysis-based annotations of the AST are ignored upon import.
      "ast": { ... }
    },
mds1 commented 8 months ago

@klkvr Related to https://github.com/foundry-rs/foundry/issues/5041 where we'd want similar functionality to only get ABIs without compiling, so we know which contracts to compile

0xalpharush commented 6 months ago

This is not strictly blocked by https://github.com/ethereum/solidity/issues/15043 as additional stages would be trivial to add but it may be premature considering the original use case needs a semantic stage.

0xalpharush commented 6 months ago

@klkvr Is there a way to not request bin and only request ast with Foundry. I think forge build --ast (not sure why this wasn't just added to --extra-output) still requests the bytecode. I would prefer the AST in the hardhat style artifact just with everything else missing (key is there but value is empty) because the foundry native artifact format has several latent bugs e.g. https://github.com/gakonst/ethers-rs/issues/1995

holic commented 3 months ago

I wonder if adding a flag for bytecode (and defaulting it on) would help with this? then you could do something like forge build --ast --bytecode=false to get just the AST?

We'd love to have this in https://github.com/latticexyz/mud so we replace our dependency on https://github.com/solidity-parser/parser (which has subpar syntax error output)

holic commented 2 months ago

I'm now thinking what I'd prefer is actually just a forge ast path/to/file.sol that emits the AST JSON to stdout, then can pipe to other tools like jq, etc.