TUnit icon indicating copy to clipboard operation
TUnit copied to clipboard

Trim analysis warnings when using `PublishAot`

Open philipp-naused opened this issue 9 months ago • 1 comments

This is a follow-up to #2393 Repro steps: Run dotnet publish --use-current-runtime -t:Rebuild on this project: Test1.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <PublishAot>true</PublishAot>
    <TrimmerSingleWarn>false</TrimmerSingleWarn>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="TUnit" Version="0.20.18" />
  </ItemGroup>
</Project>

Result:

ILC : Trim analysis warning IL2093: TUnit.Core.SourceGeneratedParameterInformation.Type.get: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'TUnit.Core.SourceGeneratedParameterInformation.Type.get' don't match overridden return value of method 'TUnit.Core.SourceGeneratedMemberInformation.Type.get'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
ILC : Trim analysis warning IL2092: TUnit.Core.SourceGeneratedParameterInformation.Type.set: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 'value' of method 'TUnit.Core.SourceGeneratedParameterInformation.Type.set' don't match overridden parameter 'value' of method 'TUnit.Core.SourceGeneratedMemberInformation.Type.set'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
ILC : Trim analysis warning IL2075: TUnit.Engine.Helpers.MethodInfoHelper.InvokeInstanceHook(MethodInfo,Object,TestContext,CancellationToken): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.PublicNestedTypes', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.PublicEvents' in call to 'System.Type.GetMembers()'. 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.

Without the TrimmerSingleWarn property, you get a less useful warning: warning IL2104: Assembly 'TUnit.Core' produced trim warnings.

Using .NET SDK 10.0.100-preview.3.25201.16 on Windows.

philipp-naused avatar May 19 '25 15:05 philipp-naused

This only fixed IL2075 in this example. The other two warnings are still present in 0.21.1. In an actual project with code, I get all 3 codes:

