hardhat-vscode
hardhat-vscode copied to clipboard
Yarn berry project cannot find local hardhat module
Create a solidity project using yarn berry, then check the logs in vscode:
Error loading HRE: Error: Cannot find module 'hardhat'
Yarn berry has no node_modules folder. It uses .pnp files to override node's default loading mechanism. I'm unsure at this time how the plugin attempts to find the local hardhat, but if it attempts to search node_modules folder it won't work. If you allow a node interpreter setting similar to eslint, this can be set with a wrapper with pnp files. Alternatively if pnp files exist, they can be automatically loaded by the plugin:
node -r ./.pnp.cjs --loader ./.pnp.loader.mjs
eslint plugin for example allows overriding nodePath: eslint.nodePath, this makes it trivial to create a yarn berry sdk that can easily instruct eslint on how to load the modules needed locally instead of implicitly reading from node_modules
As an alternative for anyone seeing this issue in the meantime, switch your .yarnrc.yml linker to nodeLinker: node-modules. This means you cannot use many of the yarn berry features hence only switch for local development. Of course this process can be quiet tedious so hopefully we can find a more general resolution (I wish there was a generic way that vscode allowed overriding any plugin's node interpreter via work space settings)
Thanks for this report. You are correct we are scanning node_modules to find the version of Hardhat to be used for the project.
We load up hardhat within the extension itself, which is a node process internal to vscode, but the key usage of hardhat is loading the hardhat.config.{ts,js} and that is shelled out to a child process. So that is likely the point to consider injection of other node loader options. We will need to investigate more to get a clearer idea.