Skip to content
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

VSCode's auto import not suggesting packages since typescript: "4.2.1-rc" #56166

Closed
ciriousjoker opened this issue Oct 21, 2023 · 6 comments
Closed
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@ciriousjoker
Copy link

ciriousjoker commented Oct 21, 2023

🔎 Search Terms

many permutations of the following keywords:

vscode, intellisense, auto, import, typescript, typescript language server, tsconfig, node_modules

Also explicitly scanned the FAQ for "import".

🕗 Version & Regression Information

  • This is an issue with VSCode's intellisense not suggesting the correct imports
  • This changed between versions 4.1.5 (works) and 4.2.1-rc (broken). There's only one version between (4.3.0-dev.20210211) and it's also broken.
  • This changed in commit or PR _______ no idea how to test this
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://codesandbox.io/s/demo-fail-imports-kwrxd6?file=/main.ts

💻 Code

The broken behavior can be demonstrated with the codesandbox link. However, the correct behavior can only be demonstrated by cloning this repository:
https://github.com/ciriousjoker/demo_fail_imports

The code is the same, but codesandbox.io doesn't support switching between typescript versions and other solutions like stackblitz don't support auto imports at all. Again, remember to restart the TS server between Typescript version switches.

🙁 Actual behavior

Import isn't suggested.

CleanShot 2023-10-21 at 02 29 06@2x

🙂 Expected behavior

Import should be suggested.

CleanShot 2023-10-21 at 02 29 30@2x

Additional information about the issue

  1. Removing any single of the dependencies "fixes" the issue, but that's not a feasable solution (npm install isn't even necessary, just altering the package.json triggers an "update" in this regard)
  2. Given that this worked with previous Typescript versions, it seems like this is a Typescript bug, not a bug with the dependencies
  3. Dependencies shouldn't break suggestions if npm install worked successfully (in my opinion)

I'm aware that typescript has some magic sauce to determine when to load type definitions, ie the "you have to import it once for Typescript to find it". However, I believe that with this minimal reproducible sample repo this (in my opinion) bug might be fixed.

@IllusionMH
Copy link
Contributor

Why your repo doesn't have tsconfig.json? I suspect it would fix problem with proper module option set

@ciriousjoker
Copy link
Author

ciriousjoker commented Oct 21, 2023

@IllusionMH Good catch, I kind of forgot about it since it didn't make a difference. I added a generic one (generated via npx tsc --init on v5.2.2) to the repo & the codesandbox. Using the one generated in v4.1.5 made literally no difference so I used the newer one just in case.

TLDR: Added tsconfig.json, issue still exists

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Oct 23, 2023
@RyanCavanaugh
Copy link
Member

Symbols can only be auto-imported when the relevant project is "in your project", i.e. has been imported at least once already (either directly or indirectly).

This changed as a result of a needed performance improvement, since some packages ship way too many / too large .d.ts files and aren't needed from code.

@ciriousjoker
Copy link
Author

@RyanCavanaugh

I'm not sure I fully understand.
I get that this is a performance optimization, but I don't see how removing specific dependencies causes it to work again. Is the optimization only enabled while the number of .d.ts files from all dependencies is lower than a threshold? It can't be the number of dependencies since replacing individual dependencies also fixes it.

Can this optimization be disabled?
Maybe create a .ts file that imports all dependencies from package.json? Haven't tried that yet, might work.

For context:
We have an nx monorepo with one relatively large "app" and we'd like to refactor it into many smaller apps and libraries. In the big app, imports are fine since someone has probably imported the dependency at least once. However, with smaller apps, the chance is essentially 0 for any given dependency to be imported already, since it seems like every app is counted separately in this regard...

@RyanCavanaugh
Copy link
Member

There's a cutoff of 10 dependencies, again for performance. You can check the TS Server log which has a fairly verbose output of what auto-import is doing and why.

There are a bunch of ways to get these files into your program for analysis purposes; a file importing the ones you explicitly want auto-import for is certainly one of them and probably the easiest to reason about.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

4 participants