fix(compiler-cli): avoid fatal diagnostics for invalid module schemas (#61220)

In the event of an invalid `schemas` field for an Angular module, an
empty schema array will now be used instead of a fatal error occurring.
A build will still fail in this case with the error reported as a
diagnostic. However, for the language service, this allows the module
to exist in the compiler registry and prevents cascading diagnostics
within an IDE due to "missing" modules/components. The originating
schema related errors will still be reported in the IDE.

PR Close #61220
This commit is contained in:
Charles Lyding 2025-05-08 13:57:46 -04:00 committed by Alex Rickabaugh
parent ba38e1c301
commit f03ff5acf9

View file

@ -466,10 +466,26 @@ export class NgModuleDecoratorHandler
}
}
const schemas =
this.compilationMode !== CompilationMode.LOCAL && ngModule.has('schemas')
? extractSchemas(ngModule.get('schemas')!, this.evaluator, 'NgModule')
: [];
let schemas: SchemaMetadata[] | undefined;
try {
schemas =
this.compilationMode !== CompilationMode.LOCAL && ngModule.has('schemas')
? extractSchemas(ngModule.get('schemas')!, this.evaluator, 'NgModule')
: [];
} catch (e) {
if (e instanceof FatalDiagnosticError) {
diagnostics.push(e.toDiagnostic());
// Use an empty schema array if schema extract fails.
// A build will still fail in this case. However, for the language service,
// this allows the module to exist in the compiler registry and prevents
// cascading diagnostics within an IDE due to "missing" components. The
// originating schema related errors will still be reported in the IDE.
schemas = [];
} else {
throw e;
}
}
let id: Expression | null = null;
if (ngModule.has('id')) {