Skip to content
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

[build] make TrimMode=full function like NativeAOT #22282

Merged
merged 4 commits into from
May 10, 2024

Conversation

jonathanpeppers
Copy link
Member

Context: dotnet/android#8805
Context: xamarin/xamarin-macios#20354

In .NET 9, we want .NET MAUI applications to be able to use the TrimMode=full option to remove unused code from the application:

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
  <TrimMode>full</TrimMode>
</PropertyGroup>

With all the trimming work done to support NativeAOT, we should toggle the same options when TrimMode=full is used:

  • MauiStrictXamlCompilation=true
  • MauiXamlRuntimeParsingSupport=false
  • MauiShellSearchResultsRendererDisplayMemberNameSupported=false
  • MauiQueryPropertyAttributeSupport=false
  • MauiImplicitCastOperatorsUsageViaReflectionSupport=false

With these set, the dotnet new maui project template should have zero trimmer warnings when TrimMode=full is used. Developers can also adjust these settings and respond to trimmer warnings in their own code.

I also updated RunOnAndroid and RunOniOS tests to verify that project templates launch successfully with TrimMode=full.

Context: dotnet/android#8805
Context: xamarin/xamarin-macios#20354

In .NET 9, we want .NET MAUI applications to be able to use the
`TrimMode=full` option to remove unused code from the application:

    <PropertyGroup Condition="'$(Configuration)' == 'Release'">
      <TrimMode>full</TrimMode>
    </PropertyGroup>

With all the trimming work done to support NativeAOT, we should toggle
the same options when `TrimMode=full` is used:

* `MauiStrictXamlCompilation=true`
* `MauiXamlRuntimeParsingSupport=false`
* `MauiShellSearchResultsRendererDisplayMemberNameSupported=false`
* `MauiQueryPropertyAttributeSupport=false`
* `MauiImplicitCastOperatorsUsageViaReflectionSupport=false`

With these set, the `dotnet new maui` project template *should* have
zero trimmer warnings when `TrimMode=full` is used. Developers can also
adjust these settings and respond to trimmer warnings in their own code.

I also updated `RunOnAndroid` and `RunOniOS` tests to verify that
project templates launch successfully with `TrimMode=full`.
@jonathanpeppers
Copy link
Member Author

@simonrozsival it seems like this found a single warning:

src\Controls\src\Core\StyleSheets\StylePropertyAttribute.cs(24,4): error IL2069: Microsoft.Maui.Controls.StyleSheets.StylePropertyAttribute.StylePropertyAttribute(String, Type, String): value stored in field 'Microsoft.Maui.Controls.StyleSheets.StylePropertyAttribute.<TargetType>k__BackingField' does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' requirements. The parameter 'targetType' of method 'Microsoft.Maui.Controls.StyleSheets.StylePropertyAttribute.StylePropertyAttribute(String, 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.

Is this different behavior than NativeAOT?

See if this fixes trimmer
@jonathanpeppers
Copy link
Member Author

I guess maui-blazor apps have trimmer warnings (but iOS only?):

ILLink error IL2072: Microsoft.AspNetCore.Components.CascadingParameterState.FindCascadingParameters(ComponentState, Boolean&): 'componentType' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'Microsoft.AspNetCore.Components.CascadingParameterState.GetCascadingParameterInfos(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.
ILLink error IL2072: Microsoft.AspNetCore.Components.ComponentFactory.PerformPropertyInjection(IServiceProvider, IComponent): 'componentType' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'Microsoft.AspNetCore.Components.ComponentFactory.GetComponentTypeInfo(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.
ILLink error IL2026: Microsoft.AspNetCore.Components.WebView.IpcCommon.Serialize(String, Object[]): Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' 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.
ILLink error IL2026: Microsoft.AspNetCore.Components.WebView.IpcCommon.TryDeserialize<T>(String, T&, ArraySegment`1&): Using member 'System.Text.Json.JsonSerializer.Deserialize(ReadOnlySpan<Char>, Type, JsonSerializerOptions)' 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.
ILLink error IL2067: Microsoft.AspNetCore.Components.WebView.Services.WebViewRenderer.AddRootComponent(Type, String): 'componentType' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'Microsoft.AspNetCore.Components.RenderTree.WebRenderer.AddRootComponent(Type, String)'. The parameter 'componentType' of method 'Microsoft.AspNetCore.Components.WebView.Services.WebViewRenderer.AddRootComponent(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.
ILLink error IL2111: Microsoft.JSInterop.Infrastructure.DotNetDispatcher.GetCachedMethodInfo(IDotNetObjectReference, String): Method 'Microsoft.JSInterop.Infrastructure.DotNetDispatcher.<GetCachedMethodInfo>g__ScanTypeForCallableMethods|16_0(Type)' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method.
ILLink error IL2065: Microsoft.JSInterop.Infrastructure.DotNetDispatcher.ScanAssemblyForCallableMethods(DotNetDispatcher.AssemblyKey): Value passed to implicit 'this' parameter of method 'System.Type.GetMethods(BindingFlags)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
ILLink error IL2121: Microsoft.JSInterop.Infrastructure.DotNetDispatcher.ScanAssemblyForCallableMethods(DotNetDispatcher.AssemblyKey): Unused 'UnconditionalSuppressMessageAttribute' for warning 'IL2072'. Consider removing the unused warning suppression.
ILLink error IL2121: Microsoft.JSInterop.Infrastructure.DotNetDispatcher.ScanAssemblyForCallableMethods(DotNetDispatcher.AssemblyKey): Unused 'UnconditionalSuppressMessageAttribute' for warning 'IL2075'. Consider removing the unused warning suppression.
ILLink error IL2121: Microsoft.JSInterop.JSRuntime.EndInvokeJS(Int64, Boolean, Utf8JsonReader&): Unused 'UnconditionalSuppressMessageAttribute' for warning 'IL2072'. Consider removing the unused warning suppression.

Android somehow passes:
image

I will probably exclude maui-blazor for now.

@jonathanpeppers jonathanpeppers marked this pull request as ready for review May 8, 2024 23:53
@jonathanpeppers jonathanpeppers requested a review from a team as a code owner May 8, 2024 23:53
@simonrozsival
Copy link
Member

It is weird that we didn't see the StylePropertyAttribute warning before. I'll need to investigate this a bit more and see if it's intentional or if it is a bug in one of the analyzers.

Copy link
Member

@simonrozsival simonrozsival left a comment

Choose a reason for hiding this comment

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

We should look into the Blazor warnings separately, these warnings are coming from a different repository (https://github.com/dotnet/aspnetcore).

@Eilon Eilon added the area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) label May 9, 2024
@jonathanpeppers jonathanpeppers merged commit a45f702 into dotnet:net9.0 May 10, 2024
48 checks passed
@jonathanpeppers jonathanpeppers deleted the TrimModeLink branch May 10, 2024 13:33
@github-actions github-actions bot locked and limited conversation to collaborators Jun 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) fixed-in-9.0.0-preview.5.24307.10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants