Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does VSCode's "Go to definition" fail on my project using Webpack alias?

This is a followup to vscode issue #16320, to which vscode dev Matt Bierner suggests I ask here.


Recent versions of VS Code...

  1. ... have a Go to definition feature, triggered by pressing F12 on a symbol...
  2. ... and also support (via jsconfig.json configuration) webpack aliases, a webpack feature enabling prefixes for import lines for friendlier imports (see vscode documentation for Webpack aliases). For example, aliasing /src/api to * will let me type import foo from */users instead of import foo from ../../../../src/api/users.

But in some uses cases, they fail together. I built a minimal broken test project (zip, 3kB) for my use case. Can anyone give a look at it and see if I'm doing anything wrong or if it looks like a bug?

Extract the zip and see README.md: path autocompletion works, but not "peek" or "Go to definition".

  • Skim through the js files to check I'm not asking you to run anything nefarious.
  • npm install && npm run build && node dist/index.js → Install & build both succeed, indicating Webpack is happy. Run logs [1, 2, 3].
  • Now, run code /path/to/project and open src/index.js On line 5, try to F12 on getLinks → No definition found for 'getLinks' 😢

Am I still doing something wrong in my jsconfig.json, or is this a bug? (the multiple levels of exports, maybe?)

like image 675
Ronan Jouchet Avatar asked Aug 20 '18 13:08

Ronan Jouchet


1 Answers

Accepted answer is correct, except my solution was simply:

import something from '~/something';

jsconfig.json looks like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "~/*": ["./*"]
    }
  }
}
like image 112
minlare Avatar answered Sep 19 '22 00:09

minlare