Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TheWyo opened this issue Oct 28, 2021 · 4 comments · Fixed by #46569
Assignees
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output Domain: Auto-import Fix Available A PR has been opened for this issue

Comments

@TheWyo
Copy link

TheWyo commented Oct 28, 2021

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.

@TheWyo
Copy link
Author

TheWyo commented Oct 28, 2021

Just spotted an additional thing, if you reload vscode between renaming the import path (step 12), and attempting to import MyClass2 for the second time (step 13), it seems to add the import just fine. So the issue possibly requires the same language server instance that generated the initial import, and stops reproducing on a fresh instance?

@andrewbranch
Copy link
Member

There have been a lot of auto-import fixes made in the last week. Have you tried this with typescript@next?

@TheWyo
Copy link
Author

TheWyo commented Oct 28, 2021

Just checked for you, still reproduces against 4.5.0-dev.20211028.

@andrewbranch andrewbranch self-assigned this Oct 28, 2021
@andrewbranch andrewbranch added Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output Domain: Auto-import labels Oct 28, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.1 milestone Oct 28, 2021
@andrewbranch
Copy link
Member

👍 thanks for the detailed repro steps. I’ve been looking for a good repro for this particular assertion failure; looking forward to fixing this one, hopefully for good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output Domain: Auto-import Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants