ng add @angular/ssr fails with TypeError when an NgModule in the app module file has no bootstrap array
維護者通常 1 天內回覆
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 90/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- angular, typescript
- 領域
- build-system, tooling
研究方向
從 packages/schematics/angular/utility/standalone/app_component.ts 中的 resolveNgModuleBasedData 開始,檢查它如何處理沒有 bootstrap 欄位的 @NgModule 裝飾器。閱讀附近現有的檢查,接著查看 server/index_spec.ts 中 server schematic 的測試涵蓋情況。當沒有 bootstrap 的裝飾器被略過、回報的兩種情況如描述般運作,且針對性測試通過時,即算完成。
由索引模型根據 Issue 內容生成。
描述
Command
add
Is this a regression?
- Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
I think before the server schematic started inferring the app component name (commit ea1143dd, April 2025). I have not run an older release to confirm.
Description
ng add @angular/ssr (and ng generate server) crashes on an NgModule-based app when one of the @NgModule decorators in the file that main.ts bootstraps has no bootstrap array. The cases I hit:
- The root module uses
ngDoBootstrap()instead ofbootstrap: [...](Angular Elements apps, AngularJS hybrid apps that bootstrap throughUpgradeModule). - The file declares another
@NgModulewithoutbootstrap(for example a small shared module) before the root module.
In packages/schematics/angular/utility/standalone/app_component.ts, resolveNgModuleBasedData loops over the module's @NgModule decorators and does:
const bootstrapProp = getMetadataField(node, 'bootstrap').find(...);
const componentIdentifier = (bootstrapProp?.initializer as ts.ArrayLiteralExpression)
.elements[0] as ts.Identifier | undefined;
When bootstrapProp is undefined, the optional chain stops at initializer and .elements is read from undefined. The function is meant to return null in that case: the caller in server/index.ts already falls back to App / AppModule when nothing is found. So the schematic stops with a TypeError instead of using the fallback (case 1) or the right module (case 2).
Minimal Reproduction
ng new demo --no-standalone- In
src/app/app-module.ts, removebootstrap: [App]and addngDoBootstrap() {}toAppModule. ng add @angular/ssr
Or, for case 2, keep bootstrap: [App] and put this above AppModule in the same file:
@NgModule({ exports: [] })
export class SharedModule {}
I reproduced this with SchematicTestRunner against @schematics/angular 22.3.0 (workspace -> application --standalone=false -> edit the module -> server and ssr schematics). Both schematics throw for case 1, and server throws for case 2. The unmodified app works.
Exception or Error
TypeError: Cannot read properties of undefined (reading 'elements')
at resolveNgModuleBasedData (node_modules/@schematics/angular/utility/standalone/app_component.js:65:14)
Your Environment
@schematics/angular 22.3.0
@angular-devkit/schematics 22.3.0
Node.js 22.23.5
Anything else relevant?
Skipping decorators that have no bootstrap field (continue, like the ts.isObjectLiteralExpression check just above it) is enough to fix both cases: case 2 then finds AppModule, and case 1 falls through to the existing fallback. I have a patch with two tests in server/index_spec.ts and can open a PR.
- 主要語言
- TypeScript
- 星號
- 27k
- 分支
- 11.8k
- 平均合併
- 1 天 2 小時
- 30 天內合併 PR
- 225
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
angular/angular-cli 的其他 Issue
-
`@angular/[email protected]` depends on vulnerable Vite 7.3.7 - GHSA-p4f5-76q4-ffq7 (Critical RCE)未關閉area: @angular/build
難度 2/5 1-3 小時 新手友好度 65/100
angular/angular-cli#34764 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 72/100
angular/angular-cli#34760 ·
維護者通常 1 天內回覆
-
Angular SSR can crash `AngularAppEngine` servers with a single aborted request (22.2.6 regression)可能已有人在做 @SkyZeroZx 於 1 天前認領。 未關閉area: @angular/ssr
難度 4/5 3-5 天 新手友好度 20/100
angular/angular-cli#34754 ·
維護者通常 1 天內回覆
-
area: @schematics/angular
難度 3/5 1-2 天 新手友好度 74/100
angular/angular-cli#34730 ·
維護者通常 1 天內回覆
-
area: @angular/build
難度 4/5 3-5 天 新手友好度 48/100
angular/angular-cli#34728 · 2 則留言 ·
維護者通常 1 天內回覆
查看 angular/angular-cli 的全部 Issue
相似的 Issue
-
status/need-triage
難度 2/5 1-3 小時 新手友好度 76/100
google-gemini/gemini-cli#30384 · 1 則留言 ·
維護者通常 1 天內回覆
-
P2
難度 1/5 1 小時以內 新手友好度 84/100
維護者通常 1 天內回覆
-
bug ready-for-human
難度 2/5 1-3 小時 新手友好度 60/100
LucasSantana-Dev/Lucky#3150 ·
維護者通常 1 天內回覆
-
enhancement good first issue priority: low size: XS
難度 2/5 1-3 小時 新手友好度 88/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
vercel-labs/scriptc#3979 ·
維護者通常 1 天內回覆