Repository navigation
Fix SourceGen hot reload for application resources #37896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Reflection; | ||
| using Microsoft.Maui.ApplicationModel; | ||
| using Microsoft.Maui.Controls.SourceGen.UnitTests.HotReload; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.Controls.SourceGen.UnitTests; | ||
|
|
||
| [Collection("XamlHotReloadTests")] | ||
| public class Maui37888Tests | ||
| { | ||
| const string IncrementalHotReloadSwitch = "Microsoft.Maui.RuntimeFeature.IsIncrementalHotReloadEnabled"; | ||
| const string ResourceKey = "ExploratoryCaption"; | ||
|
|
||
| [MetadataUpdateTheory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public void ApplicationResourceUpdate_ReachesExistingAndNewConsumers(bool useMergedDictionary) | ||
| { | ||
| AppContext.TryGetSwitch(IncrementalHotReloadSwitch, out var previousSwitch); | ||
| var previousApplication = Application.Current; | ||
| AppContext.SetSwitch(IncrementalHotReloadSwitch, true); | ||
| SetMainThreadImplementation(); | ||
|
|
||
| try | ||
| { | ||
| if (useMergedDictionary) | ||
| VerifyMergedDictionaryUpdate(); | ||
| else | ||
| VerifyInlineApplicationResourceUpdate(); | ||
| } | ||
| finally | ||
| { | ||
| Application.Current = previousApplication; | ||
| AppContext.SetSwitch(IncrementalHotReloadSwitch, previousSwitch); | ||
| ClearMainThreadImplementation(); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NullConvertedResource_GuardsComponentRegistration() | ||
| { | ||
| const string rootClass = "TestMaui37888.NullResources"; | ||
| const string codeBehind = """ | ||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Globalization; | ||
|
|
||
| namespace TestMaui37888; | ||
|
|
||
| [TypeConverter(typeof(NullResourceConverter))] | ||
| public sealed class NullResource | ||
| { | ||
| } | ||
|
|
||
| public sealed class NullResourceConverter : TypeConverter | ||
| { | ||
| public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) => | ||
| sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); | ||
|
|
||
| public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) => | ||
| null; | ||
| } | ||
|
|
||
| public partial class NullResources : global::Microsoft.Maui.Controls.ResourceDictionary | ||
| { | ||
| private partial void InitializeComponent(); | ||
|
|
||
| public NullResources() => InitializeComponent(); | ||
| } | ||
| """; | ||
|
|
||
| string Xaml(string value) => $$""" | ||
| <ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:local="clr-namespace:TestMaui37888" | ||
| x:Class="{{rootClass}}"> | ||
| <local:NullResource x:Key="NullResource">{{value}}</local:NullResource> | ||
| <x:String x:Key="FollowingResource">{{value}}</x:String> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[major] Regression Prevention and Test Coverage — Every resource exercised by the new tests is reference-typed: Add a case with a struct resource at the dictionary root — e.g. |
||
| </ResourceDictionary> | ||
| """; | ||
|
|
||
| using var harness = new XamlHotReloadTestHarness( | ||
| nameof(NullConvertedResource_GuardsComponentRegistration), | ||
| rootClass, | ||
| codeBehind); | ||
| var generation = harness.Generate(Xaml("V0"), Xaml("V1")); | ||
| var updateComponentSource = generation[1].UpdateComponentSource; | ||
|
|
||
| Assert.NotNull(updateComponentSource); | ||
| Assert.Contains("if (__uc_0 is not null)", updateComponentSource, StringComparison.Ordinal); | ||
| Assert.Contains("this[\"FollowingResource\"] = __uc_1;", updateComponentSource, StringComparison.Ordinal); | ||
| harness.Compile(generation[1]); | ||
| } | ||
|
|
||
| static void VerifyInlineApplicationResourceUpdate() | ||
| { | ||
| const string rootClass = "TestMaui37888.App"; | ||
| const string codeBehind = """ | ||
| namespace TestMaui37888; | ||
|
|
||
| public partial class App : global::Microsoft.Maui.Controls.Application | ||
| { | ||
| private partial void InitializeComponent(); | ||
|
|
||
| public App() => InitializeComponent(); | ||
| } | ||
| """; | ||
|
|
||
| string Xaml(string value) => $$""" | ||
| <Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="{{rootClass}}"> | ||
| <Application.Resources> | ||
| <x:String x:Key="{{ResourceKey}}">{{value}}</x:String> | ||
| </Application.Resources> | ||
| </Application> | ||
| """; | ||
|
|
||
| using var harness = new XamlHotReloadTestHarness( | ||
| nameof(ApplicationResourceUpdate_ReachesExistingAndNewConsumers) + "_Inline", | ||
| rootClass, | ||
| codeBehind); | ||
| var generation = harness.Generate(Xaml("App resource V0"), Xaml("App resource V1")); | ||
|
|
||
| harness.RunLive(generation, live => | ||
| { | ||
| var application = live.GetInstance<Application>(); | ||
| Application.Current = application; | ||
| VerifyConsumers(application, () => live.ApplyUpdateThroughRuntimeHandler<Application>(1)); | ||
| }); | ||
| } | ||
|
|
||
| static void VerifyMergedDictionaryUpdate() | ||
| { | ||
| const string rootClass = "TestMaui37888.AppResources"; | ||
| const string codeBehind = """ | ||
| namespace TestMaui37888; | ||
|
|
||
| public partial class AppResources : global::Microsoft.Maui.Controls.ResourceDictionary | ||
| { | ||
| private partial void InitializeComponent(); | ||
|
|
||
| public AppResources() => InitializeComponent(); | ||
| } | ||
| """; | ||
|
|
||
| string Xaml(string value) => $$""" | ||
| <ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="{{rootClass}}"> | ||
| <x:String x:Key="{{ResourceKey}}">{{value}}</x:String> | ||
| </ResourceDictionary> | ||
| """; | ||
|
|
||
| using var harness = new XamlHotReloadTestHarness( | ||
| nameof(ApplicationResourceUpdate_ReachesExistingAndNewConsumers) + "_Merged", | ||
| rootClass, | ||
| codeBehind); | ||
| var generation = harness.Generate(Xaml("App resource V0"), Xaml("App resource V1")); | ||
|
|
||
| harness.RunLive(generation, live => | ||
| { | ||
| var application = new Application(); | ||
| var dictionary = live.GetInstance<global::Microsoft.Maui.Controls.ResourceDictionary>(); | ||
| application.Resources.MergedDictionaries.Add(dictionary); | ||
| Application.Current = application; | ||
| VerifyConsumers(application, () => live.ApplyUpdateThroughRuntimeHandler<global::Microsoft.Maui.Controls.ResourceDictionary>(1)); | ||
| }); | ||
| } | ||
|
|
||
| static void VerifyConsumers(Application application, Action applyUpdate) | ||
| { | ||
| var existingConsumer = CreateConsumer(); | ||
| #pragma warning disable CS0618 | ||
| application.MainPage = new ContentPage { Content = existingConsumer }; | ||
| #pragma warning restore CS0618 | ||
| Assert.Equal("App resource V0", existingConsumer.Text); | ||
|
|
||
| applyUpdate(); | ||
|
|
||
| Assert.Equal("App resource V1", existingConsumer.Text); | ||
| Assert.Equal("App resource V1", CreateConsumer().Text); | ||
| Assert.Equal("App resource V1", application.Resources[ResourceKey]); | ||
| } | ||
|
|
||
| static Label CreateConsumer() | ||
| { | ||
| var label = new Label(); | ||
| label.SetDynamicResource(Label.TextProperty, ResourceKey); | ||
| return label; | ||
| } | ||
|
|
||
| static void SetMainThreadImplementation() | ||
| { | ||
| var method = typeof(MainThread).GetMethod( | ||
| "SetCustomImplementation", | ||
| BindingFlags.Static | BindingFlags.NonPublic, | ||
| binder: null, | ||
| [typeof(Func<bool>), typeof(Action<Action>)], | ||
| modifiers: null); | ||
| Assert.NotNull(method); | ||
| method!.Invoke(null, [() => true, (Action<Action>)(action => action())]); | ||
| } | ||
|
|
||
| static void ClearMainThreadImplementation() | ||
| { | ||
| var method = typeof(MainThread).GetMethod( | ||
| "ClearCustomImplementation", | ||
| BindingFlags.Static | BindingFlags.NonPublic); | ||
| Assert.NotNull(method); | ||
| method!.Invoke(null, null); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[critical] Logic and Correctness —
var {varName} = {valueExpression};infers the resource's own type, so the guard emitted two lines below (if ({varName} is not null)) is a null constant pattern against a non-nullable value type whenever the resource is a struct. That is a hard compile error, not a warning:error CS0037: Cannot convert null to 'double' because it is a non-nullable value type(verified against Roslyn withvar x = 14d; if (x is not null)).Concrete scenario: a standalone
x:ClassResourceDictionary containing<x:Double x:Key="FontSizeBody">14</x:Double>— the shape shipped by the MAUI project templates inStyles.xaml. Editing14→16produces exactly one__MAUI_Content__Set diff on a keyed root-level element with a singleValueNode, so every gate inTryEmitResourceDictionaryItemChangepasses andBuildResourceValueExpressionreturns adouble-typed expression viaConvertTo. The generatedUpdateComponent()then fails to compile and the hot-reload edit is lost. Same forThickness,GridLength,CornerRadius,x:Int32,x:Boolean,Size,Point— all common root-level RD entries. Reference-typed resources (Color,x:String) are unaffected, which is why this is invisible today.Fix is local: declare the local as
object?so the boxing conversion is explicit and the null guard is legal:The following
this["..."] = {varName};(indexer isobject) andXamlComponentRegistry.Register(this, ..., {varName})(parameter isobject) both already expectobject, so no other emitted line changes.