Trouble importing OpenZeppelin Contracts into VS Code

My compiler is not recognizing my import. Don't hate me for posting this again. I've clicked through about 10 different forum posts similar to this one but none actually has solved my particular issue after trying.

:computer: Environment

The latest truffle version. I'm using the latest OpenZeppelin code that is downloaded from NPM.

:memo:Details

Long story short I'm new and I've been developing a project that compiles fine before I add an import statement to one of the "@openZeppelin" contracts. I checked the settings and `Solidity: Package Default Dependencies Directory = "node_modules"`

Solidity: Package Default Dependencies Contracts Directory = "" I changed the file structure a few times and I ended up moving node_modules into the same folder as the smart contract I’m importing “@openzeppelin” from and it’s still not working.
Here’s the path I’m using

import "@openzeppelincontracts-upgradeable\tokenERC721ERC721PausableUpgradeable.sol";

I’m not sure what else to do. Yes, it’s installed. I installed it twice just to be sure.

:1234: Code to reproduce

import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721PausableUpgradeable.sol";`

Edit; Typo

1 Like

I think you’re missing a few slashes:

import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721PausableUpgradeable.sol";
//              here ^                      here ^ here ^ 
1 Like

Sorry about that. It must of happened when I was copying the code over.

import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721PausableUpgradeable.sol";

Still gives me the same error

1 Like

I fixed it. for some reason, it needed me to include the root path to the node_modules setting.

Solidity: Package Default Dependencies Directory = "filename/src/node_modules"

1 Like

Hi @presidento23,

Welcome to the community :wave:.

You shouldn’t modify the code in node_modules.

To keep your system secure, you should always use the installed code as-is, and neither copy-paste it from online sources, nor modify it yourself.
From: https://docs.openzeppelin.com/contracts/3.x/#usage

See the documentation on how to use and import the upgradeable version of OpenZeppelin Contracts:

What did you want to change in node_modules?

Feel free to share a cut down version of your contract.

Hello there. I was getting the same import error while trying to compile .sol.
I’m using VSCode + solidity plugin.
The problem here was that I had multiple projects on the same workspace. And even when I changed the workspace compiler version to Remote and set the correct version for the project, I got this error:
Captura de Tela 2021-05-29 às 19.28.42
The @openzeppelin was download correctly as a dependency in my project /node_modules folder. But the compiler was using the rootWorkspace dir as it’s “root”, and I got no /node_modules there.

The interesting was that I could compile using the truffle or hardhat, so it should be something related to relative paths used by the plugin’s compiler and where it was looking for the dependencies.

So I opened up just only that one project I was interested working on, to test if now the VSCode solidity plugin would work, and Eureka!
Captura de Tela 2021-05-29 às 19.25.19
It worked!
I hope you get your project up and running again!

For whom wants to go a little deeper, here is the result of a brief analysis I did on the reasons for that:
I was intrigued by this behavior, so I took a look into the Solidity Extension source code, and I think I’ve find how to fix it, but I need more time for that.
I never had develop a VSCode extension before, but my intuition tells me that the plugin should using

${fileWorkspaceFolder} 

instead of:
vscode.workspace.workspaceFolders[0].uri.fsPath

This is the way the plugin is accessing the definitions for the workspace for every time someone tries to compile the code. But it breaks if you have multiple projects using different solidity versions.

I hope I’ve helped :wink:

source: https://github.com/juanfranblanco/vscode-solidity/blob/7280d587ca192e9755e9e41555a521fff88dbda7/src/compiler.ts#L165

2 Likes

@natanloterio It appears that vscode-solidity just does not support multi-root workspaces yet. This use case is also mentioned in “Support OpenZeppelin Contracts with default settings” issue, which also lists workarounds for getting paths right in other situations so it’s worth taking a look if anyone with a similar problem is reading this.

I think it would be best to just report multi-root workspace support it as a separate feature request in project’s bug tracker.

@presidento23

I fixed it. for some reason, it needed me to include the root path to the node_modules setting.

The reason is that the extension by default expects node_modules in the root directory of your project. If it’s somewhere else, you have to specify the path. Truffle has an elaborate path resolver which can detect it which is why it works. I think the extension does not have anything like that and just relies on you giving it the right paths.

2 Likes