Skip to content

fix(core): rebuild a corrupted embedded view's TView instead of reusing it after an interrupted first pass - #70406

Closed
arturovt wants to merge 1 commit into
angular:mainfrom
arturovt:experimental/embedded-tview-incomplete-pass-rebuild
Closed

arturovt wants to merge 1 commit into
angular:mainfrom
arturovt:experimental/embedded-tview-incomplete-pass-rebuild

Conversation

@arturovt

@arturovt arturovt commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

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:

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 TNodes after the interruption point were missing.

This surfaced in production as:

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:

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.

@angular-robot angular-robot Bot added the area: core Issues related to the framework runtime label Aug 26, 2026
@ngbot ngbot Bot added this to the Backlog milestone Aug 26, 2026
@arturovt
arturovt force-pushed the experimental/embedded-tview-incomplete-pass-rebuild branch 5 times, most recently from d833a97 to ba2eb1d Compare August 31, 2026 22:24
@arturovt arturovt changed the title fix(core): experimental rebuild of corrupted embedded-view TView (WIP, not ready to ship) fix(core): rebuild a corrupted embedded view's TView instead of reusing it after an interrupted first pass Aug 31, 2026
@arturovt
arturovt force-pushed the experimental/embedded-tview-incomplete-pass-rebuild branch 2 times, most recently from 2e40932 to 84b2ed2 Compare August 31, 2026 22:27
@arturovt
arturovt marked this pull request as ready for review September 1, 2026 09:38
@pullapprove
pullapprove Bot requested a review from atscott September 1, 2026 09:38
@arturovt

arturovt commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor Author

note: 1) presubmit ✔️ 2) global presubmit ✔️

@arturovt
arturovt force-pushed the experimental/embedded-tview-incomplete-pass-rebuild branch from 84b2ed2 to d36e1de Compare September 1, 2026 11:42

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@arturovt
arturovt force-pushed the experimental/embedded-tview-incomplete-pass-rebuild branch from d36e1de to eb63bbd Compare September 10, 2026 18:00
…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.
@JeanMeche

Copy link
Copy Markdown
Member

Closing and moving the discussion to #72210.

@JeanMeche JeanMeche closed this Nov 26, 2026
@arturovt
arturovt deleted the experimental/embedded-tview-incomplete-pass-rebuild branch November 26, 2026 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants