juanfranblanco / vscode-solidity

Visual Studio Code language support extension for Solidity smart contracts in Ethereum https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity
MIT License
904 stars 194 forks source link

Support OpenZeppelin Contracts with default settings #178

Closed abcoathup closed 3 years ago

abcoathup commented 4 years ago

When importing from OpenZeppelin Contracts installed via npm the default settings of the plugin give a compiler error. File import callback not supported

File import callback not supported

It would be great if importing OpenZeppelin Contracts could be supported with default settings.

The default settings include:

"solidity.packageDefaultDependenciesContractsDirectory": "contracts"

Users need to change the setting from contracts to an empty string.

"solidity.packageDefaultDependenciesContractsDirectory": ""

Example contract with OpenZeppelin Contracts installed (npm i @openzeppelin/contracts)

pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract GLDToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("Gold", "GLD") public {
        _mint(msg.sender, initialSupply);
    }
}
juanfranblanco commented 4 years ago

@abcoathup So i guess the ask is to detect the import and validate the settings?

juanfranblanco commented 4 years ago

@abcoathup, @caleteet was suggesting something similar to detect the solidity version, I guess that will fit rather well in the prompt process.. (prompt for solidity compiler version, if not the same and prompt to set zeppelin settings if not set)

caleteeter commented 4 years ago

Working on it, should have something very soon. The virtual events are attempting to slow me down. :)

thegostep commented 4 years ago

Looking forward to this!

albertov19 commented 3 years ago

Hi guys,

So I was using @abcoathup example in this thread. I grabbed that contract put it into a test.sol file in project root, installed openZeppelin and I do have:

"solidity.packageDefaultDependenciesContractsDirectory": ""
"solidity.packageDefaultDependenciesDirectory": "node_modules"

And I still get the File Import error. Any ideas?

juanfranblanco commented 3 years ago

Hi @albertov19 , those settings are by default now as node_modules has become the standard to distribute libraries. Can you post an screenshot of your project structure with the file?

albertov19 commented 3 years ago

Got it, I had to remove node_modules from the settings that I had and it worked.

Thanks for this great extension!

juanfranblanco commented 3 years ago

Excellent @albertov19 :)

ColdDevil commented 3 years ago

Thanks @albertov19, I can confirm the import via import "@openzeppelin/contracts/access/Ownable.sol" is working when I set the solidity.packageDefaultDependenciesDirectory to an empty string!

Maybe the issue is that I have a web app project and a contracts subfolder in it with an own package.json only for solidity development? This means there is a <root>/node_modules and a <root>/contracts/node_modules folder as well..

juanfranblanco commented 3 years ago

@ColdDevil yes that will be an issue, on discovering the node_modules, i might need to add a full path setting to allow workspaces to set their own environment

vgeddes commented 3 years ago

Hey, I'm still getting the "File import callback not supported" error for Openzeppelin deps despite trying all the following and more:

I suspect its due to the fact that I'm using the workspace feature in VS code. I have this monorepo, and each project within the monorepo is loaded as a workspace folder in VS code.

Specifically, our ethereum contracts are loaded into VS-code using this workspace folder configuration:


{
  "folders": [
    {
      "name": "ethereum",
      "path": "polkadot-ethereum/ethereum"
    },
    // and more
  ]
}
natanloterio commented 3 years ago

We could make the plugin run the solidity specifications from the current development environment ( truffle or hardhat .config files for instance ), and use the /node_module of the current project as the packagesPath. This way, we can keep things simple. What do you think?

juanfranblanco commented 3 years ago

@natanloterio no that will add dependencies that will be required to be extended for every other new toolset, like dapptools, or even openzeppelin etc. That will be a maintenance nightmare. What it will be ideal is to have A standard for a project file.

Edit: As of now the Oz are the default ones as it is more popular.

juanfranblanco commented 3 years ago

Closing this as it is solved, other suggestions can be put in another issue.

mhpaler commented 2 years ago

@vgeddes did you ever figure out a solution for your setup? We're using workspaces as well and continue to have this problem. (ie. the solutions above aren't working)

juanfranblanco commented 2 years ago

@mpaler workspaces are supported now, although in a solution they all carry the same settings. So if you are using oz, your node_modules should be at the route of each workspace.

juanfranblanco commented 2 years ago

@mpaler if you post a shot of your current workspace(s) structure and settings it might be able to understand the issue.

mhpaler commented 2 years ago

Hi @juanfranblanco

vscode

@openzeppelin is in packages/hardhat/node_modules

Thank you! Michael

juanfranblanco commented 2 years ago

In that scenario you could set a path to packages/hardhat/nodemodules or the root will work with the default node_modules either will to have the oz packages npm installed (stating the obvious)

mhpaler commented 2 years ago

@juanfranblanco the following solved it for me:

  "solidity.packageDefaultDependenciesDirectory": "packages/hardhat/node_modules",
juanfranblanco commented 2 years ago

ah great! I guess the one at root may not have had the open zeppelin, but this matches better your project structure.

sgnovo8 commented 2 years ago

Hi, I'm following the OpenZeppelin Developing Smart Contracts "Learn" tutorial, using VSC, and all works fine but I can't seem to resolve a "problem" reported by VSC in connection with a line of code that imports "@openzeppelin/contracts/access/Ownable.sol".

VSC identifies the problem as "Source '@openzeppelin/contracts/access/Ownable.sol' not found: File import callback not supported".

Does anyone know why I get this "problem" and how to resolve it? Those @openzeppelin/contracts/access .sol files are all in my Learn directory, so why is VSC reporting otherwise?

I'm using Truffle.

I've already tried switching the default workspace compiler and modifying the package.json file in the juanblanco.solidity extension but neither change resolves anything.

DrTenma1998 commented 1 year ago

I'm just reverting my solidity extensions to 0.0135 and it worked for me you can try another another version and you'll see I'm using HardHat by the way

juanfranblanco commented 1 year ago

@DrTenma1998 the extension defaults to node_modules as the main package folder. Do you have a remappings.txt file?

juanfranblanco commented 1 year ago

@DrTenma1998 this is a new project just added open zeppelin modules

solidity-simple-setup solidity-simple-setup-goTodefinition solidity-simple-new-contract

yashdev9274 commented 1 year ago

hey @juanfranblanco I'm also getting this same error , can you pls help me too.

here is my project structure:

image