microsoft / microsoft/TypeScript

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

Open
#64,171 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 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:
```json
"imports": {
"#*": {
"node": "./dist/*/index.js",
"default": "./dist/*.js"
}
}
```

Project structure:
```text
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`:
```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:

```json
// 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`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Quick Fix from src/index.ts using the package.json imports, tsconfig.json paths, and NodeNext or custom conditions described in the issue. Trace the TypeScript language-service auto-import module-specifier logic and its resolution results; done means suggestions exclude specifiers that cannot resolve under the active runtime conditions, with coverage for imports and paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.