Skip to content

System.Text.Json 10.0.0 produces trim warning in NativeAOT published net8.0 app #122035

@stephentoub

Description

@stephentoub

Repro:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
    <InvariantGlobalization>true</InvariantGlobalization>
    <TrimmerSingleWarn>false</TrimmerSingleWarn>
  </PropertyGroup>

  <ItemGroup>
    <TrimmerRootAssembly Include="System.Text.Json" />
    <PackageReference Include="System.Text.Json" Version="10.0.0" />
  </ItemGroup>

</Project>
Console.WriteLine("test");

When publishing for 10, this works fine:

dotnet publish -c release -f net10.0                              07:22:06   9.565s
  tmpapp net10.0 win-x64 succeeded (9.3s) → bin\release\net10.0\win-x64\publish\

Build succeeded in 9.4s

But when publishing for 8, it produces trim warnings:

dotnet publish -c release -f net8.0                                 07:21:30   57ms
  tmpapp net8.0 win-x64 succeeded with 2 warning(s) (8.5s) → bin\release\net8.0\win-x64\publish\
    ILC : Trim analysis warning IL2070: System.Text.Json.Serialization.Converters.EnumConverterFactory.Create(Type,EnumConverterOptions,JsonNamingPolicy,JsonSerializerOptions): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Type.MakeGenericType(Type[])'. The parameter 'enumType' of method 'System.Text.Json.Serialization.Converters.EnumConverterFactory.Create(Type,EnumConverterOptions,JsonNamingPolicy,JsonSerializerOptions)' 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: System.Text.Json.Serialization.Converters.NullableConverterFactory.GetNullableConverterType(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Type.MakeGenericType(Type[])'. The parameter 'valueTypeToConvert' of method 'System.Text.Json.Serialization.Converters.NullableConverterFactory.GetNullableConverterType(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 2 warning(s) in 8.6s

This is problematic because it means that anything that ends up including STJ 10.0.0 directly or transitively in a .NET 8 NativeAOT app gets trim warnings.

Did we make some fix in .NET 9 or .NET 10 such that this no longer warns on 9/10 but does still on 8? Can we backport whatever that fix is to 8?

cc: @eiriktsarpalis, @MichalStrehovsky, @sbomer

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions