Skip to content

[XIHR] Patch from the applied generation after throwaway generator runs - #40378

Merged
kubaflo merged 1 commit into
candidate/11.0.1xx-sr2from
stephanedelcroix-xaml-hotreload-baseline
Nov 18, 2026
Merged

kubaflo merged 1 commit into
candidate/11.0.1xx-sr2from
stephanedelcroix-xaml-hotreload-baseline

Conversation

@StephaneDelcroix

@StephaneDelcroix StephaneDelcroix commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Target Branch

This PR is for Initial base
New features, API changes, or .NET 12 work (including .NET 12 fixes) main
.NET 11 servicing fixes net11, for review
Older servicing, release integration, infrastructure, or documentation The branch designated for that work

Important

Base: candidate/11.0.1xx-sr2 (decided). This PR builds on #40233, #40235, #40329 and #40330, which exist only on candidate/11.0.1xx-sr2 (not on net11, main or inflight/11.0.1xx-sr3). Against net11 the diff would include about 527 unrelated files. As with #40329 and #40330, it targets candidate/11.0.1xx-sr2 directly so it ships together with its prerequisites.

Description of Change

#40330 left one limitation. A throwaway generator run with different XAML, such as a design-time or speculative run whose output never reaches the app, advanced the cached previous XAML. The next real UpdateComponent() was then diffed from that intermediate generation instead of the generation the running app actually has. The result was missed edits: for example, an edit already present in the throwaway XAML was never applied.

The generator cannot tell which of its runs reached the app, so this PR has UpdateComponent() decide at runtime:

  • Generation key. Each successful generation gets a key: a SHA-256 of the normalized XAML plus the node IDs (NodeIdHelper.ComputeGenerationKey). XamlHotReloadState keeps the current generation and the previously generated ones as baselines, up to MaxBaselines = 4.
  • InitializeComponent records XamlComponentRegistry.Register(this, "__xihr_generation:<type>", key).
  • UpdateComponent reads the instance's recorded key:
    • A retained generation whose diff produces exactly the current node IDs gets its own branch: the patch diffed from that generation.
    • Anything else gets the default patch, diffed from the last generated XAML. This matches the previous behavior.
    • Every patched instance then records the new key, so running the patch again, or for a C#-only edit, changes nothing.
  • When there is nothing to patch, the output is unchanged: an empty UC, with no marker. Rude-edit rollback behavior is preserved, and the older applied generation now also gets its own branch.

Known edge, not a regression: a retained generation whose diff would assign different IDs from the current ones (e.g. some reverts) is not given a branch. Instances at that generation fall back to the default patch, which is the old behavior.

Issues Fixed

Fixes #36937 (remaining limitation from #40330)

Tests

  • XamlIncrementalHotReloadE2ETests.ThrowawayRunWithDifferentXaml_PatchStartsFromTheAppliedGeneration: real metadata update. Applied Text="Hello", then a throwaway run with Text="World" FontSize="30", then the real edit to Text="World". The live label ends at World with its default FontSize, re-running UC is a no-op, and new instances are already current. A new harness hook, GenerateWithThrowawayRuns, runs and discards a forked driver run.
  • XamlIncrementalHotReloadPipelineTests.ThrowawayRunWithDifferentXaml_PatchesFromEveryRetainedGeneration: checks the branch structure and determinism (same snapshot and C#-only edits give an identical UC).
  • XamlIncrementalHotReloadPipelineTests.RetainedGenerations_AreBounded
  • Existing rude-edit assertions now target the default patch (UpdateComponentText.DefaultPatch), because the UC legitimately contains a branch for the older applied generation.

Validation

  • DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj: passed, 899/899.
  • dotnet test src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj --filter "FullyQualifiedName~HotReload": passed, 12/12.
  • DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 dotnet test src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj: 2274 passed, 12 failed. All 12 failures are CultureNotFoundException from culture-specific tests under invariant-globalization mode; none involve hot reload.
  • dotnet format whitespace Microsoft.Maui.sln --verify-no-changes --include <changed files>: passed.

@azure-pipelines

This comment has been minimized.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026
@kubaflo

kubaflo commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

/rebase

…ted one

A throwaway generator run with different XAML advanced the cached previous
XAML, so the next real UpdateComponent was diffed from a generation that never
reached the app.

InitializeComponent now records each instance's generation key in
XamlComponentRegistry. UpdateComponent branches on that key: instances at a
retained generation (up to 4) get the patch diffed from that generation, others
get the default patch, and every patched instance records the new key, so
re-running the patch is a no-op.

Follow-up to #40330 for #36937.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions
github-actions Bot force-pushed the stephanedelcroix-xaml-hotreload-baseline branch from f5f3cba to 71ac90b Compare November 18, 2026 13:22
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@MauiBot

MauiBot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@StephaneDelcroix — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026
@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026
@MauiBot

MauiBot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@StephaneDelcroix — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026
@StephaneDelcroix
StephaneDelcroix marked this pull request as ready for review November 18, 2026 13:57
@StephaneDelcroix

Copy link
Copy Markdown
Contributor Author

@kubaflo FYI on the target branch: this PR intentionally targets candidate/11.0.1xx-sr2, not net11. It builds on #40233, #40235, #40329 and #40330, which exist only on that branch; against net11 the diff would pull in ~527 unrelated files. Same route as #40329/#40330. Please keep it on this base.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Expert Review — 1 finding

Reviewed commit 71ac90b.

See inline comments for details.

out var idsMatchCurrent);
if (i == 0)
defaultPatchBody = patchBody;
else if (idsMatchCurrent && (patchBody != null || emptyDiff) && baseline.Key is not null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[major] Logic and Correctness Verification - Node-ID compatibility does not ensure an older patch still compiles against the current C#. Generate A with a Button's Clicked="OnClicked"; generate/apply B removing that attribute while retaining the method; then generate C changing the Button's Text and deleting the now-unused OnClicked method. A's IDs still match, so this admits its A-to-C branch, where TryEmitEventChange emits Clicked -= OnClicked. The generated UC fails with CS0103 even though current XAML/IC and the default B-to-C patch no longer reference that method. Previously only B-to-C was emitted, so C compiled. Validate historical branches against the current compilation without letting obsolete handler references break the entire update, and cover this three-generation scenario.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026
@MauiBot

MauiBot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@StephaneDelcroix — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Expert Review — 2 findings

Reviewed commit 71ac90b.

See inline comments for details.

out var idsMatchCurrent);
if (i == 0)
defaultPatchBody = patchBody;
else if (idsMatchCurrent && (patchBody != null || emptyDiff) && baseline.Key is not null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[major] Logic and Correctness Verification - Node-ID compatibility does not ensure an older patch compiles against the current C#. Generate A with a Button's Clicked="OnClicked"; generate/apply B removing that attribute while retaining the method; then generate C changing the Button's Text and deleting the now-unused OnClicked method. A's IDs still match, so its A-to-C branch is admitted, and TryEmitEventChange emits Clicked -= OnClicked. The generated UC fails with CS0103 even though current XAML/IC and the default B-to-C patch no longer reference that method. Previously only B-to-C was emitted, so C compiled. Validate historical branches against the current compilation without letting obsolete handler references break the entire update, and cover this three-generation scenario.

var uc = FindUCSource(edited.RunGenerators(compilation).GetRunResult(), "uc.xsg");

Assert.NotNull(uc);
Assert.Equal(1, UpdateComponentText.BaselineCaseCount(uc!));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[moderate] Regression Prevention and Test Coverage - Both new baseline tests use property-only edits, so neither exercises rejection by the new idsMatchCurrent gate. Add a discriminating negative case: A contains a named Label, B removes it, and C restores exactly A's XAML. C's re-added Label has a different registry ID; assert distinct A/C generation keys and exclusion of the A-to-C branch. Accepting that semantically empty branch would mark an A instance current without reconciling its old ID, so a following Text edit would miss the Label. Existing sequential structural tests apply B first and do not verify exclusion of this incompatible older baseline.

@kubaflo
kubaflo merged commit 5b8f8f9 into candidate/11.0.1xx-sr2 Nov 18, 2026
36 of 80 checks passed
@kubaflo
kubaflo deleted the stephanedelcroix-xaml-hotreload-baseline branch November 18, 2026 19:29
kubaflo pushed a commit that referenced this pull request Nov 18, 2026
…ns (#40378)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

> [!IMPORTANT]
> **Base: `candidate/11.0.1xx-sr2` (decided).** This PR builds on
#40233, #40235, #40329 and #40330, which exist only on
`candidate/11.0.1xx-sr2` (not on `net11`, `main` or
`inflight/11.0.1xx-sr3`). Against `net11` the diff would include about
527 unrelated files. As with #40329 and #40330, it targets
`candidate/11.0.1xx-sr2` directly so it ships together with its
prerequisites.

### Description of Change

#40330 left one limitation. A throwaway generator run with different
XAML, such as a design-time or speculative run whose output never
reaches the app, advanced the cached previous XAML. The next real
`UpdateComponent()` was then diffed from that intermediate generation
instead of the generation the running app actually has. The result was
missed edits: for example, an edit already present in the throwaway XAML
was never applied.

The generator cannot tell which of its runs reached the app, so this PR
has `UpdateComponent()` decide at runtime:

- **Generation key.** Each successful generation gets a key: a SHA-256
of the normalized XAML plus the node IDs
(`NodeIdHelper.ComputeGenerationKey`). `XamlHotReloadState` keeps the
current generation and the previously generated ones as baselines, up to
`MaxBaselines = 4`.
- **InitializeComponent** records `XamlComponentRegistry.Register(this,
"__xihr_generation:<type>", key)`.
- **UpdateComponent** reads the instance's recorded key:
- A retained generation whose diff produces exactly the current node IDs
gets its own branch: the patch diffed from that generation.
- Anything else gets the default patch, diffed from the last generated
XAML. This matches the previous behavior.
- Every patched instance then records the new key, so running the patch
again, or for a C#-only edit, changes nothing.
- When there is nothing to patch, the output is unchanged: an empty UC,
with no marker. Rude-edit rollback behavior is preserved, and the older
applied generation now also gets its own branch.

**Known edge, not a regression:** a retained generation whose diff would
assign different IDs from the current ones (e.g. some reverts) is not
given a branch. Instances at that generation fall back to the default
patch, which is the old behavior.

### Issues Fixed

Fixes #36937 (remaining limitation from #40330)

### Tests

-
`XamlIncrementalHotReloadE2ETests.ThrowawayRunWithDifferentXaml_PatchStartsFromTheAppliedGeneration`:
real metadata update. Applied `Text="Hello"`, then a throwaway run with
`Text="World" FontSize="30"`, then the real edit to `Text="World"`. The
live label ends at `World` with its default `FontSize`, re-running UC is
a no-op, and new instances are already current. A new harness hook,
`GenerateWithThrowawayRuns`, runs and discards a forked driver run.
-
`XamlIncrementalHotReloadPipelineTests.ThrowawayRunWithDifferentXaml_PatchesFromEveryRetainedGeneration`:
checks the branch structure and determinism (same snapshot and C#-only
edits give an identical UC).
- `XamlIncrementalHotReloadPipelineTests.RetainedGenerations_AreBounded`
- Existing rude-edit assertions now target the default patch
(`UpdateComponentText.DefaultPatch`), because the UC legitimately
contains a branch for the older applied generation.

### Validation

- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj`:
**passed**, 899/899.
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
--filter "FullyQualifiedName~HotReload"`: **passed**, 12/12.
- `DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj`: 2274
passed, 12 failed. All 12 failures are `CultureNotFoundException` from
culture-specific tests under invariant-globalization mode; none involve
hot reload.
- `dotnet format whitespace Microsoft.Maui.sln --verify-no-changes
--include <changed files>`: **passed**.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@kubaflo kubaflo mentioned this pull request Nov 18, 2026
1 task
kubaflo pushed a commit that referenced this pull request Nov 18, 2026
…ns (#40378)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

> [!IMPORTANT]
> **Base: `candidate/11.0.1xx-sr2` (decided).** This PR builds on
#40233, #40235, #40329 and #40330, which exist only on
`candidate/11.0.1xx-sr2` (not on `net11`, `main` or
`inflight/11.0.1xx-sr3`). Against `net11` the diff would include about
527 unrelated files. As with #40329 and #40330, it targets
`candidate/11.0.1xx-sr2` directly so it ships together with its
prerequisites.

### Description of Change

#40330 left one limitation. A throwaway generator run with different
XAML, such as a design-time or speculative run whose output never
reaches the app, advanced the cached previous XAML. The next real
`UpdateComponent()` was then diffed from that intermediate generation
instead of the generation the running app actually has. The result was
missed edits: for example, an edit already present in the throwaway XAML
was never applied.

The generator cannot tell which of its runs reached the app, so this PR
has `UpdateComponent()` decide at runtime:

- **Generation key.** Each successful generation gets a key: a SHA-256
of the normalized XAML plus the node IDs
(`NodeIdHelper.ComputeGenerationKey`). `XamlHotReloadState` keeps the
current generation and the previously generated ones as baselines, up to
`MaxBaselines = 4`.
- **InitializeComponent** records `XamlComponentRegistry.Register(this,
"__xihr_generation:<type>", key)`.
- **UpdateComponent** reads the instance's recorded key:
- A retained generation whose diff produces exactly the current node IDs
gets its own branch: the patch diffed from that generation.
- Anything else gets the default patch, diffed from the last generated
XAML. This matches the previous behavior.
- Every patched instance then records the new key, so running the patch
again, or for a C#-only edit, changes nothing.
- When there is nothing to patch, the output is unchanged: an empty UC,
with no marker. Rude-edit rollback behavior is preserved, and the older
applied generation now also gets its own branch.

**Known edge, not a regression:** a retained generation whose diff would
assign different IDs from the current ones (e.g. some reverts) is not
given a branch. Instances at that generation fall back to the default
patch, which is the old behavior.

### Issues Fixed

Fixes #36937 (remaining limitation from #40330)

### Tests

-
`XamlIncrementalHotReloadE2ETests.ThrowawayRunWithDifferentXaml_PatchStartsFromTheAppliedGeneration`:
real metadata update. Applied `Text="Hello"`, then a throwaway run with
`Text="World" FontSize="30"`, then the real edit to `Text="World"`. The
live label ends at `World` with its default `FontSize`, re-running UC is
a no-op, and new instances are already current. A new harness hook,
`GenerateWithThrowawayRuns`, runs and discards a forked driver run.
-
`XamlIncrementalHotReloadPipelineTests.ThrowawayRunWithDifferentXaml_PatchesFromEveryRetainedGeneration`:
checks the branch structure and determinism (same snapshot and C#-only
edits give an identical UC).
- `XamlIncrementalHotReloadPipelineTests.RetainedGenerations_AreBounded`
- Existing rude-edit assertions now target the default patch
(`UpdateComponentText.DefaultPatch`), because the UC legitimately
contains a branch for the older applied generation.

### Validation

- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj`:
**passed**, 899/899.
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
--filter "FullyQualifiedName~HotReload"`: **passed**, 12/12.
- `DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj`: 2274
passed, 12 failed. All 12 failures are `CultureNotFoundException` from
culture-specific tests under invariant-globalization mode; none involve
hot reload.
- `dotnet format whitespace Microsoft.Maui.sln --verify-no-changes
--include <changed files>`: **passed**.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo pushed a commit that referenced this pull request Nov 19, 2026
…40413) (#40417)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

**Base is `candidate/11.0.1xx-sr2`, not `net11`.** The PR is test-only
and covers behavior that exists only on the candidate branch, from the
#36937 fix chain #40233, #40235, #40329, #40330, and #40378. Against
`net11` the test fails, because that fix has not flowed there yet. This
follows the precedent of #40329, #40330, and #40378, which targeted this
branch for the same reason. Maintainers: retarget if you prefer a
different lane.

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

#40413 reports that in VS on Windows (.NET 11, SourceGen inflator, XAML
Incremental Hot Reload on by default in Debug), editing a Label's `Text`
in the default template's `MainPage.xaml` is not applied.

**Root cause.** This is #36937. Roslyn/VS evaluate the same edited
generator-driver snapshot more than once, for example forked or
speculative runs, or a throwaway compilation before the applied one. On
`net11`, the first evaluation advances the cached previous XAML. The
evaluation that is actually applied then sees no diff and emits an empty
`UpdateComponent`, and the runtime silently skips it. The bug became
visible in .NET 11 because XIHR is now the default; 10.0.120 used legacy
XAML Hot Reload.

I reproduced this with the issue's own template `MainPage.xaml` through
the source generator:

| Sequence | `net11` (a25e629) | `candidate/11.0.1xx-sr2`
(cec6650) |
| --- | --- | --- |
| Edit evaluated once | patch | patch |
| Same edited snapshot evaluated twice: 2nd run | **empty UC** | patch |
| Throwaway compilation first, then real run: real run | **empty UC** |
patch |

On the candidate branch the existing fixes already handle this. This PR
adds
`DefaultTemplateMainPageTextEdit_SurvivesRepeatedAndThrowawayEvaluations`,
a theory with repeated and throwaway-first cases. It uses the template
page shape (`StaticResource` styles from a sibling `Styles.xaml`,
`x:Name`, a `Clicked` handler in code-behind) to lock in the reported
scenario. There are no product changes.

**Android .NET 10 (validation comment).** This is a separate scenario.
XIHR was not the default in .NET 10, so that report involves the legacy
XAML Hot Reload path and is not caused by this regression. It is not
addressed here.

**Validation**
- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj
--filter
"FullyQualifiedName~XamlIncrementalHotReloadPipelineTests|FullyQualifiedName~CodeBehindFieldStabilityTests"`:
**passed**, 93/93, on this branch.
- `net11`: the same two sequences, run through an equivalent temporary
probe with the template XAML, produce an empty UC, as shown in the
table. I did not port the committed test itself to `net11`: its helpers
exist only on candidate, and reverting the fix commits in place
conflicts.
- Not verified in Visual Studio on Windows or on device; I worked on a
macOS host.

### Issues Fixed

Fixes #40413

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo pushed a commit that referenced this pull request Nov 19, 2026
…ns (#40378)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

> [!IMPORTANT]
> **Base: `candidate/11.0.1xx-sr2` (decided).** This PR builds on
#40233, #40235, #40329 and #40330, which exist only on
`candidate/11.0.1xx-sr2` (not on `net11`, `main` or
`inflight/11.0.1xx-sr3`). Against `net11` the diff would include about
527 unrelated files. As with #40329 and #40330, it targets
`candidate/11.0.1xx-sr2` directly so it ships together with its
prerequisites.

### Description of Change

#40330 left one limitation. A throwaway generator run with different
XAML, such as a design-time or speculative run whose output never
reaches the app, advanced the cached previous XAML. The next real
`UpdateComponent()` was then diffed from that intermediate generation
instead of the generation the running app actually has. The result was
missed edits: for example, an edit already present in the throwaway XAML
was never applied.

The generator cannot tell which of its runs reached the app, so this PR
has `UpdateComponent()` decide at runtime:

- **Generation key.** Each successful generation gets a key: a SHA-256
of the normalized XAML plus the node IDs
(`NodeIdHelper.ComputeGenerationKey`). `XamlHotReloadState` keeps the
current generation and the previously generated ones as baselines, up to
`MaxBaselines = 4`.
- **InitializeComponent** records `XamlComponentRegistry.Register(this,
"__xihr_generation:<type>", key)`.
- **UpdateComponent** reads the instance's recorded key:
- A retained generation whose diff produces exactly the current node IDs
gets its own branch: the patch diffed from that generation.
- Anything else gets the default patch, diffed from the last generated
XAML. This matches the previous behavior.
- Every patched instance then records the new key, so running the patch
again, or for a C#-only edit, changes nothing.
- When there is nothing to patch, the output is unchanged: an empty UC,
with no marker. Rude-edit rollback behavior is preserved, and the older
applied generation now also gets its own branch.

**Known edge, not a regression:** a retained generation whose diff would
assign different IDs from the current ones (e.g. some reverts) is not
given a branch. Instances at that generation fall back to the default
patch, which is the old behavior.

### Issues Fixed

Fixes #36937 (remaining limitation from #40330)

### Tests

-
`XamlIncrementalHotReloadE2ETests.ThrowawayRunWithDifferentXaml_PatchStartsFromTheAppliedGeneration`:
real metadata update. Applied `Text="Hello"`, then a throwaway run with
`Text="World" FontSize="30"`, then the real edit to `Text="World"`. The
live label ends at `World` with its default `FontSize`, re-running UC is
a no-op, and new instances are already current. A new harness hook,
`GenerateWithThrowawayRuns`, runs and discards a forked driver run.
-
`XamlIncrementalHotReloadPipelineTests.ThrowawayRunWithDifferentXaml_PatchesFromEveryRetainedGeneration`:
checks the branch structure and determinism (same snapshot and C#-only
edits give an identical UC).
- `XamlIncrementalHotReloadPipelineTests.RetainedGenerations_AreBounded`
- Existing rude-edit assertions now target the default patch
(`UpdateComponentText.DefaultPatch`), because the UC legitimately
contains a branch for the older applied generation.

### Validation

- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj`:
**passed**, 899/899.
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
--filter "FullyQualifiedName~HotReload"`: **passed**, 12/12.
- `DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj`: 2274
passed, 12 failed. All 12 failures are `CultureNotFoundException` from
culture-specific tests under invariant-globalization mode; none involve
hot reload.
- `dotnet format whitespace Microsoft.Maui.sln --verify-no-changes
--include <changed files>`: **passed**.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo pushed a commit that referenced this pull request Nov 19, 2026
…40413) (#40417)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

**Base is `candidate/11.0.1xx-sr2`, not `net11`.** The PR is test-only
and covers behavior that exists only on the candidate branch, from the
#36937 fix chain #40233, #40235, #40329, #40330, and #40378. Against
`net11` the test fails, because that fix has not flowed there yet. This
follows the precedent of #40329, #40330, and #40378, which targeted this
branch for the same reason. Maintainers: retarget if you prefer a
different lane.

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

#40413 reports that in VS on Windows (.NET 11, SourceGen inflator, XAML
Incremental Hot Reload on by default in Debug), editing a Label's `Text`
in the default template's `MainPage.xaml` is not applied.

**Root cause.** This is #36937. Roslyn/VS evaluate the same edited
generator-driver snapshot more than once, for example forked or
speculative runs, or a throwaway compilation before the applied one. On
`net11`, the first evaluation advances the cached previous XAML. The
evaluation that is actually applied then sees no diff and emits an empty
`UpdateComponent`, and the runtime silently skips it. The bug became
visible in .NET 11 because XIHR is now the default; 10.0.120 used legacy
XAML Hot Reload.

I reproduced this with the issue's own template `MainPage.xaml` through
the source generator:

| Sequence | `net11` (a25e629) | `candidate/11.0.1xx-sr2`
(cec6650) |
| --- | --- | --- |
| Edit evaluated once | patch | patch |
| Same edited snapshot evaluated twice: 2nd run | **empty UC** | patch |
| Throwaway compilation first, then real run: real run | **empty UC** |
patch |

On the candidate branch the existing fixes already handle this. This PR
adds
`DefaultTemplateMainPageTextEdit_SurvivesRepeatedAndThrowawayEvaluations`,
a theory with repeated and throwaway-first cases. It uses the template
page shape (`StaticResource` styles from a sibling `Styles.xaml`,
`x:Name`, a `Clicked` handler in code-behind) to lock in the reported
scenario. There are no product changes.

**Android .NET 10 (validation comment).** This is a separate scenario.
XIHR was not the default in .NET 10, so that report involves the legacy
XAML Hot Reload path and is not caused by this regression. It is not
addressed here.

**Validation**
- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj
--filter
"FullyQualifiedName~XamlIncrementalHotReloadPipelineTests|FullyQualifiedName~CodeBehindFieldStabilityTests"`:
**passed**, 93/93, on this branch.
- `net11`: the same two sequences, run through an equivalent temporary
probe with the template XAML, produce an empty UC, as shown in the
table. I did not port the committed test itself to `net11`: its helpers
exist only on candidate, and reverting the fix commits in place
conflicts.
- Not verified in Visual Studio on Windows or on device; I worked on a
macOS host.

### Issues Fixed

Fixes #40413

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo pushed a commit that referenced this pull request Nov 22, 2026
…ns (#40378)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

> [!IMPORTANT]
> **Base: `candidate/11.0.1xx-sr2` (decided).** This PR builds on
#40233, #40235, #40329 and #40330, which exist only on
`candidate/11.0.1xx-sr2` (not on `net11`, `main` or
`inflight/11.0.1xx-sr3`). Against `net11` the diff would include about
527 unrelated files. As with #40329 and #40330, it targets
`candidate/11.0.1xx-sr2` directly so it ships together with its
prerequisites.

### Description of Change

#40330 left one limitation. A throwaway generator run with different
XAML, such as a design-time or speculative run whose output never
reaches the app, advanced the cached previous XAML. The next real
`UpdateComponent()` was then diffed from that intermediate generation
instead of the generation the running app actually has. The result was
missed edits: for example, an edit already present in the throwaway XAML
was never applied.

The generator cannot tell which of its runs reached the app, so this PR
has `UpdateComponent()` decide at runtime:

- **Generation key.** Each successful generation gets a key: a SHA-256
of the normalized XAML plus the node IDs
(`NodeIdHelper.ComputeGenerationKey`). `XamlHotReloadState` keeps the
current generation and the previously generated ones as baselines, up to
`MaxBaselines = 4`.
- **InitializeComponent** records `XamlComponentRegistry.Register(this,
"__xihr_generation:<type>", key)`.
- **UpdateComponent** reads the instance's recorded key:
- A retained generation whose diff produces exactly the current node IDs
gets its own branch: the patch diffed from that generation.
- Anything else gets the default patch, diffed from the last generated
XAML. This matches the previous behavior.
- Every patched instance then records the new key, so running the patch
again, or for a C#-only edit, changes nothing.
- When there is nothing to patch, the output is unchanged: an empty UC,
with no marker. Rude-edit rollback behavior is preserved, and the older
applied generation now also gets its own branch.

**Known edge, not a regression:** a retained generation whose diff would
assign different IDs from the current ones (e.g. some reverts) is not
given a branch. Instances at that generation fall back to the default
patch, which is the old behavior.

### Issues Fixed

Fixes #36937 (remaining limitation from #40330)

### Tests

-
`XamlIncrementalHotReloadE2ETests.ThrowawayRunWithDifferentXaml_PatchStartsFromTheAppliedGeneration`:
real metadata update. Applied `Text="Hello"`, then a throwaway run with
`Text="World" FontSize="30"`, then the real edit to `Text="World"`. The
live label ends at `World` with its default `FontSize`, re-running UC is
a no-op, and new instances are already current. A new harness hook,
`GenerateWithThrowawayRuns`, runs and discards a forked driver run.
-
`XamlIncrementalHotReloadPipelineTests.ThrowawayRunWithDifferentXaml_PatchesFromEveryRetainedGeneration`:
checks the branch structure and determinism (same snapshot and C#-only
edits give an identical UC).
- `XamlIncrementalHotReloadPipelineTests.RetainedGenerations_AreBounded`
- Existing rude-edit assertions now target the default patch
(`UpdateComponentText.DefaultPatch`), because the UC legitimately
contains a branch for the older applied generation.

### Validation

- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj`:
**passed**, 899/899.
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
--filter "FullyQualifiedName~HotReload"`: **passed**, 12/12.
- `DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj`: 2274
passed, 12 failed. All 12 failures are `CultureNotFoundException` from
culture-specific tests under invariant-globalization mode; none involve
hot reload.
- `dotnet format whitespace Microsoft.Maui.sln --verify-no-changes
--include <changed files>`: **passed**.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo pushed a commit that referenced this pull request Nov 22, 2026
…40413) (#40417)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

**Base is `candidate/11.0.1xx-sr2`, not `net11`.** The PR is test-only
and covers behavior that exists only on the candidate branch, from the
#36937 fix chain #40233, #40235, #40329, #40330, and #40378. Against
`net11` the test fails, because that fix has not flowed there yet. This
follows the precedent of #40329, #40330, and #40378, which targeted this
branch for the same reason. Maintainers: retarget if you prefer a
different lane.

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

#40413 reports that in VS on Windows (.NET 11, SourceGen inflator, XAML
Incremental Hot Reload on by default in Debug), editing a Label's `Text`
in the default template's `MainPage.xaml` is not applied.

**Root cause.** This is #36937. Roslyn/VS evaluate the same edited
generator-driver snapshot more than once, for example forked or
speculative runs, or a throwaway compilation before the applied one. On
`net11`, the first evaluation advances the cached previous XAML. The
evaluation that is actually applied then sees no diff and emits an empty
`UpdateComponent`, and the runtime silently skips it. The bug became
visible in .NET 11 because XIHR is now the default; 10.0.120 used legacy
XAML Hot Reload.

I reproduced this with the issue's own template `MainPage.xaml` through
the source generator:

| Sequence | `net11` (a25e629) | `candidate/11.0.1xx-sr2`
(cec6650) |
| --- | --- | --- |
| Edit evaluated once | patch | patch |
| Same edited snapshot evaluated twice: 2nd run | **empty UC** | patch |
| Throwaway compilation first, then real run: real run | **empty UC** |
patch |

On the candidate branch the existing fixes already handle this. This PR
adds
`DefaultTemplateMainPageTextEdit_SurvivesRepeatedAndThrowawayEvaluations`,
a theory with repeated and throwaway-first cases. It uses the template
page shape (`StaticResource` styles from a sibling `Styles.xaml`,
`x:Name`, a `Clicked` handler in code-behind) to lock in the reported
scenario. There are no product changes.

**Android .NET 10 (validation comment).** This is a separate scenario.
XIHR was not the default in .NET 10, so that report involves the legacy
XAML Hot Reload path and is not caused by this regression. It is not
addressed here.

**Validation**
- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj
--filter
"FullyQualifiedName~XamlIncrementalHotReloadPipelineTests|FullyQualifiedName~CodeBehindFieldStabilityTests"`:
**passed**, 93/93, on this branch.
- `net11`: the same two sequences, run through an equivalent temporary
probe with the template XAML, produce an empty UC, as shown in the
table. I did not port the committed test itself to `net11`: its helpers
exist only on candidate, and reverting the fix commits in place
conflicts.
- Not verified in Visual Studio on Windows or on device; I worked on a
macOS host.

### Issues Fixed

Fixes #40413

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo pushed a commit that referenced this pull request Nov 23, 2026
…ns (#40378)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

> [!IMPORTANT]
> **Base: `candidate/11.0.1xx-sr2` (decided).** This PR builds on
#40233, #40235, #40329 and #40330, which exist only on
`candidate/11.0.1xx-sr2` (not on `net11`, `main` or
`inflight/11.0.1xx-sr3`). Against `net11` the diff would include about
527 unrelated files. As with #40329 and #40330, it targets
`candidate/11.0.1xx-sr2` directly so it ships together with its
prerequisites.

### Description of Change

#40330 left one limitation. A throwaway generator run with different
XAML, such as a design-time or speculative run whose output never
reaches the app, advanced the cached previous XAML. The next real
`UpdateComponent()` was then diffed from that intermediate generation
instead of the generation the running app actually has. The result was
missed edits: for example, an edit already present in the throwaway XAML
was never applied.

The generator cannot tell which of its runs reached the app, so this PR
has `UpdateComponent()` decide at runtime:

- **Generation key.** Each successful generation gets a key: a SHA-256
of the normalized XAML plus the node IDs
(`NodeIdHelper.ComputeGenerationKey`). `XamlHotReloadState` keeps the
current generation and the previously generated ones as baselines, up to
`MaxBaselines = 4`.
- **InitializeComponent** records `XamlComponentRegistry.Register(this,
"__xihr_generation:<type>", key)`.
- **UpdateComponent** reads the instance's recorded key:
- A retained generation whose diff produces exactly the current node IDs
gets its own branch: the patch diffed from that generation.
- Anything else gets the default patch, diffed from the last generated
XAML. This matches the previous behavior.
- Every patched instance then records the new key, so running the patch
again, or for a C#-only edit, changes nothing.
- When there is nothing to patch, the output is unchanged: an empty UC,
with no marker. Rude-edit rollback behavior is preserved, and the older
applied generation now also gets its own branch.

**Known edge, not a regression:** a retained generation whose diff would
assign different IDs from the current ones (e.g. some reverts) is not
given a branch. Instances at that generation fall back to the default
patch, which is the old behavior.

### Issues Fixed

Fixes #36937 (remaining limitation from #40330)

### Tests

-
`XamlIncrementalHotReloadE2ETests.ThrowawayRunWithDifferentXaml_PatchStartsFromTheAppliedGeneration`:
real metadata update. Applied `Text="Hello"`, then a throwaway run with
`Text="World" FontSize="30"`, then the real edit to `Text="World"`. The
live label ends at `World` with its default `FontSize`, re-running UC is
a no-op, and new instances are already current. A new harness hook,
`GenerateWithThrowawayRuns`, runs and discards a forked driver run.
-
`XamlIncrementalHotReloadPipelineTests.ThrowawayRunWithDifferentXaml_PatchesFromEveryRetainedGeneration`:
checks the branch structure and determinism (same snapshot and C#-only
edits give an identical UC).
- `XamlIncrementalHotReloadPipelineTests.RetainedGenerations_AreBounded`
- Existing rude-edit assertions now target the default patch
(`UpdateComponentText.DefaultPatch`), because the UC legitimately
contains a branch for the older applied generation.

### Validation

- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj`:
**passed**, 899/899.
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
--filter "FullyQualifiedName~HotReload"`: **passed**, 12/12.
- `DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj`: 2274
passed, 12 failed. All 12 failures are `CultureNotFoundException` from
culture-specific tests under invariant-globalization mode; none involve
hot reload.
- `dotnet format whitespace Microsoft.Maui.sln --verify-no-changes
--include <changed files>`: **passed**.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo pushed a commit that referenced this pull request Nov 23, 2026
…40413) (#40417)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

**Base is `candidate/11.0.1xx-sr2`, not `net11`.** The PR is test-only
and covers behavior that exists only on the candidate branch, from the
#36937 fix chain #40233, #40235, #40329, #40330, and #40378. Against
`net11` the test fails, because that fix has not flowed there yet. This
follows the precedent of #40329, #40330, and #40378, which targeted this
branch for the same reason. Maintainers: retarget if you prefer a
different lane.

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

#40413 reports that in VS on Windows (.NET 11, SourceGen inflator, XAML
Incremental Hot Reload on by default in Debug), editing a Label's `Text`
in the default template's `MainPage.xaml` is not applied.

**Root cause.** This is #36937. Roslyn/VS evaluate the same edited
generator-driver snapshot more than once, for example forked or
speculative runs, or a throwaway compilation before the applied one. On
`net11`, the first evaluation advances the cached previous XAML. The
evaluation that is actually applied then sees no diff and emits an empty
`UpdateComponent`, and the runtime silently skips it. The bug became
visible in .NET 11 because XIHR is now the default; 10.0.120 used legacy
XAML Hot Reload.

I reproduced this with the issue's own template `MainPage.xaml` through
the source generator:

| Sequence | `net11` (a25e629) | `candidate/11.0.1xx-sr2`
(cec6650) |
| --- | --- | --- |
| Edit evaluated once | patch | patch |
| Same edited snapshot evaluated twice: 2nd run | **empty UC** | patch |
| Throwaway compilation first, then real run: real run | **empty UC** |
patch |

On the candidate branch the existing fixes already handle this. This PR
adds
`DefaultTemplateMainPageTextEdit_SurvivesRepeatedAndThrowawayEvaluations`,
a theory with repeated and throwaway-first cases. It uses the template
page shape (`StaticResource` styles from a sibling `Styles.xaml`,
`x:Name`, a `Clicked` handler in code-behind) to lock in the reported
scenario. There are no product changes.

**Android .NET 10 (validation comment).** This is a separate scenario.
XIHR was not the default in .NET 10, so that report involves the legacy
XAML Hot Reload path and is not caused by this regression. It is not
addressed here.

**Validation**
- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj
--filter
"FullyQualifiedName~XamlIncrementalHotReloadPipelineTests|FullyQualifiedName~CodeBehindFieldStabilityTests"`:
**passed**, 93/93, on this branch.
- `net11`: the same two sequences, run through an equivalent temporary
probe with the template XAML, produce an empty UC, as shown in the
table. I did not port the committed test itself to `net11`: its helpers
exist only on candidate, and reverting the fix commits in place
conflicts.
- Not verified in Visual Studio on Windows or on device; I worked on a
macOS host.

### Issues Fixed

Fixes #40413

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@kubaflo kubaflo added this to the .NET 11 SR2 milestone Nov 24, 2026
KarthikRajaKalaimani pushed a commit to KarthikRajaKalaimani/maui that referenced this pull request Nov 26, 2026
…ns (dotnet#40378)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

> [!IMPORTANT]
> **Base: `candidate/11.0.1xx-sr2` (decided).** This PR builds on
dotnet#40233, dotnet#40235, dotnet#40329 and dotnet#40330, which exist only on
`candidate/11.0.1xx-sr2` (not on `net11`, `main` or
`inflight/11.0.1xx-sr3`). Against `net11` the diff would include about
527 unrelated files. As with dotnet#40329 and dotnet#40330, it targets
`candidate/11.0.1xx-sr2` directly so it ships together with its
prerequisites.

### Description of Change

dotnet#40330 left one limitation. A throwaway generator run with different
XAML, such as a design-time or speculative run whose output never
reaches the app, advanced the cached previous XAML. The next real
`UpdateComponent()` was then diffed from that intermediate generation
instead of the generation the running app actually has. The result was
missed edits: for example, an edit already present in the throwaway XAML
was never applied.

The generator cannot tell which of its runs reached the app, so this PR
has `UpdateComponent()` decide at runtime:

- **Generation key.** Each successful generation gets a key: a SHA-256
of the normalized XAML plus the node IDs
(`NodeIdHelper.ComputeGenerationKey`). `XamlHotReloadState` keeps the
current generation and the previously generated ones as baselines, up to
`MaxBaselines = 4`.
- **InitializeComponent** records `XamlComponentRegistry.Register(this,
"__xihr_generation:<type>", key)`.
- **UpdateComponent** reads the instance's recorded key:
- A retained generation whose diff produces exactly the current node IDs
gets its own branch: the patch diffed from that generation.
- Anything else gets the default patch, diffed from the last generated
XAML. This matches the previous behavior.
- Every patched instance then records the new key, so running the patch
again, or for a C#-only edit, changes nothing.
- When there is nothing to patch, the output is unchanged: an empty UC,
with no marker. Rude-edit rollback behavior is preserved, and the older
applied generation now also gets its own branch.

**Known edge, not a regression:** a retained generation whose diff would
assign different IDs from the current ones (e.g. some reverts) is not
given a branch. Instances at that generation fall back to the default
patch, which is the old behavior.

### Issues Fixed

Fixes dotnet#36937 (remaining limitation from dotnet#40330)

### Tests

-
`XamlIncrementalHotReloadE2ETests.ThrowawayRunWithDifferentXaml_PatchStartsFromTheAppliedGeneration`:
real metadata update. Applied `Text="Hello"`, then a throwaway run with
`Text="World" FontSize="30"`, then the real edit to `Text="World"`. The
live label ends at `World` with its default `FontSize`, re-running UC is
a no-op, and new instances are already current. A new harness hook,
`GenerateWithThrowawayRuns`, runs and discards a forked driver run.
-
`XamlIncrementalHotReloadPipelineTests.ThrowawayRunWithDifferentXaml_PatchesFromEveryRetainedGeneration`:
checks the branch structure and determinism (same snapshot and C#-only
edits give an identical UC).
- `XamlIncrementalHotReloadPipelineTests.RetainedGenerations_AreBounded`
- Existing rude-edit assertions now target the default patch
(`UpdateComponentText.DefaultPatch`), because the UC legitimately
contains a branch for the older applied generation.

### Validation

- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj`:
**passed**, 899/899.
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
--filter "FullyQualifiedName~HotReload"`: **passed**, 12/12.
- `DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj`: 2274
passed, 12 failed. All 12 failures are `CultureNotFoundException` from
culture-specific tests under invariant-globalization mode; none involve
hot reload.
- `dotnet format whitespace Microsoft.Maui.sln --verify-no-changes
--include <changed files>`: **passed**.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
KarthikRajaKalaimani pushed a commit to KarthikRajaKalaimani/maui that referenced this pull request Nov 26, 2026
…otnet#40413) (dotnet#40417)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

**Base is `candidate/11.0.1xx-sr2`, not `net11`.** The PR is test-only
and covers behavior that exists only on the candidate branch, from the
dotnet#36937 fix chain dotnet#40233, dotnet#40235, dotnet#40329, dotnet#40330, and dotnet#40378. Against
`net11` the test fails, because that fix has not flowed there yet. This
follows the precedent of dotnet#40329, dotnet#40330, and dotnet#40378, which targeted this
branch for the same reason. Maintainers: retarget if you prefer a
different lane.

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

dotnet#40413 reports that in VS on Windows (.NET 11, SourceGen inflator, XAML
Incremental Hot Reload on by default in Debug), editing a Label's `Text`
in the default template's `MainPage.xaml` is not applied.

**Root cause.** This is dotnet#36937. Roslyn/VS evaluate the same edited
generator-driver snapshot more than once, for example forked or
speculative runs, or a throwaway compilation before the applied one. On
`net11`, the first evaluation advances the cached previous XAML. The
evaluation that is actually applied then sees no diff and emits an empty
`UpdateComponent`, and the runtime silently skips it. The bug became
visible in .NET 11 because XIHR is now the default; 10.0.120 used legacy
XAML Hot Reload.

I reproduced this with the issue's own template `MainPage.xaml` through
the source generator:

| Sequence | `net11` (a25e629) | `candidate/11.0.1xx-sr2`
(cec6650) |
| --- | --- | --- |
| Edit evaluated once | patch | patch |
| Same edited snapshot evaluated twice: 2nd run | **empty UC** | patch |
| Throwaway compilation first, then real run: real run | **empty UC** |
patch |

On the candidate branch the existing fixes already handle this. This PR
adds
`DefaultTemplateMainPageTextEdit_SurvivesRepeatedAndThrowawayEvaluations`,
a theory with repeated and throwaway-first cases. It uses the template
page shape (`StaticResource` styles from a sibling `Styles.xaml`,
`x:Name`, a `Clicked` handler in code-behind) to lock in the reported
scenario. There are no product changes.

**Android .NET 10 (validation comment).** This is a separate scenario.
XIHR was not the default in .NET 10, so that report involves the legacy
XAML Hot Reload path and is not caused by this regression. It is not
addressed here.

**Validation**
- `DOTNET_MODIFIABLE_ASSEMBLIES=debug dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj
--filter
"FullyQualifiedName~XamlIncrementalHotReloadPipelineTests|FullyQualifiedName~CodeBehindFieldStabilityTests"`:
**passed**, 93/93, on this branch.
- `net11`: the same two sequences, run through an equivalent temporary
probe with the template XAML, produce an empty UC, as shown in the
table. I did not port the committed test itself to `net11`: its helpers
exist only on candidate, and reverting the fix commits in place
conflicts.
- Not verified in Visual Studio on Windows or on device; I worked on a
macOS host.

### Issues Fixed

Fixes dotnet#40413

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants