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 package.json#imports does not take moduleResolution into account #60405

Open
alan910127 opened this issue Nov 4, 2024 · 4 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@alan910127
Copy link

alan910127 commented Nov 4, 2024

🔎 Search Terms

"subpath", "imports", "package.json", "moduleResolution"

🕗 Version & Regression Information

⏯ Playground Link

alan910127/ts-60405-repro

💻 Code

// package.json
{
  "imports": {
    "#*": "./src/*"
  },
  "devDependencies": {
    "typescript": "5.6.3"
  }
}
// tsconfig.json
{
  "compilerOptions": {
    "moduleResolution": "Bundler"
  }
}
// src/foo/bar/baz.ts
export function baz() {}
// src/one/two/three.ts
// actual   - import { baz } from "#foo/bar/baz.js";
// expected - import { baz } from "#foo/bar/baz";

🙁 Actual behavior

When auto-importing the function baz (either through completion or code action), the typescript language server will insert an import statement with a .js file extension.

import { baz } from "#foo/bar/baz.js";

🙂 Expected behavior

Since we are using "moduleResolution": "Bundler", it should insert an import statement without the .js file extension.

import { baz } from "#foo/bar/baz";

Additional information about the issue

I've seen a similar issue #59200, but I cannot reproduce the issue, with the same setup. Therefore, I think my issue might be related with the subpath imports.

@Andarist
Copy link
Contributor

Andarist commented Nov 4, 2024

I don't think this is a bug. package.json#imports don't describe relative specifiers. Resolving #foo/bar/baz based on "#*": "./src/*" to ./src/foo/bar/baz.js would be incorrect.

A related comment to this by @andrewbranch :

#60003 (comment)

@alan910127
Copy link
Author

Oh, I see. But TypeScript language server does not error if I use the extensionless module specifier either. Why is that the case?

@Andarist
Copy link
Contributor

Andarist commented Nov 4, 2024

The thread I linked to specifically calls out extensionless module specifiers as erroring. If they are not then perhaps that is a bug - it might be good to wait for @andrewbranch to chime in. It would be good to understand the difference between that issue and yours.

@andrewbranch
Copy link
Member

@alan910127 your repo link above 404s—did you accidentally make it private? If you share the repo, I’ll investigate. @Andarist is right; the import you listed as your expected behavior should have an error.

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

4 participants