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.33k stars 1.76k forks source link

bug: can't compile a contract with `function() external payable` anymore #9349

Closed enitrat closed 1 day ago

enitrat commented 2 days ago

Component

Forge

Have you ensured that all of these are up to date?

What version of Foundry are you on?

forge 0.2.0 (c13d42e 2024-11-19T00:22:29.791725000Z)

What command(s) is the bug in?

forge build

Operating System

macOS (Apple Silicon)

Describe the bug

Running forge build on a project that contains a program with a function() external payable and specific compiler restrictions such as WETH9 results in a compilation failure due to a compiler version mismatch.

Reproduction steps: forge init bug_report cd bug_report run:

echo "pragma solidity >=0.4.22 <0.6;

contract BugReport {
    function() external payable {
        deposit();
    }
    function deposit() public payable {}
}
" >> src/BugReport.sol

run forge build

Compilation fails with:

[⠊] Compiling...
[⠢] Compiling 28 files with Solc 0.8.27
[⠆] Solc 0.8.27 finished in 111.11ms
Error: Compiler run failed:
Error (5333): Source file requires different compiler version (current compiler is 0.8.27+commit.40a35a09.Darwin.appleclang) - note that nightly builds are considered to be strictly less than the released version
 --> src/BugReport.sol:1:1:
  |
1 | pragma solidity >=0.4.22 <0.6;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Now, if you remove the

    function() external payable {
        deposit();
    }

and run again:

❯ forge build
[⠊] Compiling...
[⠒] Compiling 1 files with Solc 0.5.16
[⠢] Compiling 27 files with Solc 0.8.27
[⠃] Solc 0.5.16 finished in 17.93ms
[⠊] Solc 0.8.27 finished in 637.54ms
Compiler run successful!

all is fine.

forge build should be able to compile BugReport.sol using the correct compiler version, but it is not able to.

grandizzy commented 2 days ago

@enitrat I am not able to reproduce the issue with default config / steps you mentioned, mind to share your foundry.toml file or a gh repo with simple way to reproduce?

[⠒] Compiling...
[⠢] Compiling 1 files with Solc 0.5.17
[⠰] Compiling 6 files with Solc 0.8.28
[⠊] Solc 0.5.17 finished in 23.72ms
[⠢] Solc 0.8.28 finished in 579.99ms

thanks!

enitrat commented 2 days ago

Can you try this? https://github.com/enitrat/foundry_bugreport

They're the steps described above in a repo 🤔

Otherwise we first noticed this happening in our CI run here: https://github.com/kkrt-labs/kakarot/actions/runs/11906382879/job/33178379639

grandizzy commented 2 days ago

oops, I didn't have the latest sources pulled, can reproduce and look into, thank you CC @DaniPopes @klkvr for better insights

klkvr commented 2 days ago

@DaniPopes getting this when running solar src/BugReport.sol

error: expected identifier, found `{`
 --> src/BugReport.sol:4:33
  |
4 |     function() external payable {
  |                                 ^
  |

error: aborting due to 1 previous error

I guess the root cause is that solar fails to parse the file, thus we are losing data about version requirements of the file and compile everything with 0.8.27 instead of using an older solc for the BugReport.sol

Should we fix this in solar or instead fallback to using regexes to find pragmas?

DaniPopes commented 2 days ago

We can do both, we can have better error recovery in Solar and revert the regex removal.