Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solc auto-detect not working as expected #209

Closed
moodmosaic opened this issue Dec 11, 2021 · 11 comments
Closed

solc auto-detect not working as expected #209

moodmosaic opened this issue Dec 11, 2021 · 11 comments

Comments

@moodmosaic
Copy link

moodmosaic commented Dec 11, 2021

Φοβερή δουλειά! Great job!

It looks like forge's solc auto-detect not working as expected(?)

I just run

cargo install --git https://github.com/gakonst/foundry --bin forge --locked

followed by

forge init

then

forge build

then replaced what's inside src/Contract.sol with the following (notice ^0.4.0)

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.4.0;

contract Counter {
    uint public count;

    // Function to get the current count.
    function get() public returns (uint) {
        return count;
    }

    // Function to increment count by 1.
    function inc() public {
        count += 1;
    }

    // Function to decrement count by 1.
    function dec() public {
        count -= 1;
    }
}

and then on purpose I declared get() as view which I would expect to break with ^0.4.0

diff --git a/src/Contract.sol b/src/Contract.sol
index c116a9e..cdf66eb 100644
--- a/src/Contract.sol
+++ b/src/Contract.sol
@@ -5,7 +5,7 @@ contract Counter {
     uint public count;

     // Function to get the current count
-    function get() public returns (uint) {
+    function get() public view returns (uint) {
         return count;
     }

run

forge build

and got success. instead of a compilation error (e.g. Remix gives an error since view doesn't exist in ^0.4.0.


Expected output:

src/Contract.sol:9:27: Error: Expected token LBrace got 'View'
    function get() public view returns (uint) {
                          ^

Actual output:

success.
@mattsse
Copy link
Member

mattsse commented Dec 11, 2021

can you try running with RUST_LOG=ethers=trace this should show you the solc being used.

Auto-detection is based on semver, and ^4.0.0 allows everything from 4.0.0 to <5.0.0.

So solc will try to find a compatible, already downloaded, solc install

It seems like that this doesn't necessarily hold for solc though

@moodmosaic
Copy link
Author

@mattsse, thanks for the quick response. Here's the output:

Dec 11 12:29:57.354 TRACE compile: ethers_solc: sources
Dec 11 12:29:57.356 TRACE compile: ethers_solc: done
Dec 11 12:29:57.356 TRACE compile: ethers_solc: auto-compile
Dec 11 12:29:57.357 TRACE compile:svm_compile: ethers_solc: detecting solc version for "../forge/src/Contract.sol"
Dec 11 12:29:57.395 TRACE compile:svm_compile: ethers_solc: found installed solc "0.4.26"
Dec 11 12:29:57.398 TRACE compile:svm_compile: ethers_solc: detecting solc version for "../forge/src/test/Contract.t.sol"
Dec 11 12:29:57.398 TRACE compile:svm_compile: ethers_solc: found installed solc "0.8.10"
Dec 11 12:29:57.400 TRACE compile:svm_compile: ethers_solc: compiling sources with viable solc versions
Dec 11 12:29:57.857 TRACE compile:svm_compile:solc{0.4.26}: ethers_solc: compiling_with_version
Dec 11 12:29:57.858 TRACE compile:svm_compile:solc{0.4.26}:compiling: ethers_solc: resolving libraries
Dec 11 12:29:57.859 TRACE compile:svm_compile:solc{0.4.26}:compiling: ethers_solc: resolved libraries
Dec 11 12:29:57.860 TRACE compile:svm_compile:solc{0.4.26}:compiling: ethers_solc: reading solfiles cache for incremental compilation
Dec 11 12:29:57.861 TRACE compile:svm_compile:solc{0.4.26}:compiling:sol-files-cache::read: ethers_solc::cache: reading solfiles cache at ../forge/cache/solidity-files-cache.json
Dec 11 12:29:57.862 TRACE compile:svm_compile:solc{0.4.26}:compiling:sol-files-cache::read: ethers_solc::cache: done
Dec 11 12:29:57.863 TRACE compile:svm_compile:solc{0.4.26}:compiling: ethers_solc: done reading solfiles cache for incremental compilation
Dec 11 12:29:57.864 TRACE compile:svm_compile:solc{0.4.26}:compiling: ethers_solc: reading artifacts from cache..
Dec 11 12:29:57.899 TRACE compile:svm_compile:solc{0.4.26}:compiling: ethers_solc: done reading artifacts from cache
Dec 11 12:29:57.900 TRACE compile:svm_compile:solc{0.4.26}:compiling: ethers_solc: no change
Dec 11 12:29:57.901 TRACE compile:svm_compile:solc{0.4.26}: ethers_solc: done compiling_with_version
Dec 11 12:29:58.041 TRACE compile:svm_compile:solc{0.8.10}: ethers_solc: compiling_with_version
Dec 11 12:29:58.042 TRACE compile:svm_compile:solc{0.8.10}:compiling: ethers_solc: resolving libraries
Dec 11 12:29:58.043 TRACE compile:svm_compile:solc{0.8.10}:compiling: ethers_solc: resolved libraries
Dec 11 12:29:58.046 TRACE compile:svm_compile:solc{0.8.10}:compiling: ethers_solc: reading solfiles cache for incremental compilation
Dec 11 12:29:58.047 TRACE compile:svm_compile:solc{0.8.10}:compiling:sol-files-cache::read: ethers_solc::cache: reading solfiles cache at ../forge/cache/solidity-files-cache.json
Dec 11 12:29:58.049 TRACE compile:svm_compile:solc{0.8.10}:compiling:sol-files-cache::read: ethers_solc::cache: done
Dec 11 12:29:58.050 TRACE compile:svm_compile:solc{0.8.10}:compiling: ethers_solc: done reading solfiles cache for incremental compilation
Dec 11 12:29:58.054 TRACE compile:svm_compile:solc{0.8.10}:compiling: ethers_solc: reading artifacts from cache..
Dec 11 12:29:58.097 TRACE compile:svm_compile:solc{0.8.10}:compiling: ethers_solc: done reading artifacts from cache
Dec 11 12:29:58.098 TRACE compile:svm_compile:solc{0.8.10}:compiling: ethers_solc: no change
Dec 11 12:29:58.099 TRACE compile:svm_compile:solc{0.8.10}: ethers_solc: done compiling_with_version
Dec 11 12:29:58.100 TRACE compile:svm_compile: ethers_solc: compiled sources with viable solc versions
no files changed, compilation skippped.

@moodmosaic
Copy link
Author

Hmm... I guess it'll favor the solc used in the test(s)?

@mattsse
Copy link
Member

mattsse commented Dec 11, 2021

yes, forge test only deploys contracts with test* functions.
And it seems your test/Contract.t.sol allows 0.8.10?

There are currently some limitations when it comes to dependency graphs, so if you could share the imports and pragma of test/Contract.t.sol, that'd be helpful.

@moodmosaic
Copy link
Author

moodmosaic commented Dec 11, 2021

// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.10;

import "ds-test/test.sol";

Something tells me that view simply exists in 0.4.26 (which is the one that's picked up by forge build as per the above output).

@moodmosaic
Copy link
Author

Yes! If I specify pragma solidity 0.4.0; then indeed it fails:

diff --git a/src/Contract.sol b/src/Contract.sol
index c116a9e..3ee7dc0 100644
--- a/src/Contract.sol
+++ b/src/Contract.sol
@@ -1,11 +1,11 @@
 // SPDX-License-Identifier: Unlicense
-pragma solidity ^0.4.0;
+pragma solidity 0.4.0;

 contract Counter {
     uint public count;

     // Function to get the current count
-    function get() public returns (uint) {
+    function get() public view returns (uint) {
         return count;
     }

It'd be nice if we could get the actual compiler error/output from forge build but I guess that's for a separate issue:


$ RUST_LOG=ethers=trace forge build
Dec 11 12:50:30.638 TRACE compile: ethers_solc: sources
Dec 11 12:50:30.640 TRACE compile: ethers_solc: done
Dec 11 12:50:30.640 TRACE compile: ethers_solc: auto-compile
Dec 11 12:50:30.641 TRACE compile:svm_compile: ethers_solc: detecting solc version for "../src/Contract.sol"
Dec 11 12:50:30.698 TRACE compile:svm_compile: ethers_solc: found installed solc "0.4.0"
Dec 11 12:50:30.706 TRACE compile:svm_compile: ethers_solc: detecting solc version for "../src/test/Contract.t.sol"
Dec 11 12:50:30.708 TRACE compile:svm_compile: ethers_solc: found installed solc "0.8.10"
Dec 11 12:50:30.712 TRACE compile:svm_compile: ethers_solc: compiling sources with viable solc versions
Dec 11 12:50:31.340 TRACE compile:svm_compile:solc{0.4.0}: ethers_solc: compiling_with_version
Dec 11 12:50:31.341 TRACE compile:svm_compile:solc{0.4.0}:compiling: ethers_solc: resolving libraries
Dec 11 12:50:31.342 TRACE compile:svm_compile:solc{0.4.0}:compiling: ethers_solc: resolved libraries
Dec 11 12:50:31.343 TRACE compile:svm_compile:solc{0.4.0}:compiling: ethers_solc: reading solfiles cache for incremental compilation
Dec 11 12:50:31.344 TRACE compile:svm_compile:solc{0.4.0}:compiling:sol-files-cache::read: ethers_solc::cache: reading solfiles cache at ../cache/solidity-files-cache.json
Dec 11 12:50:31.345 TRACE compile:svm_compile:solc{0.4.0}:compiling:sol-files-cache::read: ethers_solc::cache: done
Dec 11 12:50:31.346 TRACE compile:svm_compile:solc{0.4.0}:compiling: ethers_solc: done reading solfiles cache for incremental compilation
Dec 11 12:50:31.347 TRACE compile:svm_compile:solc{0.4.0}:compiling: ethers_solc: reading artifacts from cache..
Dec 11 12:50:31.389 TRACE compile:svm_compile:solc{0.4.0}:compiling: ethers_solc: done reading artifacts from cache
Dec 11 12:50:31.421 TRACE compile:svm_compile:solc{0.4.0}:compiling: ethers_solc: calling solc
Error: EOF while parsing a value at line 1 column 0

Location:
    /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/result.rs:1914:27

Expected output:

src/Contract.sol:9:27: Error: Expected token LBrace got 'View'
    function get() public view returns (uint) {
                          ^

Actual output:

Error: EOF while parsing a value at line 1 column 0

Location:
    /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/result.rs:1914:27

@mattsse
Copy link
Member

mattsse commented Dec 11, 2021

any chance you built with hardhat in the meantime?

@moodmosaic
Copy link
Author

any chance you built with hardhat in the meantime?

Sure.

However, on the expected/actual solc output above, do you think I should open an issue?

@mattsse
Copy link
Member

mattsse commented Dec 11, 2021

I think the root problem is, that solc not necessarily abide to semver rules.
perhaps our auto detection is too optimistic in some scenarios, we're using https://github.com/roynalnaruto/svm-rs so you can also install svm install "0.4.0" manually which should then be picked up

@moodmosaic
Copy link
Author

I think I finally got forge build to error out, it's just that the error isn't the actual solc compiler output/error:

Expected output:

src/Contract.sol:9:27: Error: Expected token LBrace got 'View'
    function get() public view returns (uint) {
                          ^

Actual output:

Error: EOF while parsing a value at line 1 column 0

Location:
    /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/result.rs:1914:27

Ping me and I'll make an issue for it. Closing this one for now.

@mattsse
Copy link
Member

mattsse commented Dec 11, 2021

any chance you built with hardhat in the meantime?

Sure.

ethers-solc uses a different artifact format by default, but the same cache format as hardhat, so this can happen, I think we should find a way around that

However, on the expected/actual solc output above, do you think I should open an issue?

I think It's worth discussing if we want to follow always semver or if we need some additional checks, +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants