microsoft/TypeScript

Auto-import priority rules

theoephraim opened this issue ยท 15 comments

Suggestion

It seems there are many folks struggling with the priority/ordering of VSCode's auto-import (intellisense) suggestions. There are a few different cases, but they seem to be all somewhat related to the general theme of how these suggestions are prioritized.

  • My specific case is that when coding in Vue, the import of watch from fs is always prioritized over vue (/cc @cabal95)
  • Others have issues with code from external modules being prioritized over code defined within their repo
  • Others have issues related to preferring aliases defined in their tsconfig
  • Many edge cases around specific paths suggested when dealing with monorepos...

It would be fantastic if VSCode provided a way to override the priority but it seems that TypeScript's language server suggestions could use some tweaking regardless.

โญ Suggestion

Seems like a fairly reasonable priority would be:

  • code found within the repo/module (preferring aliased paths if they exist, probably preferring "closer" imports over further away in the tree?)
  • code found in other repos/modules defined locally within a monorepo (again preferring aliases)
  • code found in explicit dependencies found in package.json
  • code found in node internals (ex: fs, path)

๐Ÿ” Search Terms

import priority prioritize prioritise auto-import vscode intellisense

๐Ÿ”— Related Issues

open

closed

None of the things you've listed are first-class concepts in TS -- basically, when we're running, it's all just files and/or ambient module declarations. Formal definitions of what it means to "be defined locally in a monorepo" or "be a node internal" would be needed

@RyanCavanaugh - thanks for replying!

Do you think there would be a willingness to add these concepts and get something implemented?

It seems this is the main file involved?

I'm thinking some of the edge cases around monorepos might get pretty nasty, but I would think sorting the explicit imports before node built-in modules may be straightforward.

Adding another similar issue from VS Code repo that was also closed: microsoft/vscode#42104. It seems like on of the issues tracked in this ticket was resolved, but the main issue of import priority was not.

Was going to open an issue about this... thankfully I found this one.

I would add that the way that autosuggest is matching/suggesting imports vs quick fix seems to be inconsistent.

e.g. I want to add the import line automatically:
import { Assessment } from '@shared/models/Assessment';

@shared is an alias to an in-house shared types project in our monorepo.

Two types of problems seem to be apparent:

  • Quick fix prefers a library imported via node_modules, which is almost always non-preferred
    Screenshot 2023-04-06 at 4 04 02 pm

  • Suggestion dropdown doesn't even offer it as an option, even when scrolling all the way down (whether I use 'assessment' or 'Assessment' doesn't change the results)
    Screenshot 2023-04-06 at 4 04 21 pm

Tightening this all up will offer a huge productivity boost.