Title: Native API: formatDiagnosticsWithColorAndContext drops related information without a file location · Issue #65087 · microsoft/TypeScript · GitHub
Open Graph Title: Native API: formatDiagnosticsWithColorAndContext drops related information without a file location · Issue #65087 · microsoft/TypeScript
X Title: Native API: formatDiagnosticsWithColorAndContext drops related information without a file location · Issue #65087 · microsoft/TypeScript
Description: 🔎 Search Terms formatDiagnosticsWithColorAndContext, relatedInformation, fileless diagnostic, missing related diagnostic text, diagnosticFormatter 🕗 Version & Regression Information Reproduced against main at 6161772 using Node.js v24.14...
Open Graph Description: 🔎 Search Terms formatDiagnosticsWithColorAndContext, relatedInformation, fileless diagnostic, missing related diagnostic text, diagnosticFormatter 🕗 Version & Regression Information Reproduced agai...
X Description: 🔎 Search Terms formatDiagnosticsWithColorAndContext, relatedInformation, fileless diagnostic, missing related diagnostic text, diagnosticFormatter 🕗 Version & Regression Information Reproduced ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/65087
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Native API: formatDiagnosticsWithColorAndContext drops related information without a file location","articleBody":"### 🔎 Search Terms\n\nformatDiagnosticsWithColorAndContext, relatedInformation, fileless diagnostic, missing related diagnostic text, diagnosticFormatter\n\n### 🕗 Version & Regression Information\n\nReproduced against `main` at 6161772c1370bdd4a82f808e6823bd755eb1870d using Node.js v24.14.0. The same conditional is still present on current `main`.\n\nThe 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.\n\n### ⏯ Playground Link\n\nNot applicable: this exercises the diagnostic formatting API.\n\n### 💻 Code\n\nSave 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.\n\n```ts\nimport { formatDiagnosticsWithColorAndContext } from \"./packages/typescript/src/api/diagnosticFormatter.ts\";\n\nconst host = {\n getCurrentDirectory: () => \"/project\",\n getCanonicalFileName: (name: string) => name,\n getNewLine: () => \"\\n\",\n};\n\nconst output = formatDiagnosticsWithColorAndContext([{\n category: 1,\n code: 2322,\n pos: 0,\n end: 0,\n text: \"Primary error.\",\n relatedInformation: [{\n category: 3,\n code: 9999,\n pos: 0,\n end: 0,\n text: \"This related explanation must remain visible.\",\n }],\n}], host);\n\nconsole.log(JSON.stringify(output.replace(/\\x1b\\[[0-9;]*m/g, \"\")));\n```\n\n### 🙁 Actual behavior\n\nThe related explanation disappears:\n\n```text\n\"error TS2322: Primary error.\\n\"\n```\n\nAdding `fileName: \"/project/a.ts\"` and `startPosition: { line: 0, character: 0 }` to the related diagnostic makes its text appear.\n\n### 🙂 Expected behavior\n\nThe related explanation should be included even when it has no file location. Only the location heading and source context depend on a location.\n\n### Additional information about the issue\n\nIn [diagnosticFormatter.ts](https://github.com/microsoft/TypeScript/blob/6161772c1370bdd4a82f808e6823bd755eb1870d/packages/typescript/src/api/diagnosticFormatter.ts#L191-L201), the call to `flattenDiagnosticMessage(related, ...)` is inside `if (related.fileName && related.startPosition)`. This also drops message chains on locationless related diagnostics.\n\nThe diagnostic response type permits omitted file names and positions. In the [6.0.3 formatter](https://github.com/microsoft/TypeScript/blob/v6.0.3/src/compiler/program.ts#L786-L795), related message text is emitted independently of the file check.\n\nThe reproduction uses constructed diagnostic objects to isolate the formatter behavior. Existing formatter tests cover related information with a location.\n\nI would be happy to submit a focused fix and regression tests once this is triaged.\n","author":{"url":"https://github.com/emerardd","@type":"Person","name":"emerardd"},"datePublished":"2026-11-01T10:41:47Z","url":"https://github.com/microsoft/TypeScript/issues/65087"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:564397ae-ba08-c248-0f84-a928bbe07682 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9770:CC1A4:16CEE44:1CFF5BB:6B013693 |
| html-safe-nonce | 2f3de5c467a6f19efe2348f0e7fb8bdce86592f00becb58737e6b96a50915a92 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzcwOkNDMUE0OjE2Q0VFNDQ6MUNGRjVCQjo2QjAxMzY5MyIsInZpc2l0b3JfaWQiOiI2ODY3NjMxMjMzMjYyNzYxNjE5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9d93fb89e74a19a9114a3a406bc0bd7b0dcd9725fbf65b5ba564ce59357d9de4 |
| hovercard-subject-tag | issue:6145950652 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/65087/issue_layout |
| twitter:image | https://opengraph.githubassets.com/72860da278735c63d7f183d8da79a7b861f26937c7697940f7aa1a5615e8b87d/microsoft/TypeScript/issues/65087 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/72860da278735c63d7f183d8da79a7b861f26937c7697940f7aa1a5615e8b87d/microsoft/TypeScript/issues/65087 |
| og:image:alt | 🔎 Search Terms formatDiagnosticsWithColorAndContext, relatedInformation, fileless diagnostic, missing related diagnostic text, diagnosticFormatter 🕗 Version & Regression Information Reproduced agai... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | emerardd |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width