microsoft / microsoft/TypeScript

[Auto-import] Quick Fix suggests invalid module specifiers that fail to resolve at runtime

オープン
#64,171 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 Search Terms

Auto-import suggest invalid

🕗 Version & Regression Information

Tried in: 5.9.3., 6.0.3 and 7.0.2

⏯ Playground Link

No response

💻 Code

The project uses "module": "nodenext" and "moduleResolution": "nodenext" with the following "imports" configuration in package.json:

"imports": {
  "#*": {
    "node": "./dist/*/index.js",
    "default": "./dist/*.js"
  }
}

Project structure:

src/
  utils/
    summarize/
      index.ts      // re‑exports summarize from summarize.ts
      summarize.ts  // export const summarize
  index.ts          // file where auto‑import is triggered

./vscode/settings.json:

  "js/ts.preferences.importModuleSpecifier": "non-relative",

Steps to reproduce:
In src/index.ts, start typing summarize.
Trigger the Quick Fix auto‑import suggestions.

🙁 Actual behavior

With the "node" condition active and "js/ts.preferences.importModuleSpecifier": "non-relative" set, the LSP (Quick Fix) offers two alias suggestions for the same symbol:

  • #utils/summarize - resolves correctly via "node" to ./dist/utils/summarize/index.js.
  • #utils/summarize/summarize - would resolve only via the "default" condition, which is not active at runtime.

Selecting the second suggestion causes a runtime error:
node:internal/modules/esm/resolve:272 throw new ERR_MODULE_NOT_FOUND...

🙂 Expected behavior

Node.js uses only the first matching condition based on the active conditions.
The second alias is therefore never considered at runtime.

The auto‑import should suggest only valid module specifiers that matches the active conditions.

For example, both suggestions would be valid if the node condition were removed.

Additional information about the issue

The behavior is not limited to nodenext; it also occurs with other resolution modes and with customConditions.

Related issue: #62439, #50762

Would not be a breaking change for LSP suggestions (Quick Fix / Auto Fix), as they would simply no longer offer specifiers that are unresolvable at runtime.

Update – additional case with paths in tsconfig.json
The issue also occurs when combining "imports" (in package.json) and "paths" (in tsconfig.json). For example:

// package.json
"imports": {
  "#*": "./dist/*/index.js"
}

// tsconfig.json
"paths": {
  "#*": ["./src/*"]
}

With this setup, the Quick Fix suggests:
import { summarize } from "#utils/summarize"; -> valid.
import { summarize } from "#utils/summarize/summarize.js"; -> ERR_MODULE_NOT_FOUND

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

package.json の imports、tsconfig.json の paths、および issue に記載された NodeNext またはカスタム conditions を使用して、src/index.ts の Quick Fix を再現します。TypeScript language-service の auto-import におけるモジュール指定子のロジックと、その解決結果を追跡します。完了の条件は、アクティブなランタイム条件では解決できない指定子が候補から除外され、imports と paths がカバレッジに含まれていることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
node.js, typescript
領域
compilers, tooling
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。