-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Offer auto-imports for all files in package.json exports
wildcard directories
#53116
Comments
I might be mistaken, but I'm fairly certain that So are you saying that the issue is that even though this |
Sorry I just learn that |
More detailed example, my project has below structure:
index.d.ts:
utils.d.ts:
package.json
During my developing in src/main.ts, If I type "A", the editor will help me auto add |
This is a known limitation for performance reasons. Originally, auto-imports were never available from files that weren’t already in the program in some way (for files in external libraries, this usually means transitively referenced by a local project file). Then, we started scraping package.json dependencies to see what else we can offer that’s not already in the program. But when we do that, we only look at the package’s entry points ( For a package that doesn’t define Fully lifting this limitation would effectively mean doing a recursive read on all of every project’s direct dependencies. We could make up an arbitrary rule that says we’ll do this recursive read only if an |
exports
wildcard directories
I think we would need to be careful here - before making this change, I would want to see an expansion of the TSServer perf suite that uses the artificial test cases you've used to measure auto-import performace. But the auto-import program looking through files covered under explicit subpath patterns in |
Does the TS Server perf suite report how many completion entries are returned? We will definitely do more work if we find more modules to auto-import from, but there’s a certain level of pay-to-play that’s acceptable there. What we want to avoid is paying a noticeable cost when the response is no better. |
I believe so - here's what it looks like today:
|
If it is a performance trade-off, maybe provide some config for those user who has small codebase or good hardware to take this performance cost would be ideal? Since the auto import may improve the productivity obviously rather than this cost. |
I was debugging this issue today in a monorepo and I wanted to share a few thoughts. As a user, I had expected this to work and started by reviewing our implementation before I came across this issue. I 100% understand the performance concerns and respect the decision by the team, but it might be helpful to allow projects to opt in/out as @pilaoda suggested. It would also be great if TS could warn about this. I'm not sure how that should look, but it would be powerful if I - as a user or an author - received information about this issue and how to fix it. I guess this could be similar to the error for missing types on exports. For now we'll add a script to add exports per module, which will solve this for our monorepo users. |
I plan to measure how costly it is to enable this for wildcard |
Thanks @andrewbranch, amazing work. Will this be in 5.2 at this stage? |
@delaneyb there was a bug around the
into
TypeScript doesn’t actually need the |
Types are automatically inferred as sibling `.d.ts` files here. See microsoft/TypeScript#53116 for extra pre-latest info
@andrewbranch sorry to comment here but I'm facing an issue that is very relevant and it seems this issue has been reopened previously to address other issues and bugs and the context for this problem is well explained here. To briefly explain the issue I'm facing: project structureI'm working with a very standard monorepo structure using
Both the
I've also tried a simpler format:
This is following a recommended strucutre from vercel's examples on setting up turbo a monorepo This project is set up with internal packages that are not independently compiled, so it exports the
Project issuesWhile the monorepo compiles and deploys to vercel just fine, I'm unable to take advantage of intellisense suggestions and auto imports in VS Code using the current typescript setup. It may well be that I'm messing up some other settings but did my best to follow recs and other threads on this topic and unable to get these to work in conjunction with wildcard Once an import is written manually, it is typed correctly, so clearly typescript is able to resolve the types once they've been coded, but while suggestions and auto imports do work inside the packages or the apps, they don't work across packages. I may be wrong in assuming the fact that the packages export As you can see, imports and completion from an import path are working, but auto import suggestions are not: I'm sharing a small reproduction of the issue based on the vercel example I mentioned: The project structure:
|
Adding another simple way to recreate this issue: Create a This will create a If you switch from named exports to a single wildcard:
All existing imports still work, the project builds fine, but suggestions stop working in VS Code. |
@BarakChamo thanks for the detailed report. The previous implementation only looked for declaration files, which redirect to implementation source files only when project references are set up. Fixed at #56848. Bit of a tangent, but it’s still weird to me that these starters and third party docs don’t set up project references just to improve type checking performance. @anthonyshew is that something you considered, or does it not play well with turborepo’s own caching mechanisms? |
Randomly chiming in... 😅 One - kinda big - DX downside of project references is that you must maintain them "manually". They are not derived from Another thing is that sometimes I found the Recently-ish I also realized that within the IDE it still might not always end up speeding that much since a change in a file has to rebuild the program and all of the imported modules, including the types from other in-monorepo packages. In other words, the in-memory cache is cleared quite a bit in a somewhat surprising (for the end user) manner. |
Can't agree more! Project reference should be derived from |
@andrewbranch, @Andarist pretty much wrote my answer. A couple other perspectives to sprinkle on from Turborepo-land:
Sidebar to the sidebar: I wasn't aware of #56848! Awesome! Definitely will incorporate that into our examples when it releases. Between that and |
Some guidance for anyone having this issue in VS Code: There is a setting called You can have it always enabled by setting it to Edit: Also, you should make sure VS Code is using the right version of Typescript. It may be using a version that is different from the one in your project. You can check that by clicking the |
This actually solved a multi-day problem for me, so thanks! I am wondering how This leads to my questions: What is the behavior of |
My npm lib module's package.json
An app depends on this npm module and install it.
The vscode auto import only support types in
import {A} from "my-lib"
resolve tomy-lib/dist/index.d.ts
, and could not find the symbol and type in something likeimport {B} from "my-lib/utils"
resolve tomy-lib/dist/utils.d.ts
.The text was updated successfully, but these errors were encountered: