vscode-solidity
vscode-solidity copied to clipboard
Contracts in NPM @org/dep scoped dependencies don't resolve
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 can you post a sample of your code and project structure including dependencies thanks
I have actually seen the issue now on Macs, I'm investigating
@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.
Any workaround for this? Changing the settings and restarting isn't fixing it for me. :(
@chanhosuh what settings are you using?
@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.
And your imports? you may need to remove the "contracts" from packageDefaultDependenciesContractsDirectory and use "" instead.
imports are:
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
But... your advice of removing "contracts" worked! :). So strange.
Yeah mainly that setting is to remove the need to write "contracts" in the import path.
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 sorry I have just seen this you should have "solidity.packageDefaultDependenciesDirectory": "node_modules" as the dependencies directory
Still happens with
"solidity.packageDefaultDependenciesContractsDirectory": "",
"solidity.packageDefaultDependenciesDirectory": "node_modules"

@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 my node_modules is in the root. I tried with pnpm, npm and yarn. None worked :(
@talentlessguy super strange. This is an example structure:


Edit: My settings are the global / default ones for those, you might have overridden them in your workspace?
@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);
}
}
@talentlessguy ^^^
@juanfranblanco thanks, testing right now
could the issue be that my contracts in the contracts folder? maybe bc of this?
No that should not be an issue, i have included the workspace settings to override the global just in case.

@talentlessguy although you shouldn't, have you tried to restart it?
@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
@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.
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.
package.json:
"dependencies": {
"@openzeppelin/contracts": "^4.8.1",
"@uniswap/v2-core": "^1.0.1",
"@uniswap/v2-periphery": "1.1.0-beta.0"
}
Settings:
Thanks for this, have you got any remappings?