dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.97k stars 4.66k forks source link

ILC and ILLink don't respect TrimmerSingleWarn in some cases #94799

Closed eerhardt closed 10 months ago

eerhardt commented 10 months ago

Description

There are a class of trimming warnings that still get emitted as IL2026 from assemblies being referenced instead of the IL2104 that is supposed to be emitted when the warning comes from a referenced assembly.

Reproduction Steps

dotnet publish the following app

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0" />
    <PackageReference Include="Polly.Core" Version="8.2.0" />
  </ItemGroup>
</Project>

(Note the same behavior occurs with <PublishTrimmed>true</PublishTrimmed> instead of PublishAot.)

using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services
    .AddHttpClient<WeatherClient>()
    .AddStandardResilienceHandler();

public class WeatherClient() { }

Expected behavior

Since the referenced library Microsoft.Extensions.Http.Resilience has trimming warnings the only warning that should be emitted is a IL2104. This is the normal behavior when a referenced library has warnings.

Actual behavior

3 individual warnings are emitted instead:

ILC : Trim analysis warning IL2026: __OptionValidationStaticInstances.<Validators_g>F8B1F3D42962A35D8FF1B1489612AEF6C36F3713335EFF79DB68A25973333C495____Attributes..cctor(): Using member
'System.ComponentModel.DataAnnotations.RangeAttribute.RangeAttribute(Type,String,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application c
ode. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All. [C:\Users\eerh
ardt\source\repos\ConsoleApp103\ConsoleApp103\ConsoleApp103.csproj]
ILC : Trim analysis warning IL2026: __OptionValidationStaticInstances.<Validators_g>F8B1F3D42962A35D8FF1B1489612AEF6C36F3713335EFF79DB68A25973333C495____Attributes..cctor(): Using member
'System.ComponentModel.DataAnnotations.RangeAttribute.RangeAttribute(Type,String,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application c
ode. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All. [C:\Users\eerh
ardt\source\repos\ConsoleApp103\ConsoleApp103\ConsoleApp103.csproj]
ILC : Trim analysis warning IL2026: __OptionValidationStaticInstances.<Validators_g>F8B1F3D42962A35D8FF1B1489612AEF6C36F3713335EFF79DB68A25973333C495____Attributes..cctor(): Using member
'System.ComponentModel.DataAnnotations.RangeAttribute.RangeAttribute(Type,String,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application c
ode. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All. [C:\Users\eerh
ardt\source\repos\ConsoleApp103\ConsoleApp103\ConsoleApp103.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

ghost commented 10 months ago

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.

Issue Details
### Description There are a class of trimming warnings that still get emitted as `IL2026` from assemblies being referenced instead of the `IL2104` that is supposed to be emitted when the warning comes from a referenced assembly. ### Reproduction Steps `dotnet publish` the following app ```xml Exe net8.0 enable enable true true ``` (Note the same behavior occurs with `true` instead of `PublishAot`.) ```C# using Microsoft.Extensions.DependencyInjection; var services = new ServiceCollection(); services .AddHttpClient() .AddStandardResilienceHandler(); public class WeatherClient() { } ``` ### Expected behavior Since the referenced library `Microsoft.Extensions.Http.Resilience` has trimming warnings the only warning that should be emitted is a `IL2104`. This is the normal behavior when a referenced library has warnings. ### Actual behavior 3 individual warnings are emitted instead: ``` ILC : Trim analysis warning IL2026: __OptionValidationStaticInstances.F8B1F3D42962A35D8FF1B1489612AEF6C36F3713335EFF79DB68A25973333C495____Attributes..cctor(): Using member 'System.ComponentModel.DataAnnotations.RangeAttribute.RangeAttribute(Type,String,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application c ode. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All. [C:\Users\eerh ardt\source\repos\ConsoleApp103\ConsoleApp103\ConsoleApp103.csproj] ILC : Trim analysis warning IL2026: __OptionValidationStaticInstances.F8B1F3D42962A35D8FF1B1489612AEF6C36F3713335EFF79DB68A25973333C495____Attributes..cctor(): Using member 'System.ComponentModel.DataAnnotations.RangeAttribute.RangeAttribute(Type,String,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application c ode. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All. [C:\Users\eerh ardt\source\repos\ConsoleApp103\ConsoleApp103\ConsoleApp103.csproj] ILC : Trim analysis warning IL2026: __OptionValidationStaticInstances.F8B1F3D42962A35D8FF1B1489612AEF6C36F3713335EFF79DB68A25973333C495____Attributes..cctor(): Using member 'System.ComponentModel.DataAnnotations.RangeAttribute.RangeAttribute(Type,String,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application c ode. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All. [C:\Users\eerh ardt\source\repos\ConsoleApp103\ConsoleApp103\ConsoleApp103.csproj] ``` ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration _No response_ ### Other information _No response_
Author: eerhardt
Assignees: -
Labels: `untriaged`, `area-NativeAOT-coreclr`
Milestone: -
eerhardt commented 10 months ago

cc @vitek-karas @sbomer

MichalStrehovsky commented 10 months ago

Is this from https://github.com/dotnet/linker/pull/2087? That one was intentional. Do the original reasons not apply here?

eerhardt commented 10 months ago

Ah, good call. This is something I forgot about, and it wasn't obvious to me that the reason these warnings weren't being single-warned was because the library had IsTrimmable on it.

The good thing is that these warnings will be fixed by https://github.com/dotnet/runtime/pull/94798, so they won't be seen soon.