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

2 Likes

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

2 Likes

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"

3 Likes

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

6 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.

3 Likes

Hello everyone,
Can you help me how can I fix it step to step? I do not know how include the root path to the node_modules setting. Please help me :frowning:

Hi @cameel , should we consider this ticket (https://github.com/juanfranblanco/vscode-solidity/issues/237) to be the one to track to solve this issue?

Hi, @natanloterio! I've had an issue just trying to install OpenZeppelin contracts in the first place. I typed the npm install 3 different ways:

npm i @openzeppelin/contracts

npm install @openzeppelin/contracts@v3.0.0

npm install @openzeppelin/contracts@v3.0.0-beta.0

(I was choosing 3.0.0 because that was the version my instructor was using in 
one of my Udemy courses)

And my terminal would act like it was installing it, but it usually spit out a bunch of errors about dependencies that needed fixing (which is normal). I tried to run npm audit fix, and that did not help. Neither did npm audit fix --force.

What did you type to install it? And did you have to open any empty folders or anything to get it right? I don't understand why VS Code doesn't want to install OpenZeppelin. Or have the imports done for the right node_modules folders.

BTW, when I run the npm install, it'll sometimes show up in my mode_modules folder for my entire computer. I need a node_modules folder for my project directory, and it won't make one. Why is that? Thanks for your help in advance, @natanloterio (or anyone who wants to help).

Seems to be the main issue (there's no other open issue about that right now) but I think that creating a new issue with a good description and focusing on the problem you're having here would not hurt. Right now the issue is a bit fuzzy - it's about linting but not really and it's not clear what does and what does not work with multi-root workspaces from user's perspective. The maintainer might not even be aware of all the problems you're having. Also, giving a good use case and showing how much it affects users always helps to better prioritize the issue.

1 Like

ok, I created a new one in case: https://github.com/juanfranblanco/vscode-solidity/issues/284

But you did not mention in the issue that your issue is with multi-root workspaces. Also I thought that your issue is about compilation, not linting.

Well, in any case these problems are best reported in the bugtracker of the extension anyway since this is not a problem in OZ.