Skip to content

[net10.0] Aggressive attribute trimming breaks NativeAOT builds #22065

@simonrozsival

Description

@simonrozsival

Apple platform

Mac Catalyst, iOS

Framework version

net10.0-*

Affected platform version

.NET 10 P1

Description

The recent addition of the switch/build property MobileAggressiveAttributeTrimming which enables aggressive attribute trimming in .NET 10 (#21314) causes problems with NativeAOT builds. This problem surfaced in dotnet/maui#27103

When the feature switch is enabled (the default behavior), ILLink removes all [DynamicallyAccessedMembers] and when ILC builds a NativeAOT binary, it cannot see these annotations anymore and it produces trim warnings and the final binary most likely won't work as expected.

I see two possible fixes:

  • Disable MobileAggressiveAttributeTrimming for NativeAOT builds and let ILC trim unnecessary attributes
  • Remove DynamicallyAccessedMembersAttribute from TrimAttributes.LinkDescription.xml.
    • My hunch is that this will reveal other problems, because the NativeAOT compiler uses other attributes from the System.Diagnostics.CodeAnalysis namespace for static analysis of the program, such as the FeatureGuardAttribute.

/cc @rolfbjarne @jonathanpeppers @sbomer

Steps to Reproduce

  1. Create a new MAUI project
  2. Add <PublishAot Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">true</PublishAot> to the project file
  3. Build the project with a local build of .NET MAUI from the net10.0 branch with this command:
    $(MAUI_DIR)/dotnet-local.sh publish -f net10.0-maccatalyst -r maccatalyst-arm64 -p:TrimmerSingleWarn=false

Did you find any workaround?

Set $(MobileAggressiveAttributeTrimming)=false in the MAUI project.

Build logs

 TestApp net10.0-maccatalyst succeeded with 18 warning(s) (37.4s) → bin/Release/net10.0-maccatalyst/maccatalyst-arm64/TestApp.dll
    /.../Projects/dotnet/maui/src/Controls/src/Core/BindableProperty.cs(93): Trim analysis warning IL2067: Microsoft.Maui.Controls.BindableProperty.BindableProperty(String,Type,Type,Object,BindingMode,BindableProperty.ValidateValueDelegate,BindableProperty.BindingPropertyChangedDelegate,BindableProperty.BindingPropertyChangingDelegate,BindableProperty.CoerceValueDelegate,BindableProperty.BindablePropertyBindingChanging,Boolean,BindableProperty.CreateDefaultValueDelegate): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The parameter '#1' of method 'Microsoft.Maui.Controls.BindableProperty.BindableProperty(String,Type,Type,Object,BindingMode,BindableProperty.ValidateValueDelegate,BindableProperty.BindingPropertyChangedDelegate,BindableProperty.BindingPropertyChangingDelegate,BindableProperty.CoerceValueDelegate,BindableProperty.BindablePropertyBindingChanging,Boolean,BindableProperty.CreateDefaultValueDelegate)' 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.
    ILC : Trim analysis warning IL2070: Microsoft.Extensions.DependencyInjection.ActivatorUtilities.TryFindMatchingConstructor(Type,Type[],ConstructorInfo&,Nullable`1<Int32>[]&): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Type.GetConstructors()'. The parameter '#0' of method 'Microsoft.Extensions.DependencyInjection.ActivatorUtilities.TryFindMatchingConstructor(Type,Type[],ConstructorInfo&,Nullable`1<Int32>[]&)' 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.
    ILC : Trim analysis warning IL2067: Microsoft.Extensions.Internal.ParameterDefaultValue.<TryGetDefaultValue>g__CreateValueType|0_0(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type)'. The parameter '#0' of method 'Microsoft.Extensions.Internal.ParameterDefaultValue.<TryGetDefaultValue>g__CreateValueType|0_0(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.
 ...

Build succeeded with 18 warning(s) in 29.0s

Metadata

Metadata

Assignees

Labels

bugIf an issue is a bug or a pull request a bug fix

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions