Skip to content

Commit ce1a476

Browse files
atscottAndrewKushnir
authored andcommitted
fix(language-service): Prevent language service from crashing on suggestion diagnostic errors
This prevents the language service from crashing when a `FatalDiagnosticError` is thrown when retrieving suggestion diagnostics. fixes angular#66069
1 parent 2e58b66 commit ce1a476

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/language-service/src/language_service.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import {AST, TmplAstNode} from '@angular/compiler';
1010
import {CompilerOptions, ConfigurationHost, readConfiguration} from '@angular/compiler-cli';
1111
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
12-
import {ErrorCode, ngErrorCode} from '@angular/compiler-cli/src/ngtsc/diagnostics';
12+
import {
13+
ErrorCode,
14+
isFatalDiagnosticError,
15+
ngErrorCode,
16+
} from '@angular/compiler-cli/src/ngtsc/diagnostics';
1317
import {absoluteFrom, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
1418
import {PerfPhase} from '@angular/compiler-cli/src/ngtsc/perf';
1519
import {FileUpdate, ProgramDriver} from '@angular/compiler-cli/src/ngtsc/program_driver';
@@ -141,11 +145,20 @@ export class LanguageService {
141145
const components = compiler.getComponentsWithTemplateFile(fileName);
142146
for (const component of components) {
143147
if (ts.isClassDeclaration(component)) {
144-
diagnostics.push(
145-
...compiler
146-
.getTemplateTypeChecker()
147-
.getSuggestionDiagnosticsForComponent(component, this.tsLS),
148-
);
148+
try {
149+
diagnostics.push(
150+
...compiler
151+
.getTemplateTypeChecker()
152+
.getSuggestionDiagnosticsForComponent(component, this.tsLS),
153+
);
154+
} catch (e) {
155+
// Type check code may throw fatal diagnostic errors if e.g. the type check
156+
// block cannot be generated. In this case, we consider that there are no available suggestion diagnostics.
157+
if (isFatalDiagnosticError(e)) {
158+
continue;
159+
}
160+
throw e;
161+
}
149162
}
150163
}
151164
}

0 commit comments

Comments
 (0)