Skip to content

Auto import from vscode suggestions fail if existing import path doesn't match TypeScript's preference. #46564

@TheWyo

Description

@TheWyo

Bug Report

Auto imports within vscode suggestions fail if there's an existing valid import path, that doesn't appear to TypeScript's first preference as to where to import from. However, imports do still work via quick actions -> add to existing import declaration.
This appears to be coming from an assertion in the language server rather than from vscode behaviour.

🔎 Search Terms

vscode, auto, import, suggestions,

🕗 Version & Regression Information

  • Based on my testing, this changed between versions 4.3.5 and 4.4.2

⏯ Playground Link

Minimal reproduction repository

Linking to a repo instead of playground as it may involve usage of npm dependencies via file urls in package.json, not sure if workbench supports that.

Reproduction steps:

  1. Clone repro repository
  2. npm install
  3. Open in vs code
  4. Open src/index.ts
  5. Place cursor here: const a = new MyClass|();
  6. CTRL+Space to bring up auto complete suggestions, enter to accept
  7. vscode imports MyClass from "../packages/mylib" (as it's a dependency, from "mylib" would probably be better, but this is a separate issue, related to Auto-import prefers parent index.ts which leads to circular reference #45953 maybe?)
  8. Now place cursor here: const b = new MyClass2|();
  9. Do auto complete from suggestion as in step 6
  10. MyClass2 is added to import - correct
  11. CTRL+Z to undo the import
  12. Change import path from "../packages/mylib", to just "mylib" to use as an npm dependency.
  13. Attempt to import MyClass2 from suggestions again
  14. No import is added
  15. If you check extension host output in vscode, there appears to be a failing debug assertion from tsserver - getCompletionEntryCodeActionsAndSourceDisplay. A copy of this error is included in error.log in the repo for convenience.
  16. Interestingly, if instead of trying to tab complete, you hover over MyClass2, and use the quick actions from the tooltip and pick "Add MyClass2 to existing import declaration from mylib", this does work correctly. Perhaps a different code path?

If you change the version of typescript installed via npm to 4.3.5, reload the vscode window and follow the steps again, this seems to stop reproducing.

💻 Code

Not really applicable as it seems to be related to import functionality from language server.

🙁 Actual behavior

Auto completing while there is an existing an import that isn't TypeScript's first preference for where to import it, fails and nothing is imported.

🙂 Expected behavior

TypeScript should see that the target class can also be imported from the existing import, and add to it.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptCrashFor flagging bugs which are compiler or service crashes or unclean exits, rather than bad outputDomain: LS: Auto-importFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions