foundry icon indicating copy to clipboard operation
foundry copied to clipboard

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

Open 0xalpharush opened this issue 1 year ago • 5 comments

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": { ... }
    },

0xalpharush avatar Feb 22 '24 17:02 0xalpharush

@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

mds1 avatar Feb 26 '24 14:02 mds1

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 avatar Apr 20 '24 16:04 0xalpharush

@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

0xalpharush avatar Apr 24 '24 15:04 0xalpharush

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)

frolic avatar Aug 07 '24 14:08 frolic

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.

frolic avatar Aug 21 '24 09:08 frolic