microsoft / microsoft/TypeScript

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

Aperta
#64,171 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 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`

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il Quick Fix da src/index.ts utilizzando gli import di package.json, i percorsi di tsconfig.json e le condizioni NodeNext o personalizzate descritte nell’issue. Traccia la logica di TypeScript language-service per gli specificatori di modulo degli auto-import e i relativi risultati di risoluzione; il lavoro è completato quando i suggerimenti escludono gli specificatori che non possono essere risolti nelle condizioni di runtime attive, con copertura per import e percorsi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
node.js, typescript
Ambito
compilers, tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.