Repository navigation
fix(core): rebuild a corrupted embedded view's TView instead of reusing it after an interrupted first pass - #70406
Conversation
d833a97 to
ba2eb1d
Compare
2e40932 to
84b2ed2
Compare
|
note: 1) presubmit ✔️ 2) global presubmit ✔️ |
84b2ed2 to
d36e1de
Compare
JeanMeche
left a comment
There was a problem hiding this comment.
AGENT: Thanks for working on this! Rebuilding the embedded view's TView when incompleteFirstPass is set is a great architectural direction, and removing the temporary NG0510 bandaid in getParentRElement makes complete sense.
However, there is a critical regression regarding queries when an embedded view's TView is rebuilt:
createTView initializes queries: null. Unlike components, embedded views inherit queries from the declaration view at template creation time. Rebuilding embeddedTView without restoring its inherited queries leaves embeddedTView.queries = null. If the parent component has queries (like @ViewChildren or viewChildren) that cross into this template, collectQueryResults in query.ts throws TypeError: Cannot read properties of null (reading 'getByIndex') during change detection.
I've left an inline comment on packages/core/src/render3/view_manipulation.ts detailing the issue and where queries need to be restored.
d36e1de to
eb63bbd
Compare
…ng it after an interrupted first pass
Angular caches a shared `TView` (a "blueprint") for each template it sees, including `@if`/`@switch` branches and `@for` item templates. The blueprint is built lazily the first time the branch or item is rendered.
If an error interrupts that first creation pass, `renderView()`'s `catch` block (`instructions/render.ts`) still marks the blueprint as finished:
```ts
if (tView.firstCreatePass) {
tView.incompleteFirstPass = true;
tView.firstCreatePass = false;
}
```
Component views already handle this. `getOrCreateComponentTView()` rebuilds a component's `TView` when it is marked incomplete. Embedded views did not have the same check. The next time the branch or item was rendered, its instructions reused the partially built `TView`, so any `TNode`s after the interruption point were missing.
This surfaced in production as:
```text
TypeError: Cannot read properties of null (reading 'parent')
```
in `getParentRElement()` (`node_manipulation.ts`). A previous fix on `main` added an `NG0510` error at that point instead of the raw crash, but the underlying corruption remained.
The fix is to rebuild the embedded view's `TView` when `incompleteFirstPass` is set. `createAndRenderEmbeddedLView()` (`view_manipulation.ts`) now does this, matching the existing component behavior.
Tests cover the main cases:
* `@if`/`@switch` (`control_flow_if_spec.ts`): a directive that throws once reproduces an interrupted first pass without hydration. Re-entering the branch renders correctly.
* `@for` (`control_flow_for_spec.ts`): all items share one `TView`, so an interrupted pass can affect other items created from the same template.
* Other embedded-view creation paths also go through `createAndRenderEmbeddedLView()`, including `@defer`, `TemplateRef.createEmbeddedView()`, foreign components, and `<ng-content>` fallback content.
With the rebuild in place, `tNode` can no longer be null at `getParentRElement()`, so the `NG0510` guard and `PARENT_NODE_NOT_FOUND` error code are no longer needed.
Rebuilding the `TView` also needs to restore inherited queries. `templateCreate()` sets these up only during the declaration view's first creation pass:
```ts
if (declarationTView.queries !== null) {
declarationTView.queries.template(declarationTView, tNode);
embeddedTView.queries = declarationTView.queries.embeddedTView(tNode);
}
```
A new `TView` starts with `queries: null`, and `templateCreate()` will not run again for this node. `createAndRenderEmbeddedLView()` now clones the inherited queries from the stale `TView` using `TQueries.cloneForRebuild()`. The clones start with no match state, so the fresh creation pass collects the queries again without modifying the declaration view's query objects.
`control_flow_if_spec.ts` covers this with a `@ViewChildren` query reaching into an `@if` branch whose first pass was interrupted.
There is one known hydration gap. If a hydration mismatch interrupts the first pass, DOM nodes already claimed during the failed attempt are not cleaned up. The branch no longer crashes after the rebuild, but can contain duplicated content.
This is being treated as a known, low-priority limitation. Per discussion with Matthieu Riegler, there is no appetite for general hydration-failure recovery. A hydration test covering this case was verified during the investigation but is not kept in the suite; the scenario can be recovered from the commit history if hydration cleanup is addressed later.
eb63bbd to
65c6c3e
Compare
|
Closing and moving the discussion to #72210. |
Angular caches a shared
TView(a "blueprint") for each template it sees, including@if/@switchbranches and@foritem templates. The blueprint is built lazily the first time the branch or item is rendered.If an error interrupts that first creation pass,
renderView()'scatchblock (instructions/render.ts) still marks the blueprint as finished:Component views already handle this.
getOrCreateComponentTView()rebuilds a component'sTViewwhen it is marked incomplete. Embedded views did not have the same check. The next time the branch or item was rendered, its instructions reused the partially builtTView, so anyTNodes after the interruption point were missing.This surfaced in production as:
in
getParentRElement()(node_manipulation.ts). A previous fix onmainadded anNG0510error at that point instead of the raw crash, but the underlying corruption remained.The fix is to rebuild the embedded view's
TViewwhenincompleteFirstPassis set.createAndRenderEmbeddedLView()(view_manipulation.ts) now does this, matching the existing component behavior.Tests cover the main cases:
@if/@switch(control_flow_if_spec.ts): a directive that throws once reproduces an interrupted first pass without hydration. Re-entering the branch renders correctly.@for(control_flow_for_spec.ts): all items share oneTView, so an interrupted pass can affect other items created from the same template.createAndRenderEmbeddedLView(), including@defer,TemplateRef.createEmbeddedView(), foreign components, and<ng-content>fallback content.With the rebuild in place,
tNodecan no longer be null atgetParentRElement(), so theNG0510guard andPARENT_NODE_NOT_FOUNDerror code are no longer needed.Rebuilding the
TViewalso needs to restore inherited queries.templateCreate()sets these up only during the declaration view's first creation pass:A new
TViewstarts withqueries: null, andtemplateCreate()will not run again for this node.createAndRenderEmbeddedLView()now clones the inherited queries from the staleTViewusingTQueries.cloneForRebuild(). The clones start with no match state, so the fresh creation pass collects the queries again without modifying the declaration view's query objects.control_flow_if_spec.tscovers this with a@ViewChildrenquery reaching into an@ifbranch whose first pass was interrupted.There is one known hydration gap. If a hydration mismatch interrupts the first pass, DOM nodes already claimed during the failed attempt are not cleaned up. The branch no longer crashes after the rebuild, but can contain duplicated content.
This is being treated as a known, low-priority limitation. Per discussion with Matthieu Riegler, there is no appetite for general hydration-failure recovery. A hydration test covering this case was verified during the investigation but is not kept in the suite; the scenario can be recovered from the commit history if hydration cleanup is addressed later.