microsoft / microsoft/TypeScript

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

未关闭
#64,171 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用 issue 中描述的 package.json imports、tsconfig.json paths 以及 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 摘要。