diff --git a/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/class_data.rs b/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/class_data.rs index e8250e32038..a9a486950ba 100644 --- a/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/class_data.rs +++ b/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/class_data.rs @@ -1529,6 +1529,7 @@ pub struct NgModuleData { pub service: Option, pub decorator_name: Option, pub is_jit: bool, + pub analysis_failed: bool, } #[derive(Clone, Debug)] @@ -2463,6 +2464,9 @@ impl ClassData { push_issue(diagnostics, file_path, converter, issue); } diagnostics.extend(m.scope_diagnostics.iter().cloned()); + if m.analysis_failed { + return; + } let Some(span) = m.module_id_span else { return; }; @@ -2512,8 +2516,12 @@ impl ClassData { diagnostics: &mut Vec, ) { let validates = match &self.decorator { - DecoratorData::Component(_) | DecoratorData::Pipe(_) | DecoratorData::NgModule(_) => { - true + DecoratorData::Component(_) | DecoratorData::Pipe(_) => true, + DecoratorData::NgModule(m) => { + !m.analysis_failed + || m.injectable.as_ref().is_some_and(|i| { + cx.strict_injection_parameters && !self.is_abstract && i.requires_valid_ctor() + }) } DecoratorData::Directive(d) => d.selector.is_some(), DecoratorData::Injectable(i) => { @@ -4173,7 +4181,7 @@ impl ClassData { declaring_ng_module, }); } - DecoratorData::NgModule(m) => { + DecoratorData::NgModule(m) if !m.analysis_failed => { let any_synthetic = |slot: &Option>>| { slot.as_ref() .is_some_and(|s| s.raw().any_reference(&mut |r| r.synthetic)) @@ -4255,8 +4263,9 @@ impl ClassData { is_jit: m.is_jit, }); } - // Already projected via injectable_data()/service_data() above. - DecoratorData::Injectable(_) | DecoratorData::Service(_) => {} + // Already projected via injectable_data()/service_data() above, or skipped after a + // fatal NgModule analysis error. + DecoratorData::Injectable(_) | DecoratorData::Service(_) | DecoratorData::NgModule(_) => {} } // Member-level API surface: from the directive part; empty defaults for diff --git a/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/dts.rs b/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/dts.rs index 4f399fb3f5a..b6b0494953d 100644 --- a/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/dts.rs +++ b/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/dts.rs @@ -569,6 +569,7 @@ fn extract_class_metadata( service: None, decorator_name: None, is_jit: false, + analysis_failed: false, }); let class_data = ClassData { diff --git a/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/ngmodule.rs b/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/ngmodule.rs index 0e86b50e54d..916f8eede73 100644 --- a/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/ngmodule.rs +++ b/packages/compiler-cli/preprocessor/ng-analyze/src/analyzer/ngmodule.rs @@ -205,6 +205,7 @@ fn parse_ng_module_args<'a>( service: None, decorator_name, is_jit: false, + analysis_failed: false, }) } diff --git a/packages/compiler-cli/preprocessor/ng-analyze/src/compiler/decorators/ng_module.rs b/packages/compiler-cli/preprocessor/ng-analyze/src/compiler/decorators/ng_module.rs index f7c8a614857..6c9a54ea1e2 100644 --- a/packages/compiler-cli/preprocessor/ng-analyze/src/compiler/decorators/ng_module.rs +++ b/packages/compiler-cli/preprocessor/ng-analyze/src/compiler/decorators/ng_module.rs @@ -26,6 +26,7 @@ pub async fn optimize_ng_module( if let Some(outcome) = registry.outcome(reference_id) { ng_module.scope_diagnostics = outcome.diagnostics.clone(); if outcome.failed { + ng_module.analysis_failed = true; ng_module.declarations = None; ng_module.declarations_span = None; ng_module.imports = None; diff --git a/packages/compiler-cli/preprocessor/ng-analyze/src/compiler/tests.rs b/packages/compiler-cli/preprocessor/ng-analyze/src/compiler/tests.rs index f9da3af5f57..f4e632e6f18 100644 --- a/packages/compiler-cli/preprocessor/ng-analyze/src/compiler/tests.rs +++ b/packages/compiler-cli/preprocessor/ng-analyze/src/compiler/tests.rs @@ -1722,13 +1722,13 @@ fn analyze_app_module_importing_non_class( } fn app_module_scope_failed(meta: &AnalysisResult) -> bool { - let ng_module = meta - .classes + // When NgModule analysis fails in full mode, NgModuleMetadata is suppressed from wire output + // (mirroring ngtsc's failed analysis handling, which omits ɵmod/ɵinj/setClassMetadata). + meta.classes .iter() .find(|c| c.class_name.as_deref() == Some("AppModule")) .and_then(|c| c.ng_module.as_ref()) - .expect("AppModule ng_module"); - ng_module.imports.is_none() + .is_none() } #[test] @@ -7944,20 +7944,21 @@ fn test_failed_ngmodule_analysis_suppresses_wire_metadata_and_preserves_diagnost app_meta.diagnostics ); - // Pre-fix baseline: BrokenModule and BrokenInjectableModule still emit wire metadata. + // Both BrokenModule and BrokenInjectableModule are filtered out of wire classes in full mode + // when NgModule analysis fails, avoiding cascading TS6133/TS2339 errors on `.ng.ts`. assert!( app_meta .classes .iter() - .any(|c| c.class_name.as_deref() == Some("BrokenModule")), - "pre-fix expected BrokenModule in wire classes" + .all(|c| c.class_name.as_deref() != Some("BrokenModule")), + "expected BrokenModule to be filtered out when analysis_failed is true" ); assert!( app_meta .classes .iter() - .any(|c| c.class_name.as_deref() == Some("BrokenInjectableModule")), - "pre-fix expected BrokenInjectableModule in wire classes" + .all(|c| c.class_name.as_deref() != Some("BrokenInjectableModule")), + "expected BrokenInjectableModule to be filtered out when analysis_failed is true" ); assert!( app_meta @@ -8047,10 +8048,9 @@ fn test_failed_ngmodule_strict_injection_parameters_diagnostics() { "expected NG6001 on BrokenModule, got {:?}", pure_meta.diagnostics ); - // Pre-fix baseline: BrokenModule also validates constructor dependencies despite failed analysis. assert!( - pure_meta.diagnostics.iter().any(|d| d.code == 2003), - "pre-fix expected NG2003 on BrokenModule, got {:?}", + pure_meta.diagnostics.iter().all(|d| d.code != 2003), + "expected BrokenModule NOT to emit NG2003 when NgModule analysis fails, got {:?}", pure_meta.diagnostics ); } diff --git a/packages/compiler-cli/preprocessor/ng-analyze/src/types/analysis.rs b/packages/compiler-cli/preprocessor/ng-analyze/src/types/analysis.rs index 90d6f1e37e2..8b2b82b5383 100644 --- a/packages/compiler-cli/preprocessor/ng-analyze/src/types/analysis.rs +++ b/packages/compiler-cli/preprocessor/ng-analyze/src/types/analysis.rs @@ -643,6 +643,21 @@ impl FileData { !((cx.emit_declaration_only || cx.experimental_local_compilation) && !c.non_ng_decorator_spans.is_empty()) }) + // TODO(parity): In ngtsc, a co-located `@Injectable()` on a class whose `@NgModule` + // fails analysis with a `FatalDiagnosticError` still compiles `ɵprov`/`ɵfac` independently + // because decorator handlers run per-decorator. Because `ngp` emits `.ng.ts` TypeScript + // source rather than post-typecheck JS, stripping `@NgModule(...)` or emitting `ɵprov` + // without `ɵfac` on a fatally errored NgModule class triggers cascading TS6133/TS2339 + // errors; we therefore suppress wire codegen for the entire class in full mode while + // preserving both `NG6xxx` and `@Injectable` (`NG2003`) diagnostics. + .filter(|c| { + !(!cx.emit_declaration_only + && !cx.experimental_local_compilation + && matches!( + &c.decorator, + crate::analyzer::DecoratorData::NgModule(m) if m.analysis_failed + )) + }) .map(|c| c.to_wire(cx)) .collect::, _>>()?; diff --git a/packages/compiler-cli/preprocessor/test/failed_ngmodule_suppression_spec.ts b/packages/compiler-cli/preprocessor/test/failed_ngmodule_suppression_spec.ts index 5e5487f023d..e186e3e28b8 100644 --- a/packages/compiler-cli/preprocessor/test/failed_ngmodule_suppression_spec.ts +++ b/packages/compiler-cli/preprocessor/test/failed_ngmodule_suppression_spec.ts @@ -77,18 +77,54 @@ describe('failed NgModule analysis wire suppression', () => { const outMap = new Map(outputs.map((o) => [o.path, o.content])); const diagJson = JSON.parse(outMap.get('/tsconfig.ngdiag.json')!) as { - diagnostics: Array<{code: number; messageText: string; start?: number; length?: number}>; + diagnostics: Array<{ + code: number; + messageText: string | {messageText: string}; + start?: number; + length?: number; + }>; }; const ng6001 = diagJson.diagnostics.filter((d) => d.code === 6001); const ng2003 = diagJson.diagnostics.filter((d) => d.code === 2003); expect(ng6001.length).toBe(2); - // Pre-fix baseline: both BrokenModule and BrokenInjectableModule emit NG2003 and static ɵmod. - expect(ng2003.length).toBe(2); + expect(ng2003.length).toBe(1); + expect(JSON.stringify(ng2003[0].messageText)).toContain('BrokenInjectableModule'); + expect(JSON.stringify(ng2003[0].messageText)).not.toContain('BrokenModule.'); const appOut = outMap.get('/out/app.ts')!; expect(appOut).toBeDefined(); - expect(appOut).toContain('ValidComponent'); - expect(appOut).toContain('ɵcmp'); + + // Neither BrokenModule nor BrokenInjectableModule may emit ɵmod, ɵinj, ɵprov, ɵfac, or + // ɵsetClassMetadata. + expect(appOut).not.toContain('ɵmod'); + expect(appOut).not.toContain('ɵinj'); + expect(appOut).not.toContain('ɵprov'); + expect(appOut).toContain('export class BrokenModule {\n constructor(dep: unknown) {}\n}'); + expect(appOut).toContain( + 'export class BrokenInjectableModule {\n constructor(dep: unknown) {}\n}', + ); + expect(appOut).not.toContain('BrokenModule_Factory'); + expect(appOut).not.toContain('BrokenInjectableModule_Factory'); + expect(appOut).not.toContain('ɵsetClassMetadata(BrokenModule'); + expect(appOut).not.toContain('ɵsetClassMetadata(BrokenInjectableModule'); + + // Both @Injectable() and @NgModule({ declarations: [ExternalComponent] }) remain intact on + // BrokenInjectableModule (and @NgModule on BrokenModule), keeping ExternalComponent used. + expect(appOut).toMatch( + /@Injectable\(\)\s*@NgModule\(\{\s*declarations:\s*\[ExternalComponent\],?\s*\}\)\s*export class BrokenInjectableModule/, + ); + expect(appOut).toMatch( + /@NgModule\(\{\s*declarations:\s*\[ExternalComponent\],?\s*\}\)\s*export class BrokenModule/, + ); + + // Sibling ValidComponent still compiles ɵcmp and ɵfac. + expect(appOut).toMatch(/class ValidComponent\s*\{[\s\S]*?static\s+ɵfac\b/); + expect(appOut).toMatch(/class ValidComponent\s*\{[\s\S]*?static\s+ɵcmp\b/); + + // Downstream tsc with `noUnusedLocals: true` and `strict: true` must report zero TS6133/TS2339 + // errors on the emitted `.ng.ts` output. + const tsDiags = typecheckWithExtDts(outMap, extDts, ['/out/app.ts']); + expect(tsDiags).toEqual([]); }); });