vscode-solidity icon indicating copy to clipboard operation
vscode-solidity copied to clipboard

Contracts in NPM @org/dep scoped dependencies don't resolve

Open cag opened this issue 5 years ago • 26 comments

I have

{
    "solidity.packageDefaultDependenciesContractsDirectory": "",
    "solidity.packageDefaultDependenciesDirectory": "node_modules"
}

in my settings and am trying to import contracts from a scoped package, but am getting an error: Source "@org/dep/contracts/Dependency.sol" not found: File import callback not supported

cag avatar Jan 24 '20 15:01 cag

@cag can you post a sample of your code and project structure including dependencies thanks

juanfranblanco avatar Jan 27 '20 15:01 juanfranblanco

I have actually seen the issue now on Macs, I'm investigating

juanfranblanco avatar Mar 02 '20 06:03 juanfranblanco

@cag I have tested this in Linux, Mac and Windows.. and I have reproduced it, although eventually just changing the settings and restarting vscode has solved the issue.

juanfranblanco avatar Mar 26 '20 16:03 juanfranblanco

Any workaround for this? Changing the settings and restarting isn't fixing it for me. :(

chanhosuh avatar Apr 25 '20 03:04 chanhosuh

@chanhosuh what settings are you using?

juanfranblanco avatar Apr 25 '20 05:04 juanfranblanco

@juanfranblanco here are my extension-specific settings... I put them both into user and workspace settings.

"solidity.linter": "solhint",
"solidity.packageDefaultDependenciesContractsDirectory": "contracts",
"solidity.packageDefaultDependenciesDirectory": "node_modules",
"solidity.compileUsingRemoteVersion": "v0.6.6+commit.6c089d02",
"solidity.enabledSolium": false

Let me know if anything else would be helpful.

I tried multiple versions going back more than a year, and the problem seems to persist. Interestingly, only the latest versions show red marks around the import; the earlier versions error on the import but do not show the red.

chanhosuh avatar Apr 26 '20 17:04 chanhosuh

And your imports? you may need to remove the "contracts" from packageDefaultDependenciesContractsDirectory and use "" instead.

juanfranblanco avatar Apr 26 '20 17:04 juanfranblanco

imports are:

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

But... your advice of removing "contracts" worked! :). So strange.

chanhosuh avatar Apr 26 '20 18:04 chanhosuh

Yeah mainly that setting is to remove the need to write "contracts" in the import path.

juanfranblanco avatar Apr 26 '20 18:04 juanfranblanco

I am getting an error with for import "merkle-trees/libraries/memory/bytes32/standard/merkle-library.sol";

where merkle-trees is in project_root/node_modules

with settings

"solidity.packageDefaultDependenciesContractsDirectory": "",
"solidity.packageDefaultDependenciesDirectory": "",

Obviously I don't have these set correctly, but its quite unclear how to set these.

Any tips?

deluca-mike avatar Oct 21 '20 02:10 deluca-mike

@deluca-mike sorry I have just seen this you should have "solidity.packageDefaultDependenciesDirectory": "node_modules" as the dependencies directory

juanfranblanco avatar Jan 06 '21 13:01 juanfranblanco

Still happens with

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

image

v1rtl avatar Jul 25 '21 10:07 v1rtl

@talentlessguy This is not an issue normally as it is also the default, the only thing I can think is that your node_modules in the root folder.

juanfranblanco avatar Jul 25 '21 12:07 juanfranblanco

@juanfranblanco my node_modules is in the root. I tried with pnpm, npm and yarn. None worked :(

v1rtl avatar Jul 25 '21 12:07 v1rtl

@talentlessguy super strange. This is an example structure:

image

image

Edit: My settings are the global / default ones for those, you might have overridden them in your workspace?

juanfranblanco avatar Jul 25 '21 12:07 juanfranblanco

@juanfranblanco nope I have them set globally and have no .vscode folder in my project.

if this helps, this is my contract code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.5;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract MyNFT is ERC721, Ownable {
    struct Metadata {
        string title;
        string picture;
        string audio;
    }

    event Mint(Metadata metadata);
    event Claim(uint256 id);

    uint256 public constant MAX_TOKENS = 50;

    uint256 private constant PRICE = 50000000000000000;

    bool private _isSaleActive = true;

    using SafeMath for uint256;

    mapping(uint256 => Metadata) id_to_nft;

    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    constructor() ERC721("MyNFT", "MNFT") {}

    // Mint NFT and increment ID
    function safeMint(address to) public onlyOwner {
        uint256 id = _tokenIdCounter.current();
        _safeMint(to, id);
        _tokenIdCounter.increment();
    }

    function _baseURI() internal pure override returns (string memory) {
        return ;
    }

    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }

    // Mint an NFT and add metadata to it
    function mint(
        string memory title,
        string memory picture,
        string memory audio
    ) public onlyOwner returns (uint256) {
        uint256 tokenId = _tokenIdCounter.current();

        Metadata memory metadata = Metadata(title, picture, audio);

        id_to_nft[tokenId] = metadata;

        safeMint(msg.sender);

        emit Mint(metadata);

        return tokenId;
    }

    // Claim and mint NFT
    function claim(uint256 id) external payable {
        require(msg.value == PRICE, "Claiming an NFT costs 0.05 ETH");
        require(_tokenIdCounter.current() <= MAX_TOKENS, "Sold out!");

        // Transfer to seller
        safeTransferFrom(address(this), msg.sender, id);

        emit Claim(id);
    }

    // Toggle sale mode
    function toggleSale() public onlyOwner returns (bool) {
        _isSaleActive = !_isSaleActive;
        return _isSaleActive;
    }

    // Check if sale is active
    function isSaleActive() public view returns (bool) {
        return _isSaleActive;
    }

    // withdraw bobux
    function withdraw() public onlyOwner {
        uint256 balance = address(this).balance;
        payable(msg.sender).transfer(balance);
    }

    function transferTo(address acc, uint256 id) public onlyOwner {
        safeTransferFrom(msg.sender, acc, id);
    }
}

v1rtl avatar Jul 25 '21 13:07 v1rtl

Check this:

NFTSample.zip

juanfranblanco avatar Jul 25 '21 13:07 juanfranblanco

@talentlessguy ^^^

juanfranblanco avatar Jul 25 '21 13:07 juanfranblanco

@juanfranblanco thanks, testing right now

could the issue be that my contracts in the contracts folder? maybe bc of this?

v1rtl avatar Jul 25 '21 13:07 v1rtl

No that should not be an issue, i have included the workspace settings to override the global just in case.

juanfranblanco avatar Jul 25 '21 13:07 juanfranblanco

image

juanfranblanco avatar Jul 25 '21 13:07 juanfranblanco

@talentlessguy although you shouldn't, have you tried to restart it?

juanfranblanco avatar Jul 25 '21 13:07 juanfranblanco

@juanfranblanco i found it out. it didn't work when i have it in a workspace with multiple projects. when opening directly it works.

but it should work for multiple projects in a workspace imo

thanks tho

v1rtl avatar Jul 25 '21 15:07 v1rtl

@talentlessguy thanks, yes it does not work with multiple workspaces, those were introduced much later, so it will break a lot of stuff, I think project files will be the way forward. But that requires to everyone agree to the format.

juanfranblanco avatar Jul 25 '21 15:07 juanfranblanco

I have the same issue: For "openzeppelin" it doesn't complain but for "uniswap" it doesn't recognize the path. If I prepend "node_modules/" for "uniswap" it works.

image image

package.json:

  "dependencies": {
    "@openzeppelin/contracts": "^4.8.1",
    "@uniswap/v2-core": "^1.0.1",
    "@uniswap/v2-periphery": "1.1.0-beta.0"
  }

Settings:

image

ErrorEater avatar Feb 19 '23 01:02 ErrorEater

Thanks for this, have you got any remappings?

juanfranblanco avatar Feb 20 '23 08:02 juanfranblanco