From 0d955f67edc7c8a897c838dfc9935cdedc840dde Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 29 Oct 2024 16:42:25 +0000 Subject: [PATCH] refactor(migrations): gracefully handle metadata parsing errors in signal migration (#58413) In 1P, we saw that a type of a target wasn't resolvable, referenced in a `hostBindings#directive` field. This breaks the entire pipeline; so we should handle gracefully but report an error. Worst case scenario here is that we would miss some references to the given directive/component. This is acceptable and we can continue investigation why that given target was broken; especially since the file was part of the target inputs- but seemingly not in the `tsconfig`. PR Close #58413 --- .../src/input_detection/input_decorator.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/core/schematics/migrations/signal-migration/src/input_detection/input_decorator.ts b/packages/core/schematics/migrations/signal-migration/src/input_detection/input_decorator.ts index 17db7ac5d18..c33715fa4bf 100644 --- a/packages/core/schematics/migrations/signal-migration/src/input_detection/input_decorator.ts +++ b/packages/core/schematics/migrations/signal-migration/src/input_detection/input_decorator.ts @@ -14,6 +14,7 @@ import {FatalDiagnosticError} from '@angular/compiler-cli/src/ngtsc/diagnostics' import {Reference, ReferenceEmitter} from '@angular/compiler-cli/src/ngtsc/imports'; import { DecoratorInputTransform, + DirectiveMeta, DtsMetadataReader, InputMapping, } from '@angular/compiler-cli/src/ngtsc/metadata'; @@ -74,9 +75,20 @@ function extractDtsInput(node: ts.Node, metadataReader: DtsMetadataReader): Extr return null; } - const directiveMetadata = metadataReader.getDirectiveMetadata( - new Reference(node.parent as ClassDeclaration), - ); + let directiveMetadata: DirectiveMeta | null = null; + + // Getting directive metadata can throw errors when e.g. types referenced + // in the `.d.ts` aren't resolvable. This seems to be unexpected and shouldn't + // result in the entire migration to be failing. + try { + directiveMetadata = metadataReader.getDirectiveMetadata( + new Reference(node.parent as ClassDeclaration), + ); + } catch (e) { + console.error('Unexpected error. Gracefully ignoring.'); + console.error('Could not parse directive metadata:', e); + return null; + } const inputMapping = directiveMetadata?.inputs.getByClassPropertyName(node.name.text); // Signal inputs are never tracked and migrated.