Skip to content

fix(@schematics/angular): skip NgModules without bootstrap when resolving the app component - #34762

Open
0z5a wants to merge 1 commit into
angular:mainfrom
0z5a:schematics-ngmodule-no-bootstrap
Open

0z5a wants to merge 1 commit into
angular:mainfrom
0z5a:schematics-ngmodule-no-bootstrap

Conversation

@0z5a

@0z5a 0z5a commented Nov 19, 2026 •

Copy link
Copy Markdown

How I found it

A lint pass (oxlint no-unsafe-optional-chaining) flagged the server schematic's module lookup in utility/standalone/app_component.ts, and I read the code to check it. The optional chain in (bootstrapProp?.initializer as ts.ArrayLiteralExpression).elements[0] stops at initializer, so .elements is read from undefined whenever find returns nothing. I then reproduced it with SchematicTestRunner against @schematics/angular 22.3.0 (details in #34761).

Where this can show up in practice

  • An Angular Elements app, or an AngularJS hybrid app, whose root module uses ngDoBootstrap() and has no bootstrap array: ng add @angular/ssr stops with TypeError: Cannot read properties of undefined (reading 'elements').
  • A module file that declares a small shared or routing @NgModule above the root module: the first decorator has no bootstrap, so the lookup throws before it reaches AppModule.
  • A root module with bootstrap: [] left over from a refactor.

This is my estimate; I have not seen a field report.

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

resolveNgModuleBasedData throws when an @NgModule decorator in the bootstrapped module's file has no usable bootstrap array, so the server and ssr schematics fail instead of using the next module or the existing App / AppModule fallback in server/index.ts.

Issue Number: Closes #34761

What is the new behavior?

Decorators without a bootstrap array are skipped with continue, like the ts.isObjectLiteralExpression check just above. If no module has one, the function returns null and the caller's fallback applies.

Two tests in server/index_spec.ts (non standalone application):

  • a shared @NgModule without bootstrap declared before the root module: the root module's component is still used;
  • a root module that uses ngDoBootstrap(): the schematic completes and writes app.module.server.ts.

Testing

  • I could not run bazel test //packages/schematics/angular:test on the machine I used (linux-arm64; rules_browsers has no chromium for that platform, so analysis fails).
  • Instead I transpiled the changed server/index_spec.ts and app_component.ts with the repo's TypeScript and ran the spec with jasmine against the published @schematics/angular 22.3.0:
    • without the fix, the two new tests fail with the TypeError above and the other 21 pass;
    • with the fix, all 23 pass. The unchanged ssr/index_spec.ts (10 specs) also passes with the fix.
  • prettier --check passes on both files.
  • CI here is the real check for the bazel target.

Does this PR introduce a breaking change?

  • Yes
  • No

…ving 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 angular#34761

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a safety check in the Angular server schematic utility to prevent failures when a root NgModule does not define a bootstrap property (for example, when using ngDoBootstrap). It also adds two new unit tests to index_spec.ts to verify this behavior and ensure that NgModules without bootstrap declared before the root module are correctly skipped. There are no review comments, so I have no feedback to provide.

@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: merge The PR is ready for merge by the caretaker labels Nov 20, 2026
@alan-agius4

Copy link
Copy Markdown
Collaborator

Thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @schematics/angular target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ng add @angular/ssr fails with TypeError when an NgModule in the app module file has no bootstrap array

2 participants