Native API: formatDiagnosticsWithColorAndContext drops related information without a file location

Aperta Adatta ai principianti
#65,087 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
node.js, typescript
Ambito
api

Direzione di ricerca

Il bug si trova in packages/typescript/src/api/diagnosticFormatter.ts, dove la chiamata flattenDiagnosticMessage(related, ...) si trova dentro un ramo if (related.fileName && related.startPosition). Esegui lo script di riproduzione dell'issue per confermare che il testo correlato manca. Il lavoro è completo quando il testo correlato appare per le diagnostiche senza posizione, mentre l'intestazione della posizione e il contesto del sorgente restano condizionati a un file. Aggiungi un test di regressione accanto ai test esistenti del formatter.

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

Descrizione

🔎 Search Terms

formatDiagnosticsWithColorAndContext, relatedInformation, fileless diagnostic, missing related diagnostic text, diagnosticFormatter

🕗 Version & Regression Information

Reproduced against main at 6161772c1370bdd4a82f808e6823bd755eb1870d using Node.js v24.14.0. The same conditional is still present on current main.

The TypeScript 6.0.3 implementation outputs related diagnostic text outside the if (file) branch; the native API implementation only outputs it inside the location branch.

⏯ Playground Link

Not applicable: this exercises the diagnostic formatting API.

💻 Code

Save as repro.mts at the repository root and run node repro.mts with Node.js 24. This calls the formatter directly with a diagnostic containing a related message without a file location.

import { formatDiagnosticsWithColorAndContext } from "./packages/typescript/src/api/diagnosticFormatter.ts";

const host = {
    getCurrentDirectory: () => "/project",
    getCanonicalFileName: (name: string) => name,
    getNewLine: () => "\n",
};

const output = formatDiagnosticsWithColorAndContext([{
    category: 1,
    code: 2322,
    pos: 0,
    end: 0,
    text: "Primary error.",
    relatedInformation: [{
        category: 3,
        code: 9999,
        pos: 0,
        end: 0,
        text: "This related explanation must remain visible.",
    }],
}], host);

console.log(JSON.stringify(output.replace(/\x1b\[[0-9;]*m/g, "")));
🙁 Actual behavior

The related explanation disappears:

"error TS2322: Primary error.\n"

Adding fileName: "/project/a.ts" and startPosition: { line: 0, character: 0 } to the related diagnostic makes its text appear.

🙂 Expected behavior

The related explanation should be included even when it has no file location. Only the location heading and source context depend on a location.

Additional information about the issue

In diagnosticFormatter.ts, the call to flattenDiagnosticMessage(related, ...) is inside if (related.fileName && related.startPosition). This also drops message chains on locationless related diagnostics.

The diagnostic response type permits omitted file names and positions. In the 6.0.3 formatter, related message text is emitted independently of the file check.

The reproduction uses constructed diagnostic objects to isolate the formatter behavior. Existing formatter tests cover related information with a location.

I would be happy to submit a focused fix and regression tests once this is triaged.

Lingua principale
Go
Stelle
112k
Fork
15.4k
Merge medio
3g 6h
PR unite (30g)
136

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

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.

Altre issue di microsoft/TypeScript

Tutte le issue di microsoft/TypeScript

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.