Microsoft/TypeScript

auto import with tsconfig alias paths to node_modules

Open

#51,058 opened on Oct 4, 2022

View on GitHub
 (4 comments) (10 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
Domain: LS: Auto-importExperience EnhancementHelp WantedSuggestion

Description

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.71.2
  • OS Version: Windows 11

Steps to Reproduce:

  1. Configure a tsconfig.json which has alias paths to node_modules

    {
      "compilerOptions": {
        "allowJs": true,
        "jsx": "react",
        "baseUrl": ".",
        "checkJs": true,
        "noEmit": true,
        "esModuleInterop": true,
        "resolveJsonModule": true,
        "paths": {
          "theme/*": [
            "./src/web/theme/*",
            "./node_modules/my-library/my-module-a/web/theme/*",
            "./node_modules/my-library/my-module-b/web/theme/*",
            "./node_modules/my-library/my-module-c/web/theme/*",
          ],
          "web/*": [
            "./src/web/*",
            "./node_modules/my-library/my-module-a/web/*",
            "./node_modules/my-library/my-module-b/web/*",
            "./node_modules/my-library/my-module-c/web/*",
          ],
          "server/*": ["./node_modules/my-library/src/server/*"],
          "config/*": [
            "./src/config/*",
            "./node_modules/my-library/my-module-a/config/*",
            "./node_modules/my-library/my-module-b/config/*",
            "./node_modules/my-library/my-module-c/config/*",
          ],
        }
      }
    }
    
  2. Try to import an existing file under the alias in node_modules

The autocomplete would prefer to use the my-library as a package instead of the shorter alias path, for example it would suggest:

import Image from "my-library/my-module-a/web/theme/Image"
import Image from "my-library/my-module-b/web/theme/Image"
import Image from "my-library/my-module-c/web/theme/Image"

But it will not suggest:

import Image from "theme/Image"

although that alias works perfectly fine when adding it manually.

Contributor guide