Repository navigation
chore: Fix many trimmer warnings in src/Uno.UI - #21920
Conversation
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-21920/docs/index.html |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-21920/wasm-skia-net9/index.html |
|
The build 184497 found UI Test snapshots differences: Details
|
|
|
e96113f to
d0876bd
Compare
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-21920/docs/index.html |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-21920/wasm-skia-net9/index.html |
d0876bd to
1d5ca99
Compare
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-21920/docs/index.html |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-21920/wasm-skia-net9/index.html |
01f7aa1 to
6bff243
Compare
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-21920/docs/index.html |
6bff243 to
edd9340
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-21920/wasm-skia-net9/index.html |
|
|
edd9340 to
7420f57
Compare
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-21920/docs/index.html |
|
|
7420f57 to
de69bd8
Compare
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-21920/docs/index.html |
|
|
de69bd8 to
edae26e
Compare
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-21920/docs/index.html |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-21920/wasm-skia-net9/index.html |
Context: unoplatform/uno.chefs#1712 Context: #21920 Context: unoplatform/uno.extensions#2966 Context: unoplatform/uno.extensions#2969 Context: https://discord.com/channels/732297728826277939/732297837953679412/1444909702512119899 While running the uno.chefs app on macOS under NativeAOT: dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \ Chefs/Chefs.csproj \ -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \ -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \ -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs (along with a change from unoplatform/uno.chefs#1712) Console output would contain the following errors: fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [Y] property getter does not exist on type [Windows.Foundation.Point] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [X] property getter does not exist on type [Windows.Foundation.Point] At this point, a description of the relation between NativeAOT and Reflection would be useful. Parts of System.Reflection can be used. Which parts *can* be used, and how to use them, is oddly *not* well specified in the [Native AOT deployment][0] documentation. What doesn't work is outlined in the [limitations][1] section: > * No dynamic loading, for example, `Assembly.LoadFile`. > * No run-time code generation, for example, `System.Reflection.Emit`. What *always* appears to work is: * `Type.GetType()` returns a `Type` instance. What ***can*** work is: * `Type.GetType(string)`, *so long as* an assembly-qualified name is used as a *string constant* // bad string name = GetSomeTypeName(); Type type = Type.GetType(name); // good Type type = Type.GetType("My.Example.TypeName, ExampleAssembly"); * `Type.GetMethods()`, but see fine print below. * `Type.GetProperties()`, but see fine print below. * `MethodInfo.Invoke()` * … **The Fine Print**: in order for many Reflection-based APIs such as `Type.GetMethods()` to work, the program must contain "reflection metadata." We can now describe, briefly, what NativeAOT does: 1. Accept IL/assemblies 2. Internally trims the IL, optimizes them, etc. 3. Generates a native binary containing two separate bits of data: * Native machine code for execution * "reflection metadata" There is no facility to separately dump reflection metadata. Programs can control what is contained within reflection metadata by using string constants with Reflection APIs, for example: typeof(SomeKnownType).GetMethod("ConstantName") will ensure that reflection metadata contains `SomeKnownType.ConstantName()`, and `ConstantName()` will be returned from `typeof(SomeKnownType).GetMethods()` and can be invoked with `MethodInfo.Invoke()`. If you *don't* use `typeof(SomeKnownType)` or string constants with `Type.GetType(string)` or `type.GetMethod("constant")`, then you need to "suggest" that some pieces of information be added to reflection metadata. The two predominant ways to do this are via the custom attributes: * [`DynamicallyAccessedMembersAttribute`][2] * [`DynamicDependencyAttribute`][3] From the Uno perspective, enter `BindingPropertyHelper`: *everything* is based on Reflection! Fortunately NativeAOT "supports" Reflection! *Unfortunately*, we now need to convince NativeAOT to store the reflection metadata Uno requires in order to work! Various other PRs have dealt with some of this, such as #21920. Returning to the original failure messages, e.g. fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] During the app build, `src/SourceGenerators/Uno.UI.SourceGenerators` generates a a `BindableMetadata.g.cs` file which will mention `[Bindable]` types, e.g. for `Microsoft.UI.Xaml.Controls.FlipView`: /// <summary> /// Builder for Microsoft.UI.Xaml.Controls.FlipView /// </summary> [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute] static class MetadataBuilder_239 { internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent) { var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType(151, typeof(global::Microsoft.UI.Xaml.Controls.FlipView)); // … } private static object CreateInstance() => new global::Microsoft.UI.Xaml.Controls.FlipView(); } `BindableMetadata.g.cs` was needed in the ye olde Xamarin world order to tell *its* linker to e.g. "preserve the `FlipView` default ctor!" *Now*, we can build upon this infrastructure and update `BindableType` to request that Reflection information regarding properties, fields, and constructors be retained. We can do this by updating the `BindableType` constructor: partial class BindableType { internal const DynamicallyAccessedMemberTypes TypeRequirements = DynamicallyAccessedMemberTypes.PublicProperties | …; public BindableType(int estimatedPropertySize, [DynamicallyAccessedMembers(TypeRequirements)] Type sourceType ) => … } *Because* we have `BindableMetadata.g.cs` *and* because it provides `typeof(FlipView)`, this directs NativeAOT to make property and other reflection metadata available to the app, *fixing* the error messages. Expand this addition of `[DynamicallyAccessedMembers]` to also include `BindableProperty` and related types. [0]: https://learn.microsoft.comdotnet/core/deploying/native-aot [1]: https://learn.microsoft.comdotnet/core/deploying/native-aot/#limitations-of-native-aot-deployment [2]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-10.0 [3]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicdependencyattribute?view=net-10.0
|
The build 185991 found UI Test snapshots differences: Details
|
Context: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming Context: https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot#aot-compatibility-analyzers Context: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options Context: 10480ec Context: 408e177 Context: dotnet/android#7473 Context: dotnet/android@6836818 Enable `$(IsAotCompatible)`=true for the following projects: * `src/Uno.Foundation/Uno.Foundation.csproj` * `src/Uno.Foundation/Uno.Foundation.netcoremobile.csproj` * `src/Uno.Foundation/Uno.Foundation.Reference.csproj` * `src/Uno.Foundation/Uno.Foundation.Skia.csproj` * `src/Uno.Foundation/Uno.Foundation.Wasm.csproj` * `src/Uno.UI/Uno.UI.netcoremobile.csproj` * `src/Uno.UI/Uno.UI.Reference.csproj` * `src/Uno.UI/Uno.UI.Skia.csproj` * `src/Uno.UI/Uno.UI.Wasm.csproj` Address the following warnings: src/Uno.UI/DataBinding/BindingPropertyHelper.MemberBinder.cs(13,60): error IL3050: Using member 'System.Dynamic.GetMemberBinder.GetMemberBinder(String, Boolean)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Creating a call site may require dynamic code generation. src/Uno.UI/DataBinding/BindingPropertyHelper.MemberBinder.cs(22,60): error IL3050: Using member 'System.Dynamic.SetMemberBinder.SetMemberBinder(String, Boolean)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Creating a call site may require dynamic code generation. src/Uno.UI/FastInvokerBuilder.Android.cs(25,34): error IL3050: Using member 'System.Reflection.Emit.DynamicMethod.DynamicMethod(String, Type, Type[], Module, Boolean)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Creating a DynamicMethod requires dynamic code. Add `[RequiresDynamicCodeAttribute]` to the specified types. Fix the following warnings: src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2WebMessageReceivedEventArgs.cs(61,70): error IL2026: Using member 'System.Text.Json.Serialization.Metadata.JsonTypeInfo.CreateJsonTypeInfo<T>(JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2WebMessageReceivedEventArgs.cs(61,70): error IL3050: Using member 'System.Text.Json.Serialization.Metadata.JsonTypeInfo.CreateJsonTypeInfo<T>(JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2WebMessageReceivedEventArgs.cs(54,22): error IL2026: Using member 'System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.DefaultJsonTypeInfoResolver()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2WebMessageReceivedEventArgs.cs(54,22): error IL3050: Using member 'System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.DefaultJsonTypeInfoResolver()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. @jonpryor was under the mistaken belief that commit 10480ec would be sufficient to fix these warnings, and was wrong. Even though we "only" need to turn JSON into a *`string`*, not a structure, do as requested: use System.Text.Json source generation. (Which, again, feels rather weird as only `string` is needed!) [JsonSourceGenerationOptions] [JsonSerializable(typeof(string))] internal sealed partial class ExampleContext : JsonSerializerContext {} // then use ExampleContext.Default.String to deserialize to string! Fix the following warning: src/Uno.UI/UI/Xaml/SpecializedResourceDictionary.cs(133,34): error IL3050: Using member 'System.Runtime.InteropServices.Marshal.SizeOf(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Marshalling code for the object might not be available. Use the SizeOf<T> overload instead. We *could* do as requested and use `Marshal.SizeOf<IntPtr>()`, but the reason why `Marshal.SizeOf()` is used here is because of 408e177, which was to workaround dotnet/android#7473. dotnet/android#7473 has since been fixed in dotnet/android@68368189, meaning we don't actually need to use `Marshal.SizeOf()` anymore. Use `IntPtr.Size` instead. Suppress the following warning: src/Uno.UI/UI/Xaml/Navigation/NavigationHistory.mux.cs(329,25): error IL2057: Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String)'. It's not possible to guarantee the availability of the target type. TODO: investigate whether we can update `PageStackEntry.m_descriptor` to have `[DynamicallyAccessedMembers]`. Suppress the following warnings: src/Uno.UI/Extensions/ViewExtensions.visual-tree.cs(214,4): error IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String, BindingFlags)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/FrameworkElementExtensions.cs(82,17): error IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Reflection.TypeInfo.GetDeclaredField(String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/FrameworkElementExtensions.cs(82,17): error IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Reflection.TypeInfo.GetDeclaredField(String)'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/FrameworkElementExtensions.cs(108,20): error IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.TypeInfo.GetDeclaredProperty(String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/FrameworkElementExtensions.cs(108,20): error IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.TypeInfo.GetDeclaredProperty(String)'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/FrameworkElementExtensions.cs(104,8): error IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Reflection.TypeInfo.GetDeclaredField(String)'. The parameter 'type' of method 'GetField(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/FrameworkElementExtensions.cs(134,8): error IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.TypeInfo.GetDeclaredProperty(String)'. The parameter 'type' of method 'GetProperty(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/DirectUI/PropertyPathStep.cs(502,16): error IL2072: 'pSourceType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'DirectUI.ReflectionPropertyAccess.CreateInstance(IPropertyAccessHost, Object, Type, Boolean)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/Controls/Frame/Frame.legacy.cs(68,4): error IL2072: 'value' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'Microsoft.UI.Xaml.Navigation.PageStackEntry.SourcePageType.set'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/Controls/NumberBox/NumberBoxParser.cs(93,20): error IL2072: 'typeName' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.PublicEvents' in call to 'Windows.Foundation.Metadata.ApiInformation.IsTypePresent(String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. As these callsites are all rooted in `value.GetType().…`, I can't think of a way to appease NativeAOT here. Suppress the following warnings: C:\a\1\s\src\Uno.UI\obj\Uno.UI.netcoremobile\Release\net10.0-ios26.0\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.DependencyObject.DependencyObjectGenerator\Microsoft.UI.Xaml.Navigation.PageStackEntry.cs(120,4): error IL2111: Method 'Microsoft.UI.Xaml.Navigation.PageStackEntry.PageStackEntry(Type, Object, NavigationTransitionInfo)' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method. C:\a\1\s\src\Uno.UI\obj\Uno.UI.netcoremobile\Release\net10.0-ios26.0\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.DependencyObject.DependencyObjectGenerator\Microsoft.UI.Xaml.Navigation.PageStackEntry.cs(150,4): error IL2111: Method 'Microsoft.UI.Xaml.Navigation.PageStackEntry.PageStackEntry(Type, Object, NavigationTransitionInfo)' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method. These warnings appear to be unactionable: what are we supposed to *do*? Like, okay then? Update `src/SourceGenerators` so that the callsite suppressses these. Address the following warnings: src/Uno.UI/Controls/BindableImageView.Android.cs(205,23): error IL2080: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The field 'Uno.UI.Controls.BindableImageView._drawables' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/DirectUI/PropertyAccess.cs(1634,18): error IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String, BindingFlags)'. The parameter 'pSourceType' of method 'DirectUI.ReflectionPropertyAccess.CreateInstance(IPropertyAccessHost, Object, Type, Boolean)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/DirectUI/MetadataAPI.cs(603,5): error IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Type.GetProperty(String, BindingFlags)'. The parameter 'type' of method 'DirectUI.MetadataAPI.TryGetDependencyPropertyByName(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/Internal/CustomEventManager.cs(33,66): error IL2091: 'TValue' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'System.Runtime.CompilerServices.ConditionalWeakTable<TKey, TValue>'. The generic parameter 'TEventArgs' of 'Uno.UI.Xaml.Core.CustomKeepLastEventManager<T, TEventArgs>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/Navigation/PageStackEntry.Properties.cs(32,10): error IL2073: 'Microsoft.UI.Xaml.Navigation.PageStackEntry.SourcePageType.get' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' requirements. The return value of method 'Microsoft.UI.Xaml.Navigation.PageStackEntry.GetValue(DependencyProperty)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. Add the requested `[DynamicallyAccessedMembers]`. Suppress the following warning: src/Uno.UI/UI/Xaml/Navigation/PageStackEntry.Properties.cs(41,3): error IL2111: Method 'Microsoft.UI.Xaml.Navigation.PageStackEntry.PageStackEntry(Type, Object, NavigationTransitionInfo)' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method. @jonpryor has no idea why this warning is appearing. Address the following warning: src/Uno.UI/Helpers/TypeMappings.cs(76,6): error IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Activator.CreateInstance(Type, params Object[])'. The return value of method 'Uno.UI.Helpers.TypeMappings.GetReplacementType(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. Update *the entire call hierarchy* within this file so that `Type` parameters and `Type`-returning methods all declare *both* `DynamicallyAccessedMemberTypes.PublicConstructors` and `DynamicallyAccessedMemberTypes.PublicParameterlessConstructor`. The latter is needed for `TypeMappings.CreateInstance<T>()`. For the following warnings: src/Uno.UI/UI/Xaml/Markup/Reader/XamlTypeResolver.cs(463,23): error IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI/UI/Xaml/Markup/Reader/XamlTypeResolver.cs(474,23): error IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethods()'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. Annotate the `type` parameter with `[DynamicallyAccessedMembers]`. (This doesn't touch `Type.BaseType`, but fixed the IL2075.) Unfortunately, after addressing all of the above, *non*-trimmer enabled builds start failing: src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs(54,29): error IL2072: 'mappedType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'Uno.UI.Helpers.TypeMappings.GetOriginalType(Type)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs(85,30): error IL2072: 'mappedType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'Uno.UI.Helpers.TypeMappings.GetOriginalType(Type)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.MetadataUpdate.cs(526,6): error IL2072: 'mappedType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'Uno.UI.Helpers.TypeMappings.RegisterMapping(Type, Type)'. The return value of method 'System.Collections.IEnumerator.Current.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.MetadataUpdate.cs(526,6): error IL2072: 'originalType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'Uno.UI.Helpers.TypeMappings.RegisterMapping(Type, Type)'. The return value of method 'System.Reflection.CustomAttributeTypedArgument.Value.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.MetadataUpdate.cs(164,25): error IL2072: 'mappedType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'Uno.UI.Helpers.TypeMappings.GetOriginalType(Type)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.MetadataUpdate.cs(180,23): error IL2072: 'originalType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'Uno.UI.Helpers.TypeMappings.GetMappedType(Type)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. Suppress these warnings. Fix and suppress the following warning: src/Uno.UI/ToolkitHelper.cs(12,20): error IL2057: Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String)'. It's not possible to guarantee the availability of the target type. Fix this by updating all callsites to use an assembly-qualified name instead of relying on `ToolkitHelper.GetProperty()` to do a string concat before a `Type.GetType()` invocation. This along with `[DynamicallyAccessedMembers]` on the `typeName` parameter *should* allow NativeAOT to preserve the referenced types. That doesn't actually remove the IL2057, though, so suppress it. For compatibility, if the initial `Type.GetType()` fails, fallback to appending `",Uno.UI.Toolkit"` to the type name.
edae26e to
6e631b0
Compare
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-21920/docs/index.html |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-21920/wasm-skia-net9/index.html |
Context: unoplatform/uno.chefs#1712 Context: #21920 Context: unoplatform/uno.extensions#2966 Context: unoplatform/uno.extensions#2969 Context: https://discord.com/channels/732297728826277939/732297837953679412/1444909702512119899 While running the uno.chefs app on macOS under NativeAOT: dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \ Chefs/Chefs.csproj \ -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \ -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \ -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs (along with a change from unoplatform/uno.chefs#1712) Console output would contain the following errors: fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [Y] property getter does not exist on type [Windows.Foundation.Point] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [X] property getter does not exist on type [Windows.Foundation.Point] At this point, a description of the relation between NativeAOT and Reflection would be useful. Parts of System.Reflection can be used. Which parts *can* be used, and how to use them, is oddly *not* well specified in the [Native AOT deployment][0] documentation. What doesn't work is outlined in the [limitations][1] section: > * No dynamic loading, for example, `Assembly.LoadFile`. > * No run-time code generation, for example, `System.Reflection.Emit`. What *always* appears to work is: * `Type.GetType()` returns a `Type` instance. What ***can*** work is: * `Type.GetType(string)`, *so long as* an assembly-qualified name is used as a *string constant* // bad string name = GetSomeTypeName(); Type type = Type.GetType(name); // good Type type = Type.GetType("My.Example.TypeName, ExampleAssembly"); * `Type.GetMethods()`, but see fine print below. * `Type.GetProperties()`, but see fine print below. * `MethodInfo.Invoke()` * … **The Fine Print**: in order for many Reflection-based APIs such as `Type.GetMethods()` to work, the program must contain "reflection metadata." We can now describe, briefly, what NativeAOT does: 1. Accept IL/assemblies 2. Internally trims the IL, optimizes them, etc. 3. Generates a native binary containing two separate bits of data: * Native machine code for execution * "reflection metadata" There is no facility to separately dump reflection metadata. Programs can control what is contained within reflection metadata by using string constants with Reflection APIs, for example: typeof(SomeKnownType).GetMethod("ConstantName") will ensure that reflection metadata contains `SomeKnownType.ConstantName()`, and `ConstantName()` will be returned from `typeof(SomeKnownType).GetMethods()` and can be invoked with `MethodInfo.Invoke()`. If you *don't* use `typeof(SomeKnownType)` or string constants with `Type.GetType(string)` or `type.GetMethod("constant")`, then you need to "suggest" that some pieces of information be added to reflection metadata. The two predominant ways to do this are via the custom attributes: * [`DynamicallyAccessedMembersAttribute`][2] * [`DynamicDependencyAttribute`][3] From the Uno perspective, enter `BindingPropertyHelper`: *everything* is based on Reflection! Fortunately NativeAOT "supports" Reflection! *Unfortunately*, we now need to convince NativeAOT to store the reflection metadata Uno requires in order to work! Various other PRs have dealt with some of this, such as #21920. Returning to the original failure messages, e.g. fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] During the app build, `src/SourceGenerators/Uno.UI.SourceGenerators` generates a a `BindableMetadata.g.cs` file which will mention `[Bindable]` types, e.g. for `Microsoft.UI.Xaml.Controls.FlipView`: /// <summary> /// Builder for Microsoft.UI.Xaml.Controls.FlipView /// </summary> [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute] static class MetadataBuilder_239 { internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent) { var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType(151, typeof(global::Microsoft.UI.Xaml.Controls.FlipView)); // … } private static object CreateInstance() => new global::Microsoft.UI.Xaml.Controls.FlipView(); } `BindableMetadata.g.cs` was needed in the ye olde Xamarin world order to tell *its* linker to e.g. "preserve the `FlipView` default ctor!" *Now*, we can build upon this infrastructure and update `BindableType` to request that Reflection information regarding properties, fields, and constructors be retained. We can do this by updating the `BindableType` constructor: partial class BindableType { internal const DynamicallyAccessedMemberTypes TypeRequirements = DynamicallyAccessedMemberTypes.PublicProperties | …; public BindableType(int estimatedPropertySize, [DynamicallyAccessedMembers(TypeRequirements)] Type sourceType ) => … } *Because* we have `BindableMetadata.g.cs` *and* because it provides `typeof(FlipView)`, this directs NativeAOT to make property and other reflection metadata available to the app, *fixing* the error messages. Expand this addition of `[DynamicallyAccessedMembers]` to also include `BindableProperty` and related types. [0]: https://learn.microsoft.comdotnet/core/deploying/native-aot [1]: https://learn.microsoft.comdotnet/core/deploying/native-aot/#limitations-of-native-aot-deployment [2]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-10.0 [3]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicdependencyattribute?view=net-10.0
Context: unoplatform/uno.chefs#1712 Context: #21920 Context: unoplatform/uno.extensions#2966 Context: unoplatform/uno.extensions#2969 Context: https://discord.com/channels/732297728826277939/732297837953679412/1444909702512119899 While running the uno.chefs app on macOS under NativeAOT: dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \ Chefs/Chefs.csproj \ -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \ -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \ -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs (along with a change from unoplatform/uno.chefs#1712) Console output would contain the following errors: fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [Y] property getter does not exist on type [Windows.Foundation.Point] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [X] property getter does not exist on type [Windows.Foundation.Point] At this point, a description of the relation between NativeAOT and Reflection would be useful. Parts of System.Reflection can be used. Which parts *can* be used, and how to use them, is oddly *not* well specified in the [Native AOT deployment][0] documentation. What doesn't work is outlined in the [limitations][1] section: > * No dynamic loading, for example, `Assembly.LoadFile`. > * No run-time code generation, for example, `System.Reflection.Emit`. What *always* appears to work is: * `Type.GetType()` returns a `Type` instance. What ***can*** work is: * `Type.GetType(string)`, *so long as* an assembly-qualified name is used as a *string constant* // bad string name = GetSomeTypeName(); Type type = Type.GetType(name); // good Type type = Type.GetType("My.Example.TypeName, ExampleAssembly"); * `Type.GetMethods()`, but see fine print below. * `Type.GetProperties()`, but see fine print below. * `MethodInfo.Invoke()` * … **The Fine Print**: in order for many Reflection-based APIs such as `Type.GetMethods()` to work, the program must contain "reflection metadata." We can now describe, briefly, what NativeAOT does: 1. Accept IL/assemblies 2. Internally trims the IL, optimizes them, etc. 3. Generates a native binary containing two separate bits of data: * Native machine code for execution * "reflection metadata" There is no facility to separately dump reflection metadata. Programs can control what is contained within reflection metadata by using string constants with Reflection APIs, for example: typeof(SomeKnownType).GetMethod("ConstantName") will ensure that reflection metadata contains `SomeKnownType.ConstantName()`, and `ConstantName()` will be returned from `typeof(SomeKnownType).GetMethods()` and can be invoked with `MethodInfo.Invoke()`. If you *don't* use `typeof(SomeKnownType)` or string constants with `Type.GetType(string)` or `type.GetMethod("constant")`, then you need to "suggest" that some pieces of information be added to reflection metadata. The two predominant ways to do this are via the custom attributes: * [`DynamicallyAccessedMembersAttribute`][2] * [`DynamicDependencyAttribute`][3] From the Uno perspective, enter `BindingPropertyHelper`: *everything* is based on Reflection! Fortunately NativeAOT "supports" Reflection! *Unfortunately*, we now need to convince NativeAOT to store the reflection metadata Uno requires in order to work! Various other PRs have dealt with some of this, such as #21920. Returning to the original failure messages, e.g. fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] During the app build, `src/SourceGenerators/Uno.UI.SourceGenerators` generates a a `BindableMetadata.g.cs` file which will mention `[Bindable]` types, e.g. for `Microsoft.UI.Xaml.Controls.FlipView`: /// <summary> /// Builder for Microsoft.UI.Xaml.Controls.FlipView /// </summary> [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute] static class MetadataBuilder_239 { internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent) { var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType(151, typeof(global::Microsoft.UI.Xaml.Controls.FlipView)); // … } private static object CreateInstance() => new global::Microsoft.UI.Xaml.Controls.FlipView(); } `BindableMetadata.g.cs` was needed in the ye olde Xamarin world order to tell *its* linker to e.g. "preserve the `FlipView` default ctor!" *Now*, we can build upon this infrastructure and update `BindableType` to request that Reflection information regarding properties, fields, and constructors be retained. We can do this by updating the `BindableType` constructor: partial class BindableType { internal const DynamicallyAccessedMemberTypes TypeRequirements = DynamicallyAccessedMemberTypes.PublicProperties | …; public BindableType(int estimatedPropertySize, [DynamicallyAccessedMembers(TypeRequirements)] Type sourceType ) => … } *Because* we have `BindableMetadata.g.cs` *and* because it provides `typeof(FlipView)`, this directs NativeAOT to make property and other reflection metadata available to the app, *fixing* the error messages. Expand this addition of `[DynamicallyAccessedMembers]` to also include `BindableProperty` and related types. [0]: https://learn.microsoft.comdotnet/core/deploying/native-aot [1]: https://learn.microsoft.comdotnet/core/deploying/native-aot/#limitations-of-native-aot-deployment [2]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-10.0 [3]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicdependencyattribute?view=net-10.0
|
The build 186137 found UI Test snapshots differences: Details
|
Context: unoplatform/uno.chefs#1712 Context: #21920 Context: unoplatform/uno.extensions#2966 Context: unoplatform/uno.extensions#2969 Context: https://discord.com/channels/732297728826277939/732297837953679412/1444909702512119899 While running the uno.chefs app on macOS under NativeAOT: dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \ Chefs/Chefs.csproj \ -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \ -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \ -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs Console output would contain the following errors: fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [Y] property getter does not exist on type [Windows.Foundation.Point] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [X] property getter does not exist on type [Windows.Foundation.Point] At this point, a description of the relation between NativeAOT and Reflection would be useful. *Parts* of System.Reflection can be used. Which parts *can* be used, and how to use them, is oddly *not* well specified in the [Native AOT deployment][0] documentation. What doesn't work is outlined in the [limitations][1] section: > * No dynamic loading, for example, `Assembly.LoadFile`. > * No run-time code generation, for example, `System.Reflection.Emit`. What *always* appears to work is: * `Type.GetType()` returns a `Type` instance. What ***can*** work is: * `Type.GetType(string)`, *so long as* an assembly-qualified name is used as a *string constant* // bad string name = GetSomeTypeName(); Type type = Type.GetType(name); // good Type type = Type.GetType("My.Example.TypeName, ExampleAssembly"); * `Type.GetMethod(string)`, but see fine print below. * `Type.GetProperty(string)`, but see fine print below. * `MethodInfo.Invoke()` * … **The Fine Print**: in order for many Reflection-based APIs such as `Type.GetMethod(string)` to work, the program must contain "reflection metadata." We can now describe, briefly, what NativeAOT does: 1. Accept IL/assemblies as input 2. Internally trim the IL, optimizes them, etc. 3. Generates a native binary containing two separate bits of data: * Native machine code for execution * "reflection metadata" You can get an inkling of what methods survived trimming and become native code by using `dotnet publish -p:IlcGenerateMapFile=true …`, which will produce a `.mstat` file, a PE file which references every type and member which is in the native binary, ***after*** inlining. (Meaning if a member is inlined, it *won't* be present!) Use e.g. `monodis --memberref App.mstat` to list post-trimmed members. You can get an inkling of the "reflection metadata" that is present by using `dotnet publish -p:IlcGenerateMetadataLog=true …`, which produces a `.metadata.csv` file. It's not particularly scrutible, but can be used to verify observed behavior. Consider one of the above failure messages: The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] An obvious question to ask: is the `TabNavigation` property in the reflection metadata? Search for `"TabNavigation"` (with quotes!), and it's *not* in there. Compare to when this commit is in use, and: 66087019, Property, "TabNavigation", "3410be9a 6810bea8 5410beae 5410beb2" 3410be9a, ConstantStringValue, "TabNavigation", "" The 3rd column is the property name `TabNavigation`, while the 2nd column is the type; for properties, we want `Property`. The 4th column is "Children", one of which is: 5410beae, MethodSemantics, "Getter : [HasThis] Microsoft.UI.Xaml.Input.KeyboardNavigationMode get_TabNavigation()", "50086c65" which at least verifies that `TabNavigation` is in play. Programs can control what is contained within reflection metadata by using string constants with Reflection APIs, for example: typeof(SomeKnownType).GetMethod("ConstantName") will ensure that reflection metadata contains `SomeKnownType.ConstantName()`, and `ConstantName()` will be returned from `typeof(SomeKnownType).GetMethods()` and can be invoked with `MethodInfo.Invoke()`. If you *don't* use `typeof(SomeKnownType)` or string constants with `Type.GetType(string)` or `type.GetMethod("constant")`, then you need to "suggest" that some pieces of information be added to reflection metadata. The two predominant ways to do this are via the custom attributes: * [`DynamicallyAccessedMembersAttribute`][2] * [`DynamicDependencyAttribute`][3] From the Uno perspective, enter `BindingPropertyHelper`: *everything* is based on Reflection! Fortunately NativeAOT "supports" Reflection! (See above.) *Unfortunately*, we now need to convince NativeAOT to store the reflection metadata Uno requires in order to work! Various other PRs have dealt with some of this, such as #21920. Returning to the original failure messages, e.g. fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] During the app build, `src/SourceGenerators/Uno.UI.SourceGenerators` generates a `BindableMetadata.g.cs` file which will mention `[Bindable]` types, e.g. for `Microsoft.UI.Xaml.Controls.FlipView`: /// <summary> /// Builder for Microsoft.UI.Xaml.Controls.FlipView /// </summary> [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute] static class MetadataBuilder_239 { internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent) { var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType(151, typeof(global::Microsoft.UI.Xaml.Controls.FlipView)); // … } private static object CreateInstance() => new global::Microsoft.UI.Xaml.Controls.FlipView(); } `BindableMetadata.g.cs` was needed in the ye olde Xamarin world order to tell *its* linker to e.g. "preserve the `FlipView` default ctor!" *Now*, we can build upon this infrastructure and update `BindableType` to request that Reflection information regarding properties, fields, and constructors be retained. We can do this by updating the `BindableType` constructor: partial class BindableType { internal const DynamicallyAccessedMemberTypes TypeRequirements = DynamicallyAccessedMemberTypes.PublicProperties | …; public BindableType(int estimatedPropertySize, [DynamicallyAccessedMembers(TypeRequirements)] Type sourceType) => … } *Because* we have `BindableMetadata.g.cs` *and* because it provides `typeof(FlipView)`, this directs NativeAOT to make property and other reflection metadata available to the app, *fixing* the error messages. Expand this addition of `[DynamicallyAccessedMembers]` to also include `BindableProperty` and related types. Note: the IL2111 warning "Method 'System.Type.TypeInitializer.get' …" is *caused by* `typeof(Type)` (?!), which is particularly odd given that we don't use `Type.TypeInitializer`! Thanks to Alexander Köplinger for [looking into this][4]. [0]: https://learn.microsoft.comdotnet/core/deploying/native-aot [1]: https://learn.microsoft.comdotnet/core/deploying/native-aot/#limitations-of-native-aot-deployment [2]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-10.0 [3]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicdependencyattribute?view=net-10.0 [4]: https://discord.com/channels/732297728826277939/732297837953679412/1445773677152174150
Context: unoplatform/uno.chefs#1712 Context: #21920 Context: unoplatform/uno.extensions#2966 Context: unoplatform/uno.extensions#2969 Context: https://discord.com/channels/732297728826277939/732297837953679412/1444909702512119899 While running the uno.chefs app on macOS under NativeAOT: dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \ Chefs/Chefs.csproj \ -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \ -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \ -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs Console output would contain the following errors: fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [Y] property getter does not exist on type [Windows.Foundation.Point] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [X] property getter does not exist on type [Windows.Foundation.Point] At this point, a description of the relation between NativeAOT and Reflection would be useful. *Parts* of System.Reflection can be used. Which parts *can* be used, and how to use them, is oddly *not* well specified in the [Native AOT deployment][0] documentation. What doesn't work is outlined in the [limitations][1] section: > * No dynamic loading, for example, `Assembly.LoadFile`. > * No run-time code generation, for example, `System.Reflection.Emit`. What *always* appears to work is: * `Type.GetType()` returns a `Type` instance. What ***can*** work is: * `Type.GetType(string)`, *so long as* an assembly-qualified name is used as a *string constant* // bad string name = GetSomeTypeName(); Type type = Type.GetType(name); // good Type type = Type.GetType("My.Example.TypeName, ExampleAssembly"); * `Type.GetMethod(string)`, but see fine print below. * `Type.GetProperty(string)`, but see fine print below. * `MethodInfo.Invoke()` * … **The Fine Print**: in order for many Reflection-based APIs such as `Type.GetMethod(string)` to work, the program must contain "reflection metadata." We can now describe, briefly, what NativeAOT does: 1. Accept IL/assemblies as input 2. Internally trim the IL, optimizes them, etc. 3. Generates a native binary containing two separate bits of data: * Native machine code for execution * "reflection metadata" You can get an inkling of what methods survived trimming and become native code by using `dotnet publish -p:IlcGenerateMapFile=true …`, which will produce a `.mstat` file, a PE file which references every type and member which is in the native binary, ***after*** inlining. (Meaning if a member is inlined, it *won't* be present!) Use e.g. `monodis --memberref App.mstat` to list post-trimmed members. You can get an inkling of the "reflection metadata" that is present by using `dotnet publish -p:IlcGenerateMetadataLog=true …`, which produces a `.metadata.csv` file. It's not particularly scrutible, but can be used to verify observed behavior. Consider one of the above failure messages: The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] An obvious question to ask: is the `TabNavigation` property in the reflection metadata? Search for `"TabNavigation"` (with quotes!), and it's *not* in there. Compare to when this commit is in use, and: 66087019, Property, "TabNavigation", "3410be9a 6810bea8 5410beae 5410beb2" 3410be9a, ConstantStringValue, "TabNavigation", "" The 3rd column is the property name `TabNavigation`, while the 2nd column is the type; for properties, we want `Property`. The 4th column is "Children", one of which is: 5410beae, MethodSemantics, "Getter : [HasThis] Microsoft.UI.Xaml.Input.KeyboardNavigationMode get_TabNavigation()", "50086c65" which at least verifies that `TabNavigation` is in play. Programs can control what is contained within reflection metadata by using string constants with Reflection APIs, for example: typeof(SomeKnownType).GetMethod("ConstantName") will ensure that reflection metadata contains `SomeKnownType.ConstantName()`, and `ConstantName()` will be returned from `typeof(SomeKnownType).GetMethods()` and can be invoked with `MethodInfo.Invoke()`. If you *don't* use `typeof(SomeKnownType)` or string constants with `Type.GetType(string)` or `type.GetMethod("constant")`, then you need to "suggest" that some pieces of information be added to reflection metadata. The two predominant ways to do this are via the custom attributes: * [`DynamicallyAccessedMembersAttribute`][2] * [`DynamicDependencyAttribute`][3] From the Uno perspective, enter `BindingPropertyHelper`: *everything* is based on Reflection! Fortunately NativeAOT "supports" Reflection! (See above.) *Unfortunately*, we now need to convince NativeAOT to store the reflection metadata Uno requires in order to work! Various other PRs have dealt with some of this, such as #21920. Returning to the original failure messages, e.g. fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] During the app build, `src/SourceGenerators/Uno.UI.SourceGenerators` generates a `BindableMetadata.g.cs` file which will mention `[Bindable]` types, e.g. for `Microsoft.UI.Xaml.Controls.FlipView`: /// <summary> /// Builder for Microsoft.UI.Xaml.Controls.FlipView /// </summary> [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute] static class MetadataBuilder_239 { internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent) { var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType(151, typeof(global::Microsoft.UI.Xaml.Controls.FlipView)); // … } private static object CreateInstance() => new global::Microsoft.UI.Xaml.Controls.FlipView(); } `BindableMetadata.g.cs` was needed in the ye olde Xamarin world order to tell *its* linker to e.g. "preserve the `FlipView` default ctor!" *Now*, we can build upon this infrastructure and update `BindableType` to request that Reflection information regarding properties, fields, and constructors be retained. We can do this by updating the `BindableType` constructor: partial class BindableType { internal const DynamicallyAccessedMemberTypes TypeRequirements = DynamicallyAccessedMemberTypes.PublicProperties | …; public BindableType(int estimatedPropertySize, [DynamicallyAccessedMembers(TypeRequirements)] Type sourceType) => … } *Because* we have `BindableMetadata.g.cs` *and* because it provides `typeof(FlipView)`, this directs NativeAOT to make property and other reflection metadata available to the app, *fixing* the error messages. Expand this addition of `[DynamicallyAccessedMembers]` to also include `BindableProperty` and related types. Note: the IL2111 warning "Method 'System.Type.TypeInitializer.get' …" is *caused by* `typeof(Type)` (?!), which is particularly odd given that we don't use `Type.TypeInitializer`! Thanks to Alexander Köplinger for [looking into this][4]. [0]: https://learn.microsoft.comdotnet/core/deploying/native-aot [1]: https://learn.microsoft.comdotnet/core/deploying/native-aot/#limitations-of-native-aot-deployment [2]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-10.0 [3]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicdependencyattribute?view=net-10.0 [4]: https://discord.com/channels/732297728826277939/732297837953679412/1445773677152174150
Context: unoplatform/uno.chefs#1712 Context: #21920 Context: unoplatform/uno.extensions#2966 Context: unoplatform/uno.extensions#2969 Context: https://discord.com/channels/732297728826277939/732297837953679412/1444909702512119899 While running the uno.chefs app on macOS under NativeAOT: dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \ Chefs/Chefs.csproj \ -p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \ -p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \ -p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs Console output would contain the following errors: fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [Y] property getter does not exist on type [Windows.Foundation.Point] fail: Uno.UI.Dispatching.NativeDispatcher[0] The [X] property getter does not exist on type [Windows.Foundation.Point] At this point, a description of the relation between NativeAOT and Reflection would be useful. *Parts* of System.Reflection can be used. Which parts *can* be used, and how to use them, is oddly *not* well specified in the [Native AOT deployment][0] documentation. What doesn't work is outlined in the [limitations][1] section: > * No dynamic loading, for example, `Assembly.LoadFile`. > * No run-time code generation, for example, `System.Reflection.Emit`. What *always* appears to work is: * `Type.GetType()` returns a `Type` instance. What ***can*** work is: * `Type.GetType(string)`, *so long as* an assembly-qualified name is used as a *string constant* // bad string name = GetSomeTypeName(); Type type = Type.GetType(name); // good Type type = Type.GetType("My.Example.TypeName, ExampleAssembly"); * `Type.GetMethod(string)`, but see fine print below. * `Type.GetProperty(string)`, but see fine print below. * `MethodInfo.Invoke()` * … **The Fine Print**: in order for many Reflection-based APIs such as `Type.GetMethod(string)` to work, the program must contain "reflection metadata." We can now describe, briefly, what NativeAOT does: 1. Accept IL/assemblies as input 2. Internally trim the IL, optimizes them, etc. 3. Generates a native binary containing two separate bits of data: * Native machine code for execution * "reflection metadata" You can get an inkling of what methods survived trimming and become native code by using `dotnet publish -p:IlcGenerateMstatFile=true …`, which will produce a `.mstat` file, a PE file which references every type and member which is in the native binary, ***after*** inlining. (Meaning if a member is inlined, it *won't* be present!) Use e.g. `monodis --memberref App.mstat` to list post-trimmed members. You can get an inkling of the "reflection metadata" that is present by using `dotnet publish -p:IlcGenerateMetadataLog=true …`, which produces a `.metadata.csv` file. It's not particularly scrutible, but can be used to verify observed behavior. Consider one of the above failure messages: The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] An obvious question to ask: is the `TabNavigation` property in the reflection metadata? Search for `"TabNavigation"` (with quotes!), and it's *not* in there. Compare to when this commit is in use, and: 66087019, Property, "TabNavigation", "3410be9a 6810bea8 5410beae 5410beb2" 3410be9a, ConstantStringValue, "TabNavigation", "" The 3rd column is the property name `TabNavigation`, while the 2nd column is the type; for properties, we want `Property`. The 4th column is "Children", one of which is: 5410beae, MethodSemantics, "Getter : [HasThis] Microsoft.UI.Xaml.Input.KeyboardNavigationMode get_TabNavigation()", "50086c65" which at least verifies that `TabNavigation` is in play. Programs can control what is contained within reflection metadata by using string constants with Reflection APIs, for example: typeof(SomeKnownType).GetMethod("ConstantName") will ensure that reflection metadata contains `SomeKnownType.ConstantName()`, and `ConstantName()` will be returned from `typeof(SomeKnownType).GetMethods()` and can be invoked with `MethodInfo.Invoke()`. If you *don't* use `typeof(SomeKnownType)` or string constants with `Type.GetType(string)` or `type.GetMethod("constant")`, then you need to "suggest" that some pieces of information be added to reflection metadata. The two predominant ways to do this are via the custom attributes: * [`DynamicallyAccessedMembersAttribute`][2] * [`DynamicDependencyAttribute`][3] From the Uno perspective, enter `BindingPropertyHelper`: *everything* is based on Reflection! Fortunately NativeAOT "supports" Reflection! (See above.) *Unfortunately*, we now need to convince NativeAOT to store the reflection metadata Uno requires in order to work! Various other PRs have dealt with some of this, such as #21920. Returning to the original failure messages, e.g. fail: Uno.UI.Dispatching.NativeDispatcher[0] The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView] During the app build, `src/SourceGenerators/Uno.UI.SourceGenerators` generates a `BindableMetadata.g.cs` file which will mention `[Bindable]` types, e.g. for `Microsoft.UI.Xaml.Controls.FlipView`: /// <summary> /// Builder for Microsoft.UI.Xaml.Controls.FlipView /// </summary> [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute] static class MetadataBuilder_239 { internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent) { var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType(151, typeof(global::Microsoft.UI.Xaml.Controls.FlipView)); // … } private static object CreateInstance() => new global::Microsoft.UI.Xaml.Controls.FlipView(); } `BindableMetadata.g.cs` was needed in the ye olde Xamarin world order to tell *its* linker to e.g. "preserve the `FlipView` default ctor!" *Now*, we can build upon this infrastructure and update `BindableType` to request that Reflection information regarding properties, fields, and constructors be retained. We can do this by updating the `BindableType` constructor: partial class BindableType { internal const DynamicallyAccessedMemberTypes TypeRequirements = DynamicallyAccessedMemberTypes.PublicProperties | …; public BindableType(int estimatedPropertySize, [DynamicallyAccessedMembers(TypeRequirements)] Type sourceType) => … } *Because* we have `BindableMetadata.g.cs` *and* because it provides `typeof(FlipView)`, this directs NativeAOT to make property and other reflection metadata available to the app, *fixing* the error messages. Expand this addition of `[DynamicallyAccessedMembers]` to also include `BindableProperty` and related types. Note: the IL2111 warning "Method 'System.Type.TypeInitializer.get' …" is *caused by* `typeof(Type)` (?!), which is particularly odd given that we don't use `Type.TypeInitializer`! Thanks to Alexander Köplinger for [looking into this][4]. [0]: https://learn.microsoft.comdotnet/core/deploying/native-aot [1]: https://learn.microsoft.comdotnet/core/deploying/native-aot/#limitations-of-native-aot-deployment [2]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-10.0 [3]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicdependencyattribute?view=net-10.0 [4]: https://discord.com/channels/732297728826277939/732297837953679412/1445773677152174150
Context: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
Context: https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot#aot-compatibility-analyzers
Context: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options
Context: 10480ec
Context: 408e177
Context: dotnet/android#7473
Context: dotnet/android@6836818
Enable
$(IsAotCompatible)=true for the following projects:src/Uno.Foundation/Uno.Foundation.csprojsrc/Uno.Foundation/Uno.Foundation.netcoremobile.csprojsrc/Uno.Foundation/Uno.Foundation.Reference.csprojsrc/Uno.Foundation/Uno.Foundation.Skia.csprojsrc/Uno.Foundation/Uno.Foundation.Wasm.csprojsrc/Uno.UI/Uno.UI.Skia.csprojAddress the following warnings:
Add
[RequiresDynamicCodeAttribute]to the specified types.Fix the following warnings:
@jonpryor was under the mistaken belief that commit 10480ec would be
sufficient to fix these warnings, and was wrong. Even though we
"only" need to turn JSON into a
string, not a structure, do asrequested: use System.Text.Json source generation. (Which, again,
feels rather weird as only
stringis needed!)Fix the following warning:
We could do as requested and use
Marshal.SizeOf<IntPtr>(), butthe reason why
Marshal.SizeOf()is used here is because of 408e177,which was to workaround dotnet/android#7473. dotnet/android#7473 has
since been fixed in dotnet/android@68368189, meaning we don't actually
need to use
Marshal.SizeOf()anymore. UseIntPtr.Sizeinstead.Suppress the following warning:
TODO: investigate whether we can update
PageStackEntry.m_descriptorto have
[DynamicallyAccessedMembers].Suppress the following warnings:
As these callsites are all rooted in
value.GetType().…, I can'tthink of a way to appease NativeAOT here.
Suppress the following warnings:
These warnings appear to be unactionable: what are we supposed to do?
Like, okay then?
Update
src/SourceGeneratorsso that the callsite suppressses these.Address the following warnings:
Add the requested
[DynamicallyAccessedMembers].Suppress the following warning:
@jonpryor has no idea why this warning is appearing.
Address the following warning:
Update the entire call hierarchy within this file so that
Typeparameters and
Type-returning methods all declare bothDynamicallyAccessedMemberTypes.PublicConstructorsandDynamicallyAccessedMemberTypes.PublicParameterlessConstructor.The latter is needed for
TypeMappings.CreateInstance<T>().For the following warnings:
Annotate the
typeparameter with[DynamicallyAccessedMembers].(This doesn't touch
Type.BaseType, but fixed the IL2075.)Unfortunately, after addressing all of the above, non-trimmer
enabled builds start failing:
Suppress these warnings.
Fix and suppress the following warning:
Fix this by updating all callsites to use an assembly-qualified name
instead of relying on
ToolkitHelper.GetProperty()to do a stringconcat before a
Type.GetType()invocation. This along with[DynamicallyAccessedMembers]on thetypeNameparameter shouldallow NativeAOT to preserve the referenced types.
That doesn't actually remove the IL2057, though, so suppress it.
For compatibility, if the initial
Type.GetType()fails, fallbackto appending
",Uno.UI.Toolkit"to the type name.