VScode Solidity extension - not finding openzepplin imports
Asked Answered
G

3

9

I'm trying to get started with what should be a very simple Solidity contract but VSCode is giving me a hard time. I'm using Juan Blancos solidity plugin but VSCode cannot find openzepplin imports

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

The error is:

Source "@openzeppelin/contracts/token/ERC20/ERC20.sol" not found: File import callback not supported

Even though vscode shows red squigglies I can compile successfully via hardhat compile.

If I change the path to

import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";

VScode stops complaining but then I cannot compile via hardhard, with the error being:

Source "node_modules/@openzeppelin/contracts/security/Pausable.sol" not found: File outside of allowed directories.

My VSCode settings for Solidity extension for both User and Workspace are:

"solidity.packageDefaultDependenciesContractsDirectory": "contracts",
"solidity.packageDefaultDependenciesDirectory": "node_modules"

Which corresponds to my project structure

root 
 |_ contracts
    |_ MyToken.sol
 |_ node_modules
    |_ @openzepplin

I followed the instructions here and have done extensive researching but unfortunately can't get it to working.

Gonick answered 29/4, 2021 at 9:22 Comment(1)
I'm trying to import it VScode using GitHub. import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.1.0/contracts/access/Ownable.sol"; But it's giving me an error This contract not found and File import callback not supported. Using Juan Blanco extension for Solidity.Religieux
L
3

I was facing the same issues. enter image description here Go to the VSCode Settings (Workspace) and type packageDefaultDependencies in the seachbar. enter image description here

Then click the Edit in settings.json at the bottom. enter image description here

Add this line to the settings.json file

"solidity.packageDefaultDependenciesDirectory": "node_modules",

Then the red lines will be removed. enter image description here

Leonaleonanie answered 5/6, 2023 at 14:2 Comment(0)
H
1
Source "@openzeppelin/contracts/token/ERC20/ERC20.sol" not found: File import callback not supported

I had same issue. I had multiple folders added in the unnamed workspace. After removing all other folders and keeping only one project folder resolved issue for me.

Hispanicism answered 11/8, 2021 at 13:25 Comment(0)
U
1

Linting issues If you see something along the lines of:

ParserError: Source "OpenZeppelin/[email protected]/contracts/access/Ownable.sol" not found: File not found.
import "@openzeppelin/contracts/access/Ownable.sol";

In your vscode, these and be safely ignored. However you can also add to your settings to ignore these.

  1. Create a .vscode folder at the root of your project.
  2. Create a file called settings.json Add the following code:
{
  "solidity.remappings": [
    "@chainlink/=/Users/patrick/.brownie/packages/smartcontractkit/[email protected]",
    "@openzeppelin/=/Users/patrick/.brownie/packages/OpenZeppelin/[email protected]"
  ]
}

Or whatever version your @chainlink and @openzeppelin contracts need. For example: enter image description here

For more information see here

Uniform answered 19/3, 2022 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.