Skip to content

[P0 blocker][XAML SourceGen Hot Reload] Subsequent updates fail with ENC1002 after generated method identity is lost #36824

Description

@rmarinho

Description

.NET 11 Preview 7's opt-in Incremental XAML Hot Reload / XAML SourceGen path can fail on a subsequent Hot Reload update with:

ENC1002: Cannot apply changes -- unexpected error

System.NullReferenceException / InvalidOperationException
at Microsoft.CodeAnalysis.Emit.DefinitionMap.GetPreviousMethodHandle(...)
at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.CreateDeletedMemberDefs(...)
at Microsoft.CodeAnalysis.Emit.EmitHelpers.EmitDifference(...)

The complete minimal analysis and EnC logs are in:

Roslyn closed both issues as Resolution-External. The maintainer diagnosis on #84569 was:

The root cause of the problem is that the source generator is non-deterministic.

This therefore needs a canonical MAUI-side blocker rather than being considered fixed because the Roslyn issue is closed.

Why this is a P0 blocker

This blocks trustworthy validation of the .NET 11 Preview 7 opt-in XAML SourceGen/Incremental Hot Reload feature:

  • A first XAML update can succeed, while a later update crashes Roslyn's EnC delta emitter.
  • The failure affects the core repeated-edit workflow that Hot Reload must support.
  • It can affect both Visual Studio and VS Code because both consume the generated code through Roslyn EnC.
  • A closed Roslyn issue currently gives a false impression that the defect was fixed.

Scope is intentionally narrow: this blocks the opt-in .NET 11 XAML SourceGen/Incremental path. It is not evidence that Legacy/default XAML Hot Reload is broken.

Root cause

The Incremental XAML Hot Reload generator can emit UpdateComponent() only in an EnC delta rather than in generation 0. On a later generation, Roslyn can no longer map that delta-added method:

  1. UpdateComponent() is introduced in generated *.xaml.uc.xsg.g.cs output after the baseline.
  2. A subsequent generator output does not deterministically preserve/re-emit the method.
  3. Roslyn's definition mapping loses the corresponding EmitBaseline.MethodsAdded entry.
  4. A later update/delete reaches DefinitionMap.GetPreviousMethodHandle, which cannot resolve the previous method and throws.

The MAUI generator must produce stable output across every generation, including C#-only edits, invalid/intermediate XAML, structural resets, and no-op patches.

Steps to reproduce

Use the minimal reproduction and diagnostic logs from:

dotnet/roslyn#84569

At a high level:

  1. Use a .NET 11 Preview 7 MAUI app with XAML SourceGen/Incremental Hot Reload enabled:

    <MauiXamlInflator>SourceGen</MauiXamlInflator>
    <MauiXamlHotReload>SourceGen</MauiXamlHotReload>
    <EnableIncrementalHotReload>true</EnableIncrementalHotReload>
  2. Start a managed Hot Reload session.

  3. Apply the first XAML update that introduces the generated patch method.

  4. Apply subsequent XAML and/or interleaved C# updates.

  5. Observe ENC1002 and the GetPreviousMethodHandle exception on a later update.

Expected behavior

Every subsequent XAML or interleaved C# update applies successfully. Generated patch methods retain deterministic identity across all EnC generations.

Actual behavior

A later update fails in Roslyn's EnC delta emitter because a previously delta-added generated method can no longer be resolved.

Current fix status

No fix is merged or shipped as of 2026-07-27:

Related issues that should remain separate

Suggested fix direction

Revive the intent of #36680 while addressing its review concerns:

  • deterministically preserve/re-emit UpdateComponent() across all generator branches;
  • avoid unbounded static state or retain state only for the bounded lifetime of the active compilation/session;
  • avoid broad exception swallowing;
  • add regression tests for:
    • repeated XAML edits;
    • XAML edit followed by C#-only edit;
    • invalid/intermediate XAML followed by recovery;
    • structural reset/full-page transitions;
    • multiple pages/projects without state leaking between compilations.

Exit criteria

  • A deterministic MAUI generator fix is merged into net11.0.
  • The fix is present in the .NET 11 Preview/MAUI build used by VS and VS Code.
  • Repeated-edit regression tests cover the exact #84569 failure.
  • Device/desktop E2E verifies multiple consecutive XAML updates and interleaved XAML/C# updates.
  • No ENC1002, GetPreviousMethodHandle exception, stale generator state, or memory retention across repeated sessions.

Version and platforms

  • Version: .NET 11 Preview 7 / MAUI net11.0 CI builds with Incremental XAML Hot Reload enabled.
  • Platforms: generator/EnC-layer issue; potentially cross-platform. The Roslyn repro used Windows, while related SourceGen repeated-edit failures have also been observed on Android.
  • Regression: new opt-in feature introduced by [XAML] Incremental XAML Hot Reload (source-generated patch chains) #34338; no prior stable version of this feature.

Metadata

Metadata

Labels

area-toolingXAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debuggingarea-xamlXAML, CSS, Triggers, BehaviorsblockerThis issue/PR is currently blocking other issues/PRsp/0Current heighest priority issues that we are targeting for a release.partner/hot-reload-xamlIssues impacting XAML Hot Reload experiencess/triagedIssue has been reviewedt/bugSomething isn't working

Type

No type

Projects

Status
In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions