ma96o / gialog

MIT License
0 stars 0 forks source link

0804 _ Solidity 開発における Visual Studio Code の警告 #7

Open ma96o opened 2 years ago

ma96o commented 2 years ago

Visual Studio Code で開発を行うと,バックグラウンド分析により警告を表示してくれる. 大体は気になりつつも,その場しのぎで対応してしまっているので,今日は Solidity 開発において初歩的な内容について,いくつか事例を挙げ対応策をまとめた.

1. SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.

現象

スクリーンショット 2022-08-04 21 35 17

原因

SPDX license identifiers を指定していないから Solidity はv0.6.8からSPDX license identifiersを取り入れた

解決策

SPDX license identifiersを明示する(↓は [UNLICENSED] の場合)

+ // SPDX-License-Identifier: UNLICENSED
  pragma solidity ^0.8.15;

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

現象

スクリーンショット 2022-08-04 21 39 16

原因

Visual Studio Code の workspace compiler のバージョンが,指定したものよりも低いから

解決策

workspace compiler をコードで指定したものに揃える

  1. 任意の場所で右クリック => 「Solidity Change workspace compiler version (Remote)」を選択
  2. コードで指定したバージョン以上のバージョンを選択する

ref. How to Change the Solidity Compiler in VS Code - Dapp Dev Tips - Medium

3. Source "XXX.sol" not found: File import callback not supported

現象

スクリーンショット 2022-08-04 21 43 50

原因

node_modules/配下のライブラリを呼んでいないため

解決策

デフォルトのパッケージパスにnode_modules/ を指定

  1. settings.json に↓の記述を追加
    • cmd+shift+p で「settings json」と検索すれば開ける
+     "solidity.packageDefaultDependenciesContractsDirectory": "",
+     "solidity.packageDefaultDependenciesDirectory": "node_modules",

ref. https://ethereum.stackexchange.com/a/111572

ma96o commented 2 years ago

ちなみに,SPDX license identifiersは SPDX を利用してコードのライセンスを明示するもの. そして SPDX とは,Linux Foundation のプロジェクトで、「ソフトウェアのライセンスやコンポーネント,コピーライト,セキュリティーリファレンスといった情報を記述する標準形式」を定めようとするもの. 具体的には,ソフトウェアのパッケージに含まれるデータをやり取りする際の標準的なフォーマットを提供している. HPは https://spdx.dev/ ライセンスリストはSPDX License Listで見ることができる.