juanfranblanco / vscode-solidity

Visual Studio Code language support extension for Solidity smart contracts in Ethereum https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity
MIT License
885 stars 190 forks source link

Override Problem Showed Up in VSCode But No Problem Compiling #327

Open fahadh4ilyas opened 2 years ago

fahadh4ilyas commented 2 years ago

I have a smart contract like this

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/security/Pausable.sol";

contract SampleContract is Context, AccessControlEnumerable, Pausable {
    grantRole(
        bytes32 role, 
        address account
    )
        public
        virtual
        override
    {
        super.grantRole(role, account);
        // do stuff after grantRole
    }
}

Somehow, this raise error in VS Code

Function needs to specify overridden contracts "AccessControl" and "IAccessControl".

But, when I tried to compiled it using solc 0.8.11, nothing happened. The compiler run successfully. I want to get rid of the error by adding override(AccessControl, IAccessControl), but the compiler raised error like this

TypeError: Invalid contract specified in override list: "AccessControl".
  --> project:/contracts/SampleContract.sol:15:9:
   |
15 |         override(AccessControl, IAccessControl)
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note: This contract:
  --> @openzeppelin/contracts/access/AccessControl.sol:49:1:
   |
49 | abstract contract AccessControl is Context, IAccessControl, ERC165 {
   | ^ (Relevant source part starts here and spans across multiple lines).

Compilation failed. See above.

I don't understand why is this happening? How to solve this? At least I want to ignore error from VS Code because it's kind of misleading.

cameel commented 2 years ago

I think your code snippet is not the one that triggers the error message you're showing. You're missing the function keyword and also you have override there but the message clearly shows that you had override(AccessControl, IAccessControl) there when the code was being compiled.

In any case, adding function and override(AccessControl, IAccessControl) is everything you need for it to compile with 0.8.13. There used to be a bug, which resulted in override not always being required as it should have been (https://github.com/ethereum/solidity/issues/12615). It was fixed in 0.8.12. Unfortunately the result of fixing it is that some contracts that would compile without override before, no longer will. The changes needed to avoid this error are pretty straightforward though.

By the way, this has nothing to do with vscode-solidity specifically. It was a change in the compiler.

fahadh4ilyas commented 2 years ago

I think your code snippet is not the one that triggers the error message you're showing. You're missing the function keyword and also you have override there but the message clearly shows that you had override(AccessControl, IAccessControl) there when the code was being compiled.

In any case, adding function and override(AccessControl, IAccessControl) is everything you need for it to compile with 0.8.13. There used to be a bug, which resulted in override not always being required as it should have been (ethereum/solidity#12615). It was fixed in 0.8.12. Unfortunately the result of fixing it is that some contracts that would compile without override before, no longer will. The changes needed to avoid this error are pretty straightforward though.

By the way, this has nothing to do with vscode-solidity specifically. It was a change in the compiler.

Sorry, I forget to add function keyword in my question. But, I assure you that I already add function in my script. The problem is from the override(AccessControl, IAccessControl). Because if the reason of error is because function keyword, the compiler will select function keyword error first right?

This is surely the problem from vscode-solidity because it tells me to add override(AccessControl, IAccessControl) even though the compiler with version 0.8.11 will raise error when you do that and only need to use override. vscode-solidity doesn't adapt to the compiler version.

fahadh4ilyas commented 2 years ago

@cameel Here is my proof

Here is how vscode-solidity telling me that writing override only is wrong...

vscode-solidity telling me error

Here is when I tried to compiled it...

compiled successfully

Here is when I tried to compiled it by adding override(AccessControl, IAccessControl)

compile error

cameel commented 2 years ago

It's not that I don't believe you, I just don't know all the factors coming into play here and a broken snippet did not help :)

First of all, if vscode-solidity and Truffle each compile one version but not the other, it's almost certain that they're using different compiler versions under the hood. Like I said, this did change in the compiler between 0.8.11 and 0.8.12 so the error is real, the problem is just why it's so inconsistent for you. Can you verify which compiler are you using with vscode-solidity? I'm pretty sure it's 0.8.12 or later.

Also, the error you get from Truffle on the second screenshot seems truncated or mangled. Does it always look like this for you? It should contain the error message that vscode-solidity is showing you in the first screenshot. You might want to report this as a problem in Truffle if it's reproducible (I mean just the way the error is shown, not the fact that there is an error as that's expected).

fahadh4ilyas commented 2 years ago

It's not that I don't believe you, I just don't know all the factors coming into play here and a broken snippet did not help :)

First of all, if vscode-solidity and Truffle each compile one version but not the other, it's almost certain that they're using different compiler versions under the hood. Like I said, this did change in the compiler between 0.8.11 and 0.8.12 so the error is real, the problem is just why it's so inconsistent for you. Can you verify which compiler are you using with vscode-solidity? I'm pretty sure it's 0.8.12 or later.

Where can I see compiler version of vscode-solidity? Because I'm using solc 0.8.11 in my truffle.

Also, the error you get from Truffle on the second screenshot seems truncated or mangled. Does it always look like this for you? It should contain the error message that vscode-solidity is showing you in the first screenshot. You might want to report this as a problem in Truffle if it's reproducible (I mean just the way the error is shown, not the fact that there is an error as that's expected).

That's not make sense, because from my second screenshot, it literally show that contracts are compiled successfully even though vscode-solidity said otherwise. The compiler is like the opposite of vscode-solidity. Compiler said that writing override only is right. But vscode-solidity said that writing override(AccessControl, IAccessControl) is right. So the last screenshot showing that the error is because of writing override(AccessControl, IAccessControl). Here is the verbose version of the error.

verbose of error

cameel commented 2 years ago

Where can I see compiler version of vscode-solidity? Because I'm using solc 0.8.11 in my truffle.

README has some instructions on downloading selecting specific compiler versions. See if you can check it that way.

But if you want to be 100% sure you're using 0.8.11, just set the pragma to pragma solidity 0.8.11. It just won't compile with any other version then and you'll see the version that was actually used in the error message.

That's not make sense, because from my second screenshot, it literally show that contracts are compiled successfully even though vscode-solidity said otherwise.

But it's not vscode-solidity running yarn truffle compile in your screenshot, is it? I'm assuming it's the command you ran manually and it can just as well be using a different compiler binary. Truffle and vscode-solidity manage compiler binaries independently as far as I know.

In any case, I'm not using vscode-solidity myself so I can be wrong about how it works but I know the compiler and I'm just saying that the behavior you're observing is consistent with what you'd see if one was using 0.8.11 and the other 0.8.12.

Here is the verbose version of the error.

Oh, so it was some kind of shortened output on that other screenshot? Ok then. Explains why it seemed mangled. Not a bug then :)