vkhorikov/CSharpFunctionalExtensions

IL2104 warning when publishing with `/p:PublishTrimmed=true` on .NET 6.0

Closed this issue · 1 comments

Hi @vkhorikov,

I have a console application project using your package that I recently upgraded to .NET 6.0. I noticed that when publishing the project with assembly trimming, build process generates warnings for the package.

Here's a simple example to reproduce the issue. First the .csproj file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CSharpFunctionalExtensions" Version="2.25.0" />
  </ItemGroup>

</Project>

then a .cs file using the Result<> class (uses C# 10 syntax):

using System;
using CSharpFunctionalExtensions;

var result = GetMessage();
if (result.IsSuccess)
{
    Console.WriteLine(result.Value);
}

static Result<string> GetMessage()
{
    return "Hello World!";
}

Then publishing it with

dotnet publish --self-contained --runtime linux-x64 /p:PublishTrimmed=true

outputs

Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /home/dev/dotnet/TrimmingTest/Net6Test/Net6Test.csproj (in 719 ms).
  Net6Test -> /home/dev/dotnet/TrimmingTest/Net6Test/bin/Debug/net6.0/linux-x64/Net6Test.dll
/home/dev/.nuget/packages/csharpfunctionalextensions/2.25.0/lib/net5.0/CSharpFunctionalExtensions.dll : warning IL2104: Assembly 'CSharpFunctionalExtensions' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [/home/dev/dotnet/TrimmingTest/Net6Test/Net6Test.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
  Net6Test -> /home/dev/dotnet/TrimmingTest/Net6Test/bin/Debug/net6.0/linux-x64/publish/

For me this is problematic mainly because my project is set to treat warnings as errors, so the entire build fails because of this. I didn't investigate if the warning is otherwise justified and can produce faulty output.

I'm executing this with dotnet 6.0.100 on Linux. Similar example using <TargetFramework>net5.0</TargetFramework> in .csproj does not produce the warning, so I suspect that making the package itself target .NET 6.0 and resolving potential upgrade issues will fix it, but I didn't try it.

Thanks in advance for your consideration, I'm a big fan of your work. :)

BR

Just a bit more info. I just tried to clone the package repo and make it target net6.0. Then I built the package and added it to the project from local source. Unfortunately that is not enough to get rid of the warning :(.