VSCode Go to Definition doesn't work for aliased package installed with yarn in a monorepo

19 views Asked by At

In Visual Studio Code, for my JavaScript Node.js project, I expect to be able to put my cursor on a require line (see first code example below) and choose Go to Definition (typically F12), and it should open the file containing the definition of the module (or function) within a sibling package in my monorepo.

But lately I've been using corepack and yarn 4.1.0, yarn install and workspaces. This uses the Plug'n'Play mechanism of yarn and modules are not stored in a node_modules folder. As a consequence, it seems that vscode can't locate the files and Go to Definition won't find the definition.

somewhere in ~/monorepo/main/something.js:

const foo = require('@modules/my_monorepo_module');

I expect to be able to go to the definition of my_monorepo_module or of foo above. But this doesn't work. It shows errors like:

No definition found for foo
No definition found for my_monorepo_module

My main package.json contains a reference to another package located within my monorepo though. e.g.: a file layout like:

~/monorepo/main
~/monorepo/main/package.json
~/monorepo/modules/my_monorepo_module
~/monorepo/modules/my_monorepo_module/package.json

The main package depends on the local monorepo package with an alias beginning with @modules/ to avoid name collisions with npm packages of similar names. And it makes use of workspaces for everything in the modules folder of the monorepo.

~/monorepo/main/package.json:

{
  ...
  "dependencies": {
    "@modules/my_monorepo_module": "../modules/my_monorepo_module"
  },
  "workspaces": [
    "../modules/*"
  ]
}

Note that Go to Definition generally works on typical symbols and local required files just fine. I suspect this problem has something to do with the way yarn resolves the files vs. the way vscode expects to locate them.

How can I make Go to Definition work in this yarn-monorepo scenario?

0

There are 0 answers