-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Avoid barrel re-exports in auto-imports #51418
Comments
Can you set up a sample repo, and also clarify the "expected result" ? Are you expecting a different import path to be generated, or not to be shown that import suggestion at all? |
@RyanCavanaugh I have created the repo: https://github.com/TheColorRed/vscode-autoImportFileExcludePatterns Steps:
You will then notice the auto import imports the full path to the file: import { readFile } from '@org/core/src/files';
readFile Now if you turn off the setting import { readFile } from '@org/core';
readFile Having this setting works the way I want when referencing a file in the current mono-repo project (aka when Here is what I am looking for:
Here is what is happening:
|
I ran into a similar issue with this. It would be great if there were a way to exclude "index.ts" from being suggested directly while still allowing other specifiers that resolve to a file called "index.ts". |
@andrewbranch Any chance this could fit in with some of the ongoing auto-import improvements? Would make a huge difference to me as currently I either get imports from my |
This is super working-as-intended, and the desired behavior I think clearly doesn’t map onto Auto-imports has a preference toward barrel files because they usually indicate the most public export of something and result in the shortest path. I would like to move away from this preference since barrel files often make build tools work poorly, but there needs to be a stronger proposal for how to make this shift, and it needs to be motivated by objective reasons, not code style aesthetics. In the example project here, if you don’t want to import from |
I feel instinctively opposed to a setting like “avoid barrel re-exports in my own files but not from my dependencies.” There are many reasons barrels are bad; the circularity problem is just one. If auto-imports is making a circularity that could have been detected, or at least looks suspicious, we should try to fix that universally, not behind a setting. I did some work like that in #47516. But given all the other barrel problems, it begs two questions:
To me, this makes it feel like we kind of don’t need any additional settings. If you want short import paths from node_modules but you don’t want to import from your own barrels, maybe you should stop writing barrels in your code. On the other hand, if we changed settings for everyone to avoid barrels everywhere, and people started complaining about getting unwieldy auto-import paths for their dependencies, then maybe those dependencies should define |
@andrewbranch Maybe I should have described my use case more clearly but I don't use barrel files (or at least in the sense I normally hear them described). I only have a single I have a paths config like this that allows me to avoid having to build to see my runtime or type changes reflected e.g. in tests: "paths": {
"arktype": ["./ark/type/main.ts"],
"@arktype/util": ["./ark/util/main.ts"],
"@arktype/fs": ["./ark/fs/main.ts"],
"@arktype/schema": ["./ark/schema/main.ts"],
"@arktype/attest": ["./ark/attest/main.ts"]
} If you have another suggestion as to how to achieve this that doesn't result in the problematic suggestions but does allow importing from a path like |
@andrewbranch Rereading your second comment, I understand more what you're suggesting now, but top-level discoverability for the API of a library feels valuable. I would definitely not want to give that up just to avoid some obnoxious auto-imports that worst-case scenario I can manually fix. Granted it is grating given how often these kinds of imports occur, but I don't know if I can recall a library that doesn't provide some top-level export and I think there are good reasons for that. |
IMO you should never use {
"exports": {
"ts": "./src/main.ts",
"default": "./dist/main.js"
}
} then set your test runner / whatever to use the
|
@andrewbranch I actually had this kind of setup originally but it felt like a lot of extra ceremony for no extra benefit compared to using paths, at least within my own repo. I also had the same problem with autoimports with that set up. |
@andrewbranch Any new ideas on this one? I've been talking with @colinhacks about strategies for developing in a TS monorepo with no build step as documented in his blog post here. This is one of the biggest downsides to either of these approaches. I don't see any way around having at least one "barrel file" for your API entry point that doesn't involve freezing your file structure in order to have a stable release. |
I’m open to trying a |
@andrewbranch I'm sure that could be useful in some situations but I expect it would result in imports resolving to arbitrary source files across packages in a setup like the one I'm describing. I'm far from committed to tsconfig paths if there's a better way to achieve this that avoids the autoimports issue. What I'm really wondering is do you...
My first intuition was that perhaps |
I’ve been using
No, it’s both possible and desirable, just nitpicky design questions remain: #55092 |
Yeah I think for now it would fall victim to the same underlying problem, I will keep an eye on the issue and add some context there. |
If I’m understanding you correctly, it just depends on how you set up the |
@andrewbranch What you described does explain how What I meant about the same underlying problem though is that given that such a setting doesn't exist currently, both tsconfig paths and package.json exports would be affected by this issue, correct? |
One major difference between |
@andrewbranch Sure but I'm already using exports for that reason and I mostly just have one entry point per package so this doesn't really impact me. |
Then I don’t understand what problem you’re currently having 😅 The OP complained about auto-imports like |
I have a package To avoid importing from the API entrypoint from within "typescript.preferences.autoImportFileExcludePatterns": [
"./ark/schema/api.ts"
] However, if I do this, I no longer get autoimports from Maybe it is because I only have a single root-level tsconfig that is shared by both packages during dev? EDIT: it seems like this happens even if I create a tsconfig.json in each individual package. |
If you rename |
@andrewbranch Is I wanted to avoid it because of the association with directory imports but I would definitely switch back if it is the only way to get that behavior for now. |
TypeScript/src/services/codefixes/importFixes.ts Lines 1344 to 1358 in 4ada270
It was originally intended primarily to avoid module specifiers like |
@andrewbranch I think that works! 🎉 Thank you so much- that has been plaguing me since long before I commented on this a year ago 😅 Would definitely be interested if other file names are supported so I can switch back, but I care 95% less about that than being able to import across packages easily. |
It actually might be possible to do this cheaply without heuristics, as long as the file you’re currently editing exports something... the data structure central to auto imports can answer whether an arbitrary symbol exported from your current file can also be imported from the file we’re generating an import to, detecting a circularity. We’re well set up to do that for individual exports, just not whole modules, so we’d do well for |
I'm generally exporting a specific subset of symbols in a package entry point to make the API as explicit as possible. The I guess the biggest downside is discoverability as I never would have guessed changing the file name would fix this. |
Bug Report
🔎 Search Terms
Bug
When using the setting
typescript.preferences.autoImportFileExcludePatterns
, it works, however, in a mono-repo I cannot import from another package's root without using the full path to the file when excludingindex.ts
files.I have a workspace
package.json
that looks like this:In the
package.json
ofpackages/core
I have this:Then in the
packages/a
, I try to import a file frompackages/core
, but autoimport imports something like this even though it is exported through the main file:The
package.json
forpackages/a
The
tsconfig.json
for both:Expected Result
This might be a feature request, but I think it would be great if barrel files could be ignored in the auto-import if within the same module/package, as this can often lead to circular dependencies that are hard to track down.
The text was updated successfully, but these errors were encountered: