From 76578f9ee14ab719b1cb4f869d684bc2f4450a1c Mon Sep 17 00:00:00 2001 From: 0z5a Date: Fri, 20 Nov 2026 00:00:19 +0100 Subject: [PATCH] fix(@schematics/angular): skip NgModules without bootstrap when resolving the app component `resolveNgModuleBasedData` read `.elements` from an undefined `bootstrap` property when an `@NgModule` in the bootstrapped module's file had no `bootstrap` array (for example a root module using `ngDoBootstrap()`, or a shared module declared before the root module). This made the `server` and `ssr` schematics throw a TypeError instead of continuing with the next module or falling back to the default names. Closes #34761 --- .../schematics/angular/server/index_spec.ts | 72 +++++++++++++++++++ .../utility/standalone/app_component.ts | 6 +- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/packages/schematics/angular/server/index_spec.ts b/packages/schematics/angular/server/index_spec.ts index 42c086559e8f..61f66a68a789 100644 --- a/packages/schematics/angular/server/index_spec.ts +++ b/packages/schematics/angular/server/index_spec.ts @@ -109,6 +109,78 @@ describe('Server Schematic', () => { expect(contents).toContain(`bootstrap: [MyCustomApp],`); }); + it('should skip an NgModule without bootstrap that is declared before the root module', async () => { + appTree.create( + '/projects/bar/src/app/my-custom-module.ts', + ` + import { NgModule } from '@angular/core'; + import { BrowserModule } from '@angular/platform-browser'; + import { MyCustomApp } from './foo/bar/baz/app.foo'; + + @NgModule({ + exports: [], + }) + export class SharedModule {} + + @NgModule({ + declarations: [MyCustomApp], + imports: [BrowserModule, SharedModule], + bootstrap: [MyCustomApp] + }) + export class MyCustomModule {} + `, + ); + + appTree.overwrite( + '/projects/bar/src/main.ts', + ` + import { platformBrowser } from '@angular/platform-browser'; + import { MyCustomModule } from './app/my-custom-module'; + + platformBrowser().bootstrapModule(MyCustomModule) + .catch(err => console.error(err)); + `, + ); + + const tree = await schematicRunner.runSchematic('server', defaultOptions, appTree); + const contents = tree.readContent('/projects/bar/src/app/app.module.server.ts'); + + expect(contents).toContain(`import { MyCustomApp } from './foo/bar/baz/app.foo';`); + expect(contents).toContain(`imports: [MyCustomModule],`); + expect(contents).toContain(`bootstrap: [MyCustomApp],`); + }); + + it('should not fail when the root NgModule uses ngDoBootstrap', async () => { + appTree.create( + '/projects/bar/src/app/my-custom-module.ts', + ` + import { NgModule } from '@angular/core'; + import { BrowserModule } from '@angular/platform-browser'; + + @NgModule({ + imports: [BrowserModule], + }) + export class MyCustomModule { + ngDoBootstrap() {} + } + `, + ); + + appTree.overwrite( + '/projects/bar/src/main.ts', + ` + import { platformBrowser } from '@angular/platform-browser'; + import { MyCustomModule } from './app/my-custom-module'; + + platformBrowser().bootstrapModule(MyCustomModule) + .catch(err => console.error(err)); + `, + ); + + const tree = await schematicRunner.runSchematic('server', defaultOptions, appTree); + expect(tree.exists('/projects/bar/src/app/app.module.server.ts')).toBeTrue(); + }); + it('should account for renamed app component and module that have been aliased', async () => { appTree.create( '/projects/bar/src/app/my-custom-module.ts', diff --git a/packages/schematics/angular/utility/standalone/app_component.ts b/packages/schematics/angular/utility/standalone/app_component.ts index dff6c953f88b..2faae99b71a0 100644 --- a/packages/schematics/angular/utility/standalone/app_component.ts +++ b/packages/schematics/angular/utility/standalone/app_component.ts @@ -89,7 +89,11 @@ function resolveNgModuleBasedData( ); }); - const componentIdentifier = (bootstrapProp?.initializer as ts.ArrayLiteralExpression) + if (!bootstrapProp) { + continue; + } + + const componentIdentifier = (bootstrapProp.initializer as ts.ArrayLiteralExpression) .elements[0] as ts.Identifier | undefined; const componentResult = componentIdentifier ? resolveIdentifier(componentIdentifier) : null; const bootstrapCall = findBootstrapModuleCall(tree, mainFilePath);