1

Please take a look at my environment details.

Environment

  • Node installed with nvm
  • Truffle is installed in different node environments which can be switched by simply changing the node by nvm use v16.17.0
  • In my vs code I have installed Truffle for VS Code extension, which is also tightly dependent on solidity extension.
  • I have installed Ganache desktop application.
  • I also have solcjs installed as npm package in one of my node versions.

Contract

// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.4.25 <0.9.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract CallerContract is Ownable {

}

Project Structure The node modules are installed one folder behind the truffle project for contract as shown below.

├── caller
│   ├── build
│   ├── contracts
│   ├── migrations
│   ├── test
│   └── truffle-config.js
├── oracle
│   ├── build
│   ├── contracts
│   ├── migrations
│   ├── test
│   └── truffle-config.js
├── node_modules
├── package-lock.json
├── package.json

Output

  • truffle compile works fine in terminal by using truffle compile in all the node versions.
  • In VS code truffle extension's Truffle contract explorer part successfully compiles the contract.

Failure

  • I am facing one failure and that is when I right-click Solidity: Compile Contract. It outputs
Retrieving compiler information:
Compiler using default compiler (embedded on extension), solidity version: 0.8.9+commit.e5eed63a.Emscripten.clang
ParserError: Source "@openzeppelin/contracts/access/Ownable.sol" not found: File import callback not supported
 --> path to contracts/contracts/CallerContract.sol:7:1:
  |
  | import "@openzeppelin/contracts/access/Ownable.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Compilation failed with 1 errors

And I have tried many ways to resolve this as mentioned here, mainly this solution.

"solidity.packageDefaultDependenciesContractsDirectory": "",
"solidity.packageDefaultDependenciesDirectory": "node_modules",
Asim Khan
  • 508
  • 1
  • 7
  • 21

2 Answers2

1

I am in the same situation. I tried the vscode settings also to no avail. Googleing in circles seems to be an environment issue.

My workaround is to prepend the imports with the path as such:

import "../nodemodules/@openzeppelin-etc".....

And then when I need to actually compile in Hardhat I change them back Not ideal - hopefully someone has a better answer.

0

I had the same problem. What resolved this for me was going to the Solidity extension settings, then Package Default Dependencies Directory, then click on Edit in settings.json and adding this line:

    "solidity.packageDefaultDependenciesDirectory": "node_modules",

The error went away without restarting VS Code. I hope this helps!

Michelin Man
  • 1,013
  • 8
  • 9