How to resolve Solidity version inconsistencies

2024-03-05
2021-10-23

If the version of Solidity you have implemented does not match the version of the compiler Solc (solc.js) associated with the Truffle command, you will get a Compilation failure when compiling.

Specify the version of conpiler

If the version is specified as follows, that of compiler is >= 0.5.0 and < 0.6.0.

pragma solidity ^0.5.0

Install truffle of the specific version

terminal

$ npm i -g truffle@5.1.4
$ truffle version
Truffle v5.1.4 (core: 5.1.4)
Solidity v0.5.12 (solc-js)

The Solidity version must match the contract version.

(P.S. 2021.10.30)

The Solidity version output by truffle version seems to depend on truffle-config.js (truffle.js). For example, if you set solc: ^0.5.12, the version will be slightly different as follows.

$ truffle version
Truffle v5.1.4 (core: 5.1.4)
Solidity - ^0.5.12 (solc-js)

When installing, there is no need to specify the version (if it is a newer version), and it seems to be OK as long as the version of the truffle-config.js (truffle.js) file matches the declaration of the Solidity file.

<= v5.0.35

trufflesuite / truffle / packages / compile-solidity / package.json

> v5.0.34

trufflesuite / truffle / packages / truffle-compile / package.json

The version of VS Code

The following error message may appear while editing Solidity in VS Code.

Source file requires different compiler version (current compiler is 0.4.24+commit.e67f0147.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version)

This is displayed when there is a mismatch between the Solidity version specified in the contract code and the Solidity version set in the VS Code workspace.

The VS Code workspace version can be changed by following the steps below.

  1. Open the command palette with command⌘ + shift + P
  2. Select "Solidity: Change workspace compiler version (Remote)"
  3. Specify a version that meets the above condition