TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

auto-import with `package.json#imports` does not take `moduleResolution` into account

Open alan910127 opened this issue 1 year ago • 5 comments

🔎 Search Terms

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

🕗 Version & Regression Information

  • This changed in commit or PR #55015
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about imports, completions

⏯ 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.

alan910127 avatar Nov 04 '24 07:11 alan910127

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 :

https://github.com/microsoft/TypeScript/issues/60003#issuecomment-2364130579

Andarist avatar Nov 04 '24 08:11 Andarist

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

alan910127 avatar Nov 04 '24 09:11 alan910127

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.

Andarist avatar Nov 04 '24 10:11 Andarist

@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.

andrewbranch avatar Nov 04 '24 17:11 andrewbranch

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.

Hi, I'm trying to implement a just in time ui lib in a monorepo. What do u think is the best practice now to implement absolute path resolve inside this pacakge? I'm currently using both package.json subpath import along with compilerOptions.path to surpress the warning. It works fine but I just really wanna keep the logic in one place :)

shawnyang0210 avatar Mar 18 '25 05:03 shawnyang0210