-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Exclude specific files from auto import suggestions #35395
Comments
We have a similar issue in a large project: a legacy set of code that no new code should reference but which must stay where it is for compatibility. Being able to instruct VSCode to ignore the files in that folder for the purposes of auto-import would be VERY beneficial. Currently, we're looking into whether or not ESLint can at least cause an error but I'm not sure it supports restricting relative modules like that. |
I would also love to be able to exclude certain files. In my case I need a certain CSS file included AFTER another, however the organizeImports config automatically sorts them in the wrong order. |
An additional use case we have is to enable splitting implementation of a large namespace to multiple module files but still expose it under a single namespace. This would allow us to have better control over shape of exposed apis without being tied to the internal module structure we use. For example, assuming we have three module files:
We then define a re-exports module like this:
We then use the |
I also would love to see this feature. The problem is that TypeScript's auto-import suggestion prioritizes importing from |
@RyanCavanaugh It looks like this ticket generated quite a bit of feedback since you added that label. Is there any other specific feedback you're looking for? |
I liked the prior comment's example of a real-life file layout that caused problems. |
I have a different use-case for asking for the same (or perhaps a different, but very similar) feature. I have a codebase which uses a couple of libraries which have thousands of types defined in them (e.g. the "aws-sdk" library has, by my count, 28MB of ".d.ts" files). These libraries are only used in a couple of places in this codebase. It appears that their entire contents get considered for auto-suggestion everywhere. I believe this makes IDEs misbehave (I see VS Code spends >500ms in Since I only use those libraries directly in a handful of files, it would be very useful to be able to omit them from typescript's auto suggestions. It would be even more useful to be able to turn auto suggest for them back on in just the specific files where I do want to use them. As a workaround, I'm considering splitting my project into multiple packages (in one repo) so that the main top-level package doesn't have a direct dependency on the big libraries & their types aren't visible in it. |
Issue #32174 looks like it's describing the same performance problem that I'm having & I'm looking for a way to work around. (FWIW I personally don't view it as a bug: expecting an analysis tool to be fast when feeding it 28MB of .d.ts files seems unreasonable to me, so I'd be ecstatic with a workaround for it rather than a "fix".) |
Disabling autoimports with "typescript.suggest.autoImports" does fine for me, as the |
I have a Vue application with a import Vue from 'vue'
import './plugins/logger'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'
import i18n from './i18n'
// Auth needs axios and vuex (in store) defined
import './plugins/auth'
import vuetify from './plugins/vuetify'
// Make sure to register before importing any components
import './class-component-hooks'
import './plugins/caches'
Vue.config.productionTip = false
new Vue({
router,
store,
vuetify,
i18n,
render: h => h(App)
}).$mount('#app') I am using a code action on save to organize imports automatically. On this very file, I have: import Vue from 'vue'
import App from './App.vue'
// Make sure to register before importing any components
import './class-component-hooks'
import i18n from './i18n'
// Auth needs axios and vuex (in store) defined
import './plugins/auth'
import './plugins/axios'
import './plugins/caches'
import './plugins/logger'
import vuetify from './plugins/vuetify'
import router from './router'
import store from './store'
Vue.config.productionTip = false
new Vue({
router,
store,
vuetify,
i18n,
render: h => h(App)
}).$mount('#app') As you can see, the import order is now wrong, since Organize Imports is a very good features, especially on save, but I need to exclude this specific file from it. Even an inline comment could do the trick. |
This would be really useful for Vue as some libraries use an intermediate package in order to support Vue 2 and Vue 3 at the same time. The package is named Given the existing options of |
Maybe the Whenever I want to import
Would it be so strange looking into
Currently it is not even suggested. I would even argue that any relative import that pass by a folder with a |
|
Vue 3 project here and the problem in my case is yup. It exports types that clash with practically all primitive types, so if I want to define Been Googling this issue for hours, but so far haven't found a solution outside of getting completely rid of |
This has become an issue in the last few weeks that did not use to occur. If I try to auto import standard React functions or types, the default suggestion is coming from a different folder in node_modules. This consistently breaks my build and always takes me a minute to figure out. I would love to be able to add 'react-transition-group' to a blocked auto import list. |
@HaveSpacesuit that's #45784, fixed by #45792 |
@Dids can you open a new issue for your problem? I believe globals are supposed to prevent auto-imports of the same name from showing up at all, but the rules around this have changed a couple times. It’s worth looking into though. As a general rule, if auto-imports is doing something that objectively makes no sense, please file a new issue instead of commenting on this one for better visibility. This thread is about excluding things that we really have no way of knowing to exclude without the user telling us explicitly, like #35395 (comment). Thanks! |
…t autocompletions from dist. This is a workaround to prevent a regression for microsoft/TypeScript#47184 If microsoft/TypeScript#35395 (Exclude specific files from auto import suggestions) is implemented, we should just use that instead.
…t autocompletions from dist. This is a workaround to prevent a regression for microsoft/TypeScript#47184 If microsoft/TypeScript#35395 (Exclude specific files from auto import suggestions) is implemented, we should just use that instead.
…t autocompletions from dist. This is a workaround to prevent a regression for microsoft/TypeScript#47184 If microsoft/TypeScript#35395 (Exclude specific files from auto import suggestions) is implemented, we should just use that instead.
…t autocompletions from dist. This is a workaround to prevent a regression for microsoft/TypeScript#47184 If microsoft/TypeScript#35395 (Exclude specific files from auto import suggestions) is implemented, we should just use that instead.
Hi, @fo-fo, @acarroll-trend and others, I made VS Code extension (essentially a TS plugin) that should satisfy your requests. Ignoring or changing sorting of import suggestions per symbol: Configuration examplesTo disable (ignore) specific imports from any module: "tsEssentialPlugins.suggestions.ignoreAutoImports": [
"path/*#resolve,join", // ignore resolve and join from `path` module and any subpaths of it
"path/win32" // ignore all imports from this module specifically
], #35395 (comment), It ensures these two imports will appear on top. For example when running Fix all missing imports quick fix, "tsEssentialPlugins.autoImport.changeSorting": {
"isEqual": [
// ensures these two will always be on top even if other libs are installed
"lodash-es/isEqual",
"lodash-es"
]
} While it should work from quick fix, it doesn't work from autocomplete, because AFAIK reexports takes precedence in autocomplete, I can add a setting for that, but I'm not sure of format that would cover all cases. |
@andrewbranch
Let's say I have this project structure:
Where {
"typescript.preferences.autoImportFileExcludePatterns": [
"/**/node_modules/react-i18next"
]
} In |
That wasn’t the intention—it’s really just supposed to be individual files that are ignored. It sounds like that could be a bug. Are |
Yes, the instructions say to install both of them. |
@GabenGar would you mind opening a new bug to track this? I’ll take a look at both issues you mentioned. |
@andrewbranch a bug? Isn't it like at least 3 (potential) bugs?
|
I don’t understand what your third bullet point means, but yes, there are two potential bugs. Feel free to open two separate ones if you prefer. |
The path resolution rundown says this:
What does it mean in context of multiroot workspaces, especially if it's in |
It resolves relative to the “workspace folder” of the file being edited ( |
The current implementation doesn't cover nodejs built-in modules, right? The two that are bothering me the most are "assert" and "debug", but adding them to the config doesn't seem to have any effect. These two wouldn't be a problem if there were a way to let TS always prefer functions from your own codebase over external modules. That may be easier to implement? Seems like a useful feature anyway. I don't know why you'd want to ever give priority to external modules when you have the same named functions within your codebase, so maybe it could even be default behavior... The reason the built-in modules are messing things up is that I have functions named |
It does. You just have to specify filenames or globs that cover the file(s) where those ambient modules are declared (whatever shows up in Go To Definition on the module specifier). Every time someone proposes a very reasonable sounding change to auto-import heuristics, we find that implementing it makes the experience worse for ~50% of people. When “local” modules are given priority, people in really large codebases get mad that |
@andrewbranch Awesome. For me, adding |
... but somehow it doesn't work for "log" coming from the debug module. Vscode points to |
That file doesn’t exist in @types/node, so I’m not sure what you’re seeing. |
@andrewbranch I got confused. It pointed to @types/debug, an external dependency. Everything seems to work fine after all 🎉 |
"javascript.preferences.autoImportFileExcludePatterns": ["**/node_modules/@types/node/**"],
"typescript.preferences.autoImportFileExcludePatterns": ["**/node_modules/@types/node/**"],
|
I wanna exclude all the import suggestions from "data-fns". {
"typescript.preferences.autoImportFileExcludePatterns": [
"date-fns",
"**/date-fns",
"**/date-fns/**",
"**/date-fns/**/*",
"node_modules/date-fns",
"**/node_modules/date-fns",
"**/node_modules/date-fns/**",
"**/node_modules/date-fns/**/*"
],
"javascript.preferences.autoImportFileExcludePatterns": [
"date-fns",
"**/date-fns",
"**/date-fns/**",
"**/date-fns/**/*",
"node_modules/date-fns",
"**/node_modules/date-fns",
"**/node_modules/date-fns/**",
"**/node_modules/date-fns/**/*"
]
} I have tried these but still not working. Typescript version: 5.3.2 |
Currently only workspace-relative paths (starting with |
Got it.... After using:
It's working |
@andrewbranch This is great work. Very helpful. I can't figure out excluding built-ins though ? typing non of these work:
testing on Windows 10 |
|
I'm working on an Ionic using Angular's standalone components and they give you the following settings on a newly generated project: "typescript.preferences.autoImportFileExcludePatterns": ["@ionic/angular/common", "@ionic/angular"] But it doesn't seem like it does what is intended, because firstly, the first entry I also found the option
should allow me to exclude the other imports while maintaining |
Can you open a new issue following the template with repro steps? |
As soon as I said that here I saw that there was a new VS Code update, turns out that fixed the issue immediately for |
Search Terms
autoimport, exclude, ignore, isolation, encapsulation
Suggestion
We need a way to exclude files from auto import. Not from the project, only from the auto import suggestions.
Use Cases
My project includes a .d.ts file (generated from the previous version of the project) with legacy data structures (interfaces), many of which have the same names as their counterparts, newer data structures. As that file uses
declare module "..." { ... }
syntax, imports from it are non-relative, which is why auto import gives them a higher priority whereas modules with the newer structures have relative paths. The result is that the 'Add all missing imports' command constantly creates lots of wrong imports. If only there were a way to suppress suggestions for that specific module. I want to import from it only in rare cases.I feel like this problem kind of defeats one of the important points of modules: I wanted to isolate those legacy definitions inside their module and forget about them, but because of this auto import issue they constantly get in the way.
Another example. Type definitions for libraries are in no hurry to use the new
asserts
feature of TS 3.7 for compatibility reasons. When I defined my ownassert
function to benefit from this feature, VS Code started to insist on importingassert
form Node'sconsole
module instead, so, for lack of a better solution, I ended up renaming the function.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: