From 17db18aa87c33fa2817ed83f8c01afe9b57b5a50 Mon Sep 17 00:00:00 2001
From: Alan Agius <17563226+alan-agius4@users.noreply.github.com>
Date: Tue, 20 Oct 2026 08:57:18 +0000
Subject: [PATCH] fix(@angular/build): preserve diagnostic locations at offset
0 and guard empty files
Ensure `convertTypeScriptDiagnosticInfo` checks `info.start !== undefined` so diagnostics and related information starting at file offset `0` retain their line, column, and lineText location properties. Also clamp `info.file.text.length - 1` to `0` when computing `lastLineOfFile` to prevent a TypeScript `Debug Failure` when reporting diagnostics on empty files.
---
.../tools/angular/compilation/diagnostics.ts | 4 +-
.../angular/compilation/diagnostics_spec.ts | 111 ++++++++++++++++++
2 files changed, 113 insertions(+), 2 deletions(-)
create mode 100644 packages/angular/build/src/tools/angular/compilation/diagnostics_spec.ts
diff --git a/packages/angular/build/src/tools/angular/compilation/diagnostics.ts b/packages/angular/build/src/tools/angular/compilation/diagnostics.ts
index bf85b69f707c..41563b90ebbd 100644
--- a/packages/angular/build/src/tools/angular/compilation/diagnostics.ts
+++ b/packages/angular/build/src/tools/angular/compilation/diagnostics.ts
@@ -37,7 +37,7 @@ function convertTypeScriptDiagnosticInfo(
};
// Calculate the line/column location and extract the full line text that has the diagnostic
- if (info.start) {
+ if (info.start !== undefined) {
const { line, character } = typescript.getLineAndCharacterOfPosition(info.file, info.start);
note.location.line = line + 1;
note.location.column = character;
@@ -50,7 +50,7 @@ function convertTypeScriptDiagnosticInfo(
// will error if a nonexistent line is passed).
const { line: lastLineOfFile } = typescript.getLineAndCharacterOfPosition(
info.file,
- info.file.text.length - 1,
+ Math.max(0, info.file.text.length - 1),
);
const lineEndPosition =
line < lastLineOfFile
diff --git a/packages/angular/build/src/tools/angular/compilation/diagnostics_spec.ts b/packages/angular/build/src/tools/angular/compilation/diagnostics_spec.ts
new file mode 100644
index 000000000000..8ea79c20ba43
--- /dev/null
+++ b/packages/angular/build/src/tools/angular/compilation/diagnostics_spec.ts
@@ -0,0 +1,111 @@
+/**
+ * @license
+ * Copyright Google LLC All Rights Reserved.
+ *
+ * Use of this source code is governed by an MIT-style license that can be
+ * found in the LICENSE file at https://angular.dev/license
+ */
+
+import ts from 'typescript';
+import { convertTypeScriptDiagnostic } from './diagnostics';
+
+describe('convertTypeScriptDiagnostic', () => {
+ it('populates line, column, and lineText when diagnostic or relatedInformation starts at offset 0', () => {
+ const sourceFile = ts.createSourceFile(
+ '/src/app/app.component.ts',
+ 'const invalid: number = "string";\nconst secondLine = 2;\n',
+ ts.ScriptTarget.Latest,
+ );
+ const templateFile = ts.createSourceFile(
+ '/src/app/app.component.html',
+ '