Open
Description
Publish the following application with -p:PublishTrimmed=true
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var json = @"{""Name"":""Test"",""Sizes"":[0, 256, 256]}";
var s = (Shape)JsonSerializer.Deserialize(json, typeof(Shape));
Console.WriteLine(s.Sizes[0]);
Console.WriteLine(s.Sizes[1]);
Console.WriteLine(s.Sizes[2]);
}
}
public class Shape
{
public string Name { get; set; }
public List<int> Sizes { get; set; }
}
}
Expected results
I only have 1 warning in my application, I should only see 1 warning in the output.
Actual results
❯ dotnet publish -r win-x64 -p:PublishTrimmed=true
Microsoft (R) Build Engine version 17.0.0-preview-21271-07+1560b6ce8 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
C:\DotNetTest\HelloWorld\Program.cs(13,28): warning IL2026: Using method 'System.Text.Json.JsonSerializer.Deserialize(string, System.Type, System.Text.Json.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.. [C:\DotNetTest\HelloWorld\HelloWorld.csproj]
HelloWorld -> C:\DotNetTest\HelloWorld\bin\Debug\net6.0\win-x64\HelloWorld.dll
C:\DotNetTest\HelloWorld\Program.cs(13,13): Trim analysis warning IL2026: ConsoleApp1.Program.Main(String[]): Using method 'System.Text.Json.JsonSerializer.Deserialize(String,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.. [C:\DotNetTest\HelloWorld\HelloWorld.csproj]
Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
HelloWorld -> C:\DotNetTest\HelloWorld\bin\Debug\net6.0\win-x64\publish\
Notice that I'm getting duplicated warnings:
C:\DotNetTest\HelloWorld\Program.cs(13,28): warning IL2026: Using method 'System.Text.Json.JsonSerializer.Deserialize...
and
C:\DotNetTest\HelloWorld\Program.cs(13,13): Trim analysis warning IL2026: ConsoleApp1.Program.Main(String[]): Using method 'System.Text.Json.JsonSerializer.Deserialize...
I assume the first warning comes from running the Roslyn analyzer, while the second warning comes from running the linker.