question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

auto import in vscode for lib exports not working for new project without explicit include in root tsconfig

See original GitHub issue

Current Behavior

For a new empty project with a nestjs app and a workspace lib called “utils”, when typing the name of an exported function from @org/utils (for example exportedExampleA) in a file within the nestjs app I do not get an auto import suggestion from vscode’s IntelliSense.

Expected Behavior

If I’m typing exportedExampleA in a file within the nestjs app, I would expect vscode to suggest an auto import like import { exportedExampleA } from '@org/utils' so that I can select it without me having to type in the import manually.

Steps to Reproduce

nx g @nrwl/nest:application webapi
nx g @nrwl/workspace:library utils
// libs/utils/src/index.ts
export function exportedExampleA() {
  return 'lol';
}
// apps/webapi/src/main.ts
...
exportedExampleA // type this and expect auto import suggestion

Failure Logs

N/A.

Environment

OS: macOS 10.15.5 vscode: 1.47.1 nx: latest

Workaround

Add the following to the monorepo’s root tsconfig.json and restart vsc:

{
   ...
  "include": [
    "./libs/*/src/**/*",
    "./apps/*/src/**/*"
  ]
}

Auto import now works as expected.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
vsavkincommented, Jul 22, 2020

Unfortunately, we cannot add “include” into the root tsconfig because it is inherited by all the projects. They have to be independent from each other.

Tollowing works:

Add the following to the lib:

export function funcA() {
  return 'lol';
}

export function funcB() {
  return 'lol';
}

Add the following the app:

import { funcA } from '@myscope/mylib' 

Now when type in “funcB”, vscode will autocomplete it. Basically as long as your app imports mylib anywhere in its source, the autocompletion from mylib will work. Otherwise it won’t. You can “fix” it manually by adding path to the lib to tsconfig.app.json of your app, but I personally would not do it. This is a limitation of how vscode autocompletion works. The language service has to know that myapp points to mylib before it can autocomplete tokens from it. The same is true about autocompleting tokens from node_modules.

As a side note, WebStorm doesn’t have this issue because their autocompletion doesn’t rely on the language service.

I don’t think this issue can be fixed on our side, so I’m going to close it.

4reactions
dgrbradycommented, Aug 11, 2021

Just in case anyone else is stumbling through this, I have pretty much a blank NX workspace that I created yesterday with 1 barebones angular app and 1 barebones nestjs app. Both were generated via the NX CLI. Doing these 2 things fixed the issue for me:

  1. Remove node_modules from the excludes property in tsconfig.base.json (only doing this did not fix the issue for me like others have suggested)
  2. Add "../../node_modules/@angular/**/*.d.ts" to the include array in tsconfig.editor.json for your angular app. You may be able to add it in tsconfig.json or tsconfig.app.json, but based on file names alone, I figured it was more appropriate to place in the editor json since this is an editor related issue.
Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript auto imports not working macos - Stack Overflow
In your case, typescript is unable to locate all the files in your project. To locate all the files, modify your include array...
Read more >
jsconfig.json Reference - Visual Studio Code
js doesn't reference a file b.ts explicitly (either using import or CommonJS modules), there is no common project context between the two files....
Read more >
Enable JavaScript auto-import suggestions in VS Code
Now that VS Code knows to treat all your JS files as an explicit project, auto-import suggestions will populate with all your relevant...
Read more >
TypeScript | IntelliJ IDEA Documentation - JetBrains
In the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | General | Auto Import. The Auto Import page opens. In the TypeScript/ ......
Read more >
eslint-import-resolver-typescript - npm
Start using eslint-import-resolver-typescript in your project by running `npm ... If you're facing some problems on rules import/default or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found