Skip to content
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

auto import with tsconfig alias paths to node_modules #51058

Open
paul-vd opened this issue Oct 4, 2022 · 1 comment
Open

auto import with tsconfig alias paths to node_modules #51058

paul-vd opened this issue Oct 4, 2022 · 1 comment
Labels
Domain: Auto-import Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@paul-vd
Copy link

paul-vd commented Oct 4, 2022

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.

@mjbvz mjbvz self-assigned this Oct 4, 2022
@mjbvz mjbvz transferred this issue from microsoft/vscode Oct 4, 2022
@mjbvz mjbvz removed their assignment Oct 4, 2022
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Help Wanted You can do this Experience Enhancement Noncontroversial enhancements Domain: Auto-import labels Oct 5, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 5, 2022
@cabal95
Copy link

cabal95 commented Jun 16, 2024

I think this might be fixed/resolved already. Possibly by multiple PRs related to #53116.

The setup I have is a reference to package @rockrms/obsidian-framework - but I needed to use @Obsidian/* in the import lines - for various historical reasons. To make this work required a couple minor changes.

  1. In the package.json consuming the library put the reference in dependencies instead of devDependencies (putting it in devDependencies was probably a mistake on my part but I had assumed that was correct since it is not bundled at build time and instead provided at runtime by the host application).
  2. Add the alias to tsconfig.json as
    "paths": {
        "@Obsidian/*": [ "./node_modules/@rockrms/obsidian-framework/types/*" ]
    }
  3. Also in tsconfig.json, set moduleResolution to NodeNext.
  4. In the @rockrms/obsidian-framework package, set exports value to ./types/*.d.ts.

All 4 of these steps were required, but it now correctly recommends @Obsidian/* instead of @rockrms/obsidian-framework/*.

image

(Edit: This was tested in VS Code 1.90.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Auto-import Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants