Skip to content

Exclude specific files from auto import suggestions #35395

Description

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 own assert function to benefit from this feature, VS Code started to insist on importing assert form Node's console module instead, so, for lack of a better solution, I ended up renaming the function.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Activity

  1. raijinsetsu commented on Aug 7, 2020

    @raijinsetsu

    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.

  2. SimpleCookie commented on Aug 10, 2020

    @SimpleCookie

    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.

  3. yuval-gilboa commented on Aug 28, 2020

    @yuval-gilboa

    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:

    module1
    module2
    module3

    We then define a re-exports module like this:

    import * as module1 from './module1';
    import * as module2 from './module2';
    import * as module3 from './module3';
    
    export const lib = { ...module1, ...module2, ...module3};
    

    We then use the lib namespace to access all members of module1...3. But the members of these internal modules are also exposed on the global 'namespace' which pollutes the api surface. This cannot be avoided due to the way the module system works, but if we were able to exclude these files from intellisense auto-import it would make them practically hidden and thus partially solve our problem.

  4. uhyo commented on Aug 28, 2020

    @uhyo
    Contributor

    I also would love to see this feature.
    I recently created a library named rocon and it has react.d.ts at the root directory so that users can import stuff from "rocon/react". The react.d.ts reexports everything from ./lib/react.

    The problem is that TypeScript's auto-import suggestion prioritizes importing from rocon/lib/react rather than rocon/react. So it would be nice to exclude files inslide rocon/lib from the suggestion.

  5. IanEdington commented on Jan 25, 2021

    @IanEdington

    Ryan Cavanaugh (@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?

  6. RyanCavanaugh commented on Jan 26, 2021

    @RyanCavanaugh
    Member

    I liked the prior comment's example of a real-life file layout that caused problems.

  7. richardbarrell-calvium commented on Feb 17, 2021

    @richardbarrell-calvium

    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 JSON.parse(), I'm told that IntelliJ exhibits weird behaviour) when typescript sends the IDE a big auto-complete candidate list.

    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.

  8. richardbarrell-calvium commented on Feb 17, 2021

    @richardbarrell-calvium

    I have a different use-case for asking for the same (or perhaps a different, but very similar) feature.
    ...snip

    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".)

  9. alarbada commented on Mar 13, 2021

    @alarbada

    Disabling autoimports with "typescript.suggest.autoImports" does fine for me, as the Quick fix functionality does the auto importing well enough, even though it would be nice to be able to exclude some folder paths to improve perfomance.

  10. Glandos commented on Jun 28, 2021

    @Glandos

    I have a Vue application with a main.ts:

    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 auth is imported before axios.

    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.

  11. posva commented on Aug 11, 2021

    @posva

    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 vue-demi and is used by the libraries, but never in the application. So when people are using libraries using vue-demi (which points to either Vue 3 or Vue 2 depending on the installed dependencies), the auto-import features automatically adds import { ref } from 'vue-demi' instead of the corresponding import { ref } from 'vue' or import { ref } from '@vue/composition-api'. This is very easy to overlook and can cause very subtle and hard to debug issues.

    Given the existing options of typescript.preferences.import... and some others, maybe there could be an option typescript.preferences.excludeAutoImports: string[] that exclude paths and package names. Maybe even a glob if that even makes sense but I think ignoring a whole folder would be enough.

  12. fgarcia commented on Aug 16, 2021

    @fgarcia

    Maybe the exports keyword from packages.json is not a propper standard yet despite being widely supported, but it could be used as guideline of the preferred import path.

    Whenever I want to import {Logger} from my @app/core/logs package within {repo}/packages/core I am suggested imports from

    • '../../core/src/logs'
    • '../../core/build/logs'
    • '../../core/dist/logs'
    • '../node_modules/@app/core/logs'

    Would it be so strange looking into /packages/core/package.json and guess I just want this?

    • `@app/core/logs'

    Currently it is not even suggested. I would even argue that any relative import that pass by a folder with a package.json file should be removed from the list of suggestions.

  13. vincaslt commented on Sep 10, 2021

    @vincaslt

    @chakra-ui/react is facing the same issue, it's reexporting everything, but autocomplete prefers direct imports from @chakra-ui/button etc.

  14. 44 remaining items

  15. andrewbranch commented on Mar 7, 2023

    @andrewbranch
    Member

    The current implementation doesn't cover nodejs built-in modules, right?

    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 Component gets auto-imported from "../../../../../apps/email-templating/external/__mocks/types/flux-capacitor" when they obviously meant to import from "react". It can definitely be worth revisiting these things from time to time, but my preference is to come up with solutions that don’t make zero-sum trades in sorting/priority.

  16. 0x80 commented on Mar 8, 2023

    @0x80

    Andrew Branch (@andrewbranch) Awesome. For me, adding @types/node/assert.d.ts and @types/node/console.d.ts fixed my problem 👍

  17. 0x80 commented on Mar 18, 2023

    @0x80

    ... but somehow it doesn't work for "log" coming from the debug module. Vscode points to "@types/node/debug/index.d.ts" but including it in the list of exclude patterns has no effect for me.

  18. andrewbranch commented on Mar 20, 2023

    @andrewbranch
    Member

    That file doesn’t exist in @types/node, so I’m not sure what you’re seeing.

  19. 0x80 commented on Apr 2, 2023

    @0x80

    Andrew Branch (@andrewbranch) I got confused. It pointed to @types/debug, an external dependency. Everything seems to work fine after all 🎉

  20. oviniciusfeitosa commented on Sep 20, 2023

    @oviniciusfeitosa
    • Update your settings.json file:
    "javascript.preferences.autoImportFileExcludePatterns": ["**/node_modules/@types/node/**"],
    "typescript.preferences.autoImportFileExcludePatterns": ["**/node_modules/@types/node/**"],
    • Then reload your VSCode
  21. NazmusSayad commented on Jan 16, 2024

    @NazmusSayad

    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
    VSCode version: 1.85.1
    OS: Windows

  22. andrewbranch commented on Jan 16, 2024

    @andrewbranch
    Member

    Currently only workspace-relative paths (starting with ./ or ../) work on Windows. A fix is in progress.

  23. NazmusSayad commented on Jan 16, 2024

    @NazmusSayad

    Got it....

    After using:

    ./**/node_modules/date-fns

    It's working

  24. ev45ive commented on Feb 6, 2024

    @ev45ive

    Andrew Branch (@andrewbranch) This is great work. Very helpful. I can't figure out excluding built-ins though ?

    typing log i get import {log} from "console" and console.log($0) snippet is next option, where import jest gets in the way
    also typing beforeEach i get import {beforeEach} from "node:test" when I have vitest or jest already in imports

    non of these work:

        "typescript.preferences.autoImportFileExcludePatterns": [
            "console",
            "node:test",
            "**/node_modules/@types/node/**",
            "./**/node_modules/console"
        ],
    

    testing on Windows 10
    VSCode 1.85.2
    "typescript": "^5.2.2"

  25. andrewbranch commented on Feb 6, 2024

    @andrewbranch
    Member

    ./**/node_modules/@types/node should do it. Also, leading with ./ shouldn’t be necessary by now if you’re using TypeScript nightly or VS Code Insiders.

  26. 188599 commented on Sep 24, 2024

    @188599

    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 @ionic/angular/common is completely useless, since every single import under @ionic/angular is excluded, including the imports from @ionic/angular/standalone which you'd expect to be allowed, since it's where all the imports you need from their package reside.

    I also found the option typescript.preferences.autoImportSpecifierExcludeRegexes, which as per it's description:

    Specify regular expressions to exclude auto imports with matching import specifiers. Examples:
    
    ^node:
    lib/internal (slashes don't need to be escaped...)
    /lib\/internal/i (...unless including surrounding slashes for i or u flags)
    ^lodash$ (only allow subpath imports from lodash)
    

    should allow me to exclude the other imports while maintaining @ionic/angular/standalone that I need, but it does not seem to work, at all.

  27. andrewbranch commented on Sep 24, 2024

    @andrewbranch
    Member

    Can you open a new issue following the template with repro steps?

  28. 188599 commented on Sep 24, 2024

    @188599

    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 typescript.preferences.autoImportSpecifierExcludeRegexes. I was gonna say that here but I couldn't find where I had posted anymore 😅

  29. applemate commented on May 17, 2025

    @applemate

    I also have a suggestion entry I like to be gone forever. Is there way to remove it ?

    Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.SuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions