vscode-solidity: Source "@openzeppelin/contracts...." not found: File import callback not supported

Hi all,

I need some help trying to make this extension work with the @openzeppelin/contracts NPM package. I know issues like this have been raised in the past, but I tried a lot of things and I still keep getting the same error (or similar). Here’s what I do to initialize a simple Solidity project:

mkdir ack && cd ack truffle init truffle create all DeedRepository npm init -y npm install @openzeppelin/contracts

Then I open VS Code and edit my DeedRepository contract like this,

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.7;

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

contract DeedRepository is ERC721 {
  constructor(string memory _name, string memory _symbol) ERC721(_name, _symbol) {}
}

Here’s a picture where you can see both the directory structure and the code side-by-side:

image

And the ERC721.sol contract is in there as well

image

The original error

Even before trying to compile this contract, I already have an error and a warning from the extension:

image

Btw, these are my default settings. I see no mention of Solidity in there at first,

image

So I go ahead anyway and try to compile the contract by right-clicking on the screen and selecting “Solidity: Compile Contract”. This is the compiler output:

image

1st solution

I read on the Project Structure and Remappings section of the README that I should add a couple of settings. I add them so that the settings file looks like this:

image

Second error

Then I try to compile the contract again the same way as before, by right-clicking on the screen and selecting “Solidity: Compile Contract”. I get a slightly different output this time, but still no luck:

image

image

Keep in mind that the full path underlined in red on the image above actually exists on my PC.

Compiling the code through solc

I thought this might be a configuration issue with the extension itself, so I tried to compile the contract by calling solc directly

image

Reading solc docs, I see that there are a couple of arguments that I can set to tell the compiler where to look for dependencies. So I call it again and pass those arguments. It works:

image

Conclusion

I’m guessing that the values for the following settings

“solidity.packageDefaultDependenciesContractsDirectory”: “”, “solidity.packageDefaultDependenciesDirectory”: “node_modules”

eventually make their way into compiler arguments, but I can’t seem to find the appropriate values. Could someone please help me find the correct values given my project structure?

PS, compiling through the extension works if given a relative path

image

I really would like to make it work with the following import syntax, though

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27 (8 by maintainers)

Most upvoted comments

I manually add this in .vscode/setting.json and it works for me

  "solidity.remappingsUnix": ["@openzeppelin/=node_modules/@openzeppelin/"]

source: https://stackoverflow.com/a/72168392

This is my setup for this project:

image image

I can confirm that adding the remappingsUnix setting in .vscode/setting.json works on MacOs with a similar project structure. @juanfranblanco I think it is safe to close this issue.

Not great, but the best solution I have found so far is to downgrade extension version to v0.0.135

The extension needs to identify where is the root in that scenario so if you have foundry.toml, hardhat.config, truffle or remapping.txt file there it will use that as the root. So on your screenshot that will be the sol folder. Try copy and pasting your remappings into a remappings.txt file on the sol folder. Or open the sol folder directly, which it maybe why you see the issue again.