ILC : Trim analysis warning IL2093: TUnit.Core.SourceGeneratedParameterInformation.Type.get: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'TUnit.Core.SourceGeneratedParameterInformation.Type.get' don't match overridden return value of method 'TUnit.Core.SourceGeneratedMemberInformation.Type.get'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
ILC : Trim analysis warning IL2092: TUnit.Core.SourceGeneratedParameterInformation.Type.set: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 'value' of method 'TUnit.Core.SourceGeneratedParameterInformation.Type.set' don't match overridden parameter 'value' of method 'TUnit.Core.SourceGeneratedMemberInformation.Type.set'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
ILC : Trim analysis warning IL2075: TUnit.Assertions.Compare.<CheckEquivalent>d__2`2.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' in call to 'System.Type.GetFields()'. 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.
ILC : Trim analysis warning IL2075: TUnit.Assertions.Compare.<CheckEquivalent>d__2`2.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' in call to 'System.Type.GetFields()'. 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.
ILC : Trim analysis warning IL2075: TUnit.Assertions.Compare.<CheckEquivalent>d__2`2.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetField(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.
ILC : Trim analysis warning IL2075: TUnit.Assertions.Compare.<CheckEquivalent>d__2`2.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetField(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.
ILC : Trim analysis warning IL2075: TUnit.Assertions.Compare.<CheckEquivalent>d__2`2.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. 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.
ILC : Trim analysis warning IL2075: TUnit.Assertions.Compare.<CheckEquivalent>d__2`2.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. 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.
ILC : Trim analysis warning IL2075: TUnit.Assertions.Compare.<CheckEquivalent>d__2`2.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' 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.
ILC : Trim analysis warning IL2075: TUnit.Assertions.Compare.<CheckEquivalent>d__2`2.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' 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.

PhilippNaused avatar May 19 '25 20:05 PhilippNaused

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jun 19 '25 00:06 github-actions[bot]

I can still reproduce this issue. Don't close it.

PhilippNaused avatar Jun 19 '25 09:06 PhilippNaused

Starting with v0.50.0 I also get IL2026, IL2060, and IL2075, IL3000 in my tests. All of them are about Trim/AOT analysis from TUnit or TUnit's generated code. You get a lot more of them when targeting .NET 10.

PhilippNaused avatar Aug 04 '25 19:08 PhilippNaused

Still reproducible in 0.61.58. Same as #2458.

Test1 succeeded with 8 warning(s) (8.2s) → bin\Release\net8.0\win-x64\publish\
    ILC : Trim analysis warning IL2041: Polyfills.Polyfill.GetMethod(Type,String,Int32,BindingFlags,Type[]): The 'DynamicallyAccessedMembersAttribute' is not allowed on methods. It is allowed on method return value or method parameters though.
    ILC : Trim analysis warning IL2093: TUnit.Core.ParameterMetadata.Type.get: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'TUnit.Core.ParameterMetadata.Type.get' don't match overridden return value of method 'TUnit.Core.MemberMetadata.Type.get'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    ILC : Trim analysis warning IL2092: TUnit.Core.ParameterMetadata.Type.set: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 'value' of method 'TUnit.Core.ParameterMetadata.Type.set' don't match overridden parameter 'value' of method 'TUnit.Core.MemberMetadata.Type.set'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    ILC : Trim analysis warning IL2026: TUnit.Engine.Framework.TUnitServiceProvider.TUnitServiceProvider(IExtension,ExecuteRequestContext,ITestExecutionFilter,IMessageBus,IServiceProvider,ITestFrameworkCapabilities): Using member 'TUnit.Engine.Discovery.ReflectionTestDataCollector.ReflectionTestDataCollector()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Reflection-based test discovery requires unreferenced code.
    ILC : AOT analysis warning IL3050: TUnit.Engine.Framework.TUnitServiceProvider.TUnitServiceProvider(IExtension,ExecuteRequestContext,ITestExecutionFilter,IMessageBus,IServiceProvider,ITestFrameworkCapabilities): Using member 'TUnit.Engine.Discovery.ReflectionTestDataCollector.ReflectionTestDataCollector()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Expression compilation requires dynamic code generation.
    ILC : warning IL3000: TUnit.Engine.Discovery.ReflectionTestDataCollector.ShouldScanAssembly(Assembly): 'System.Reflection.Assembly.Location.get' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
    ILC : Trim analysis warning IL2070: TUnit.Engine.Services.TestGenericTypeResolver.TryInferTypeMapping(Type,Type,Dictionary`2<Type,Type>): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'argumentType' of method 'TUnit.Engine.Services.TestGenericTypeResolver.TryInferTypeMapping(Type,Type,Dictionary`2<Type,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.
    ILC : Trim analysis warning IL2077: TUnit.Engine.Discovery.ReflectionTestDataCollector.DynamicReflectionTestMetadata.<>c__DisplayClass5_0.<<get_CreateExecutableTestFactory>b__1>d.MoveNext(): 'testClassType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'TUnit.Core.Helpers.ClassConstructorHelper.TryCreateInstanceWithClassConstructor(IReadOnlyList`1<Attribute>,Type,String,TestContext)'. The field 'TUnit.Engine.Discovery.ReflectionTestDataCollector.DynamicReflectionTestMetadata._testClass' 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 8 warning(s) in 8.6s

PhilippNaused avatar Sep 30 '25 07:09 PhilippNaused

Still getting 5 of them in 0.63.0

Test1 succeeded with 5 warning(s) (7.5s) → bin\Release\net8.0\win-x64\publish\
    ILC : Trim analysis warning IL2041: Polyfills.Polyfill.GetMethod(Type,String,Int32,BindingFlags,Type[]): The 'DynamicallyAccessedMembersAttribute' is not allowed on methods. It is allowed on method return value or method parameters though.
    ILC : Trim analysis warning IL2093: TUnit.Core.ParameterMetadata.Type.get: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method 'TUnit.Core.ParameterMetadata.Type.get' don't match overridden return value of method 'TUnit.Core.MemberMetadata.Type.get'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    ILC : Trim analysis warning IL2092: TUnit.Core.ParameterMetadata.Type.set: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 'value' of method 'TUnit.Core.ParameterMetadata.Type.set' don't match overridden parameter 'value' of method 'TUnit.Core.MemberMetadata.Type.set'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    ILC : Trim analysis warning IL2065: TUnit.Engine.Discovery.ReflectionTestDataCollector.<DiscoverDynamicTests>d__39.MoveNext(): Value passed to implicit 'this' parameter of method 'System.Type.GetMethods(BindingFlags)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
    ILC : Trim analysis warning IL2070: TUnit.Engine.Services.TestGenericTypeResolver.TryInferTypeMapping(Type,Type,Dictionary`2<Type,Type>): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'argumentType' of method 'TUnit.Engine.Services.TestGenericTypeResolver.TryInferTypeMapping(Type,Type,Dictionary`2<Type,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 5 warning(s) in 7.8s

PhilippNaused avatar Oct 02 '25 18:10 PhilippNaused

Should be fixed now!

thomhurst avatar Oct 05 '25 18:10 thomhurst

@thomhurst I'm sorry, but I still get warnings from the generated code: #3312

PhilippNaused avatar Oct 06 '25 18:10 PhilippNaused