Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cag opened this issue Jan 24, 2020 · 26 comments
Open

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

cag opened this issue Jan 24, 2020 · 26 comments

Comments

@cag
Copy link

cag commented Jan 24, 2020

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

@juanfranblanco
Copy link
Owner

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

@juanfranblanco
Copy link
Owner

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

@juanfranblanco
Copy link
Owner

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

@chanhosuh
Copy link

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

@juanfranblanco
Copy link
Owner

@chanhosuh what settings are you using?

@chanhosuh
Copy link

chanhosuh commented Apr 26, 2020

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

@juanfranblanco
Copy link
Owner

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

@chanhosuh
Copy link

imports are:

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

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

@juanfranblanco
Copy link
Owner

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

@deluca-mike
Copy link

deluca-mike commented Oct 21, 2020

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?

@juanfranblanco
Copy link
Owner

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

@talentlessguy
Copy link

Still happens with

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

image

@juanfranblanco
Copy link
Owner

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

@talentlessguy
Copy link

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

@juanfranblanco
Copy link
Owner

juanfranblanco commented Jul 25, 2021

@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?

@talentlessguy
Copy link

@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);
    }
}

@juanfranblanco
Copy link
Owner

Check this:

NFTSample.zip

@juanfranblanco
Copy link
Owner

@talentlessguy ^^^

@talentlessguy
Copy link

@juanfranblanco thanks, testing right now

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

@juanfranblanco
Copy link
Owner

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

@juanfranblanco
Copy link
Owner

image

@juanfranblanco
Copy link
Owner

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

@talentlessguy
Copy link

@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

@juanfranblanco
Copy link
Owner

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

@ErrorEater
Copy link

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

@juanfranblanco
Copy link
Owner

Thanks for this, have you got any remappings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants