Repository navigation
Description
Follow-up to @tmat's review on #36833 (inline comment on src/Controls/src/SourceGen/XamlHotReloadState.cs).
Concern
XamlHotReloadState holds XAML Incremental Hot Reload (XIHR) generation state in mutable static state and diffs the CURRENT generation against a CACHED PREVIOUS one to produce the incremental patch (the UpdateComponent() body).
Tomas asked (paraphrased): "Sounds somewhat brittle. Would it work well if Roslyn created multiple compilations in the same process, ran source generators on them, and then threw them away?"
Why it's brittle
The cache is static (not tied to a specific Compilation) and survives across compilations in the same process — e.g. an IDE/design-time host, dotnet watch, or a test host that spins up several Compilations. Interleaved or throwaway compilations can:
- Miss a real change — if the "previous" cached snapshot came from an unrelated/throwaway compilation, the diff against the current content can come out empty, so
UpdateComponent()is emitted empty and Hot Reload silently drops a genuine XAML edit. - Report a spurious change — conversely, a stale cached "previous" can make an unchanged file look changed.
This matters now that XIHR is on-by-default in Debug (#36833) and the diagnostics signal is "empty UpdateComponent() body ⟺ no XAML change", classified pre-dispatch on UpdateRequested.HandledTypes. A poisoned cache corrupts that signal.
Options
- Key the cache by
Compilation(e.g.ConditionalWeakTable<Compilation, …>) so throwaway compilations don't cross-contaminate and entries are collected with the compilation. Lowest-risk; keeps the empty-UC signal. - Drop the cache entirely and always full-reapply the
UpdateComponent()body. Most robust, but thenUpdateComponent()is never empty, which breaks the empty-UC diagnostics signal — would require moving change-classification to a runtimeUpdateComponent()IL diff and re-aligning with the XamlTools team (@noiseonwires). - Move to incremental-generator-native caching (
IncrementalValueProvider) instead of hand-rolled static state.
Option 1 is the likely near-term fix; option 2 is the "correct" long-term design but carries a cross-team contract implication.
References
- Review comment: [XAML] XIHR: deterministic content-hash versioning + always-emit UpdateComponent #36833 (comment)
- Companion comment (dead
__version/StableContentHash) addressed in [XAML] XIHR: remove the now-dead __version field and StableContentHash #36912 - File:
src/Controls/src/SourceGen/XamlHotReloadState.cs
cc @tmat
Activity
noiseonwires commentedon Nov 6, 2026
We now have a concrete reproduction of the lost
UpdateComponentpatch described here, on the build containing #39928.Tested build
11.0.100-rc.3.26522.148.11.0.10-ci.pr39928.26555.75.1866354d267f3934c08f27eba64f6bbd476123aa(PR [XAML] Refresh implicit styles after incremental Hot Reload setter edits #39928 CI merge, containing head12d10d243b48a726917f84836d081524e61523a7).Important distinction: #39928 fixes the original implicit-style replacement problem in #39780. The attached minimal app now works in a manual VS test. A larger demo and CommunityToolkit.Maui.Sample still fail, but their captured deltas lack the live patch entirely; this is a separate generation-state problem, not evidence that executing the replacement code fails.
Deterministic generator-driver reproduction
Use the actual packaged
Microsoft.Maui.Controls.SourceGen.XamlGeneratorwith a reusable Roslyn driver. Supply a ResourceDictionary with an implicit Label style:Use a partial
Styles : ResourceDictionarywhose constructor callsInitializeComponent. Additional-file metadata selectsGenKind=Xaml,Inflator=SourceGen,EnableIncrementalHotReload=true, with consistent RelativePath/TargetPath/ManifestResourceName; the global configuration is Debug/net11.0. Replace only the Red literal with Blue.The critical sequence is two branches from the same immutable edited driver, not two distinct generator instances:
The second branch still generates Blue initialization, so relative to the Red baseline it changes InitializeComponent only. Compiling baseline initialization plus each generated patch and invoking it on retained implicit-style consumers gives:
A controlled probe passed 32 assertions across explicit/implicit XML namespaces and simple/complex styles, including the demo's actual implicit Label style with VisualStateGroups and AppThemeBinding resource lookups. The first patch works for all four shapes; the second branch loses it for all four. This rules out those style/namespace differences as an inherent reason the replacement cannot work.
Captured VS evidence
For the larger demo, the captured baseline dictionary's
InitializeComponenttoken is06000011, and itsUpdateComponentis06000013. The emitted Windows method list includes the former, not the latter. The baseline patch is two bytes,00 2A(nop; ret). The inactive Android target in the same apply does receive the typed style-replacement patch.For the subsequent Toolkit retest (update
2.1), Windows initialization token060000C6is updated, while patch token060000C8is omitted and its baseline is also00 2A. The old/new initialization snapshots differ at exactly one line,Colors.Red->Colors.Blue. All four Toolkit TFMs show initializer-only changes and no changed.uc.xsg.cssource in that apply.State mechanism
The per-driver session token does not isolate driver snapshots/branches sharing the same pipeline closure:
The first branch advances shared cached text to Blue before runtime apply acknowledgment. The second branch sees
previousXaml == currentXaml, computes no patch, and emits the empty method. Locking cache access does not provide snapshot isolation or deterministic output.Limit: the controlled test proves this failure mechanism and matches the captured VS output. The logs do not identify the exact earlier VS background/speculative invocation that consumed the patch in either session; that still needs cache/driver instrumentation.
Please add regression coverage for repeated evaluation/driver forks with identical edited inputs, alongside the existing linear metadata-update tests. A valid pending patch must not disappear because another generation evaluated the same XAML first. Baseline ownership also needs to remain correct across cancellation, rejected deltas, reverts, unrelated C# regeneration and concurrent design-time requests.
noiseonwires commentedon Nov 11, 2026
One proposal for preserving XIHR E2E classification while moving away from history-dependent empty/non-empty
UpdateComponent()bodies: generate a dedicated, XAML-only companion signal type per document.A XAML change changes the fingerprint method body, so the companion appears in Roslyn's changed-type list. A pure C# edit leaves it unchanged. The MUH recognizes companions in
updatedTypes, maps/deduplicates them to their roots, and preserves synchronousUpdateRequested.HandledTypesclassification plus the existingUpdateApplied/UpdateSkipped/UpdateFailedcontract. This needs neither page instances nor startup capture of old fingerprints; Roslyn's existing accepted compilation baseline supplies the comparison, without a new Roslyn/PS origin flag.A small
EmitDifferenceprobe passed four cases: C#-only excludes the companion; XAML-only, mixed, and replayed XAML edits include it. The signal must be independent of unrelated C# regeneration and use semantic fingerprinting to avoid formatting-only false positives.[XSG] XIHR: re-derive the pending UpdateComponent patch on repeated e…
[XSG] XIHR: re-derive the pending UpdateComponent patch on repeated e…
[XSG] XIHR: recover from rude edits that fail to compile (#40235)
[XSG] XIHR: re-derive the pending UpdateComponent patch on repeated e…
[XSG] XIHR: recover from rude edits that fail to compile (#40235)
[XSG] XIHR: re-derive the pending UpdateComponent patch on repeated e…
[XSG] XIHR: recover from rude edits that fail to compile (#40235)
[XSG] XIHR: keep the pending patch across throwaway compilations with…
[XSG] XIHR: re-derive the pending UpdateComponent patch on repeated e…
[XSG] XIHR: recover from rude edits that fail to compile (#40235)
22 remaining items
[XSG] XIHR: recover from rude edits that fail to compile (#40235)
[XSG] XIHR: keep the pending patch across throwaway compilations with…
[XIHR] Patch from the applied generation after throwaway generator ru…
[XIHR] Add regression test for default-template MainPage text edit (#…
[XSG] XIHR: re-derive the pending UpdateComponent patch on repeated e…
[XSG] XIHR: recover from rude edits that fail to compile (#40235)
[XSG] XIHR: keep the pending patch across throwaway compilations with…
[XIHR] Patch from the applied generation after throwaway generator ru…
[XIHR] Add regression test for default-template MainPage text edit (#…
[XSG] XIHR: re-derive the pending UpdateComponent patch on repeated e…
[XSG] XIHR: recover from rude edits that fail to compile (dotnet#40235)
[XSG] XIHR: keep the pending patch across throwaway compilations with…
[XIHR] Patch from the applied generation after throwaway generator ru…
[XIHR] Add regression test for default-template MainPage text edit (d…