Is it possible to import openzeppelin contract from github to vscode?

I’m trying to import OpenZeppelin’s Ownable.sol smart-contract directly from github to vscode but It is giving me an error.

Error

Source "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.1.0/contracts/access/Ownable.sol" not found: File import callback not supported

:1234: Code to reproduce

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.1.0/contracts/access/Ownable.sol";

:computer: Environment

Microsoft vscode.
Solidity extension: Juan Blanco

This import is working fine in Remix but not in vscode. Is this kind of import valid for Remix only?

Is this kind of import valid for Remix only?

Short answer: yes, AFAIK currently pretty much only Remix supports URLs in imports in such a way that you can just paste one and have it just work.

Long answer: It could work but with most tools it won’t. The Solidity compiler provides two mechanisms that can be used to make it work with any tool:

  • You can clone the repo and use import remapping to tell the compiler that the content of https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.1.0/contracts/ can be found in a local directory on your compuer.
  • Tools using the JavaScript interface of the compiler (solc-js) can also provide an import callback. I.e. the compiler can ask them during compilation "what is the content of https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.1.0/contracts/access/Ownable.sol" and they can download the file and return the content in response.

Remix uses the callback but it’s pretty much an exception among the tools I know of.

The support for remappings is easier to add and some tools do support them (Brownie, dapp-tools), some don’t (Truffle, Hardhat). vscode-solidity extension does not support them yet but it has been requested (https://github.com/juanfranblanco/vscode-solidity/issues/183) and Juan said recently that he’s going to work on adding them.

Right answer: Remix supports importing directly from npm packages. Just change the import to @openzeppelin/contracts/access/Ownable.sol and it will work in both Remix and vscode-solidity (with vscode extension you of course have to install the package using npm).

Thanks @cameel for such a detailed answer.