Trouble importing OpenZeppelin Contracts into VS Code

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