Solidity VSCode – How to Resolve Override Warning in VSCode During Compile

function-overridesolidityvisual-studio-code

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.

Best Answer

Your Solidity extension in VS Code is probably not using the same compiler version as your contract. Different compiler versions may have conflicting rules, so this will make sure VS Code uses the same compiler from your node_modules folder.

Solution 1: Change it in File > Preferences > Settings, search for Solidity and change the Default Compiler to localNodeModule:

Default Compiler

Solution 2: You could also change this in your project's local settings.json, once pushed into your repo, your colleagues can pull and they don't need to change this in their VS Code.

{
    "solidity.defaultCompiler": "localNodeModule",
}

enter image description here

Related Topic