Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YamlDotNet 16.2.0 produces trim warnings, while YamlDotNet 13.1.0 does not #1012

Closed
hach-que opened this issue Nov 29, 2024 · 5 comments · Fixed by #1013
Closed

YamlDotNet 16.2.0 produces trim warnings, while YamlDotNet 13.1.0 does not #1012

hach-que opened this issue Nov 29, 2024 · 5 comments · Fixed by #1013

Comments

@hach-que
Copy link

Describe the bug
When building a trimmed executable against YamlDotNet 13.1.0, the .NET linker is able to successfully build. After upgrading to YamlDotNet 16.2.0, linking now fails with:

/Users/runner/.nuget/packages/yamldotnet/16.2.0/lib/net8.0/YamlDotNet.dll : error IL2104: Assembly 'YamlDotNet' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [/Users/runner/work/uet/uet/UET/uet/uet.csproj]

To Reproduce
Create a project that has these dependencies:

<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="13.0.2" />
<PackageReference Include="YamlDotNet" Version="13.1.0" />

and enable trim warnings and other analyzers with:

  <PropertyGroup>
    <IsTrimmable>true</IsTrimmable>
    <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
    <AnalysisLevel>latest</AnalysisLevel>
    <AnalysisMode>All</AnalysisMode>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);CS1591;CS1573</NoWarn>
  </PropertyGroup>

The project will build successfully. Now modify the project dependencies to:

<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.2.0" />
<PackageReference Include="YamlDotNet" Version="16.2.0" />

and it will fail with that trim warning.

This issue could be caught in the YamlDotNet build process by adding the following property to the YamlDotNet project, though it will likely generate a bunch of warnings on dynamic APIs that either need to be addressed or silenced:

<PropertyGroup>
    <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup>
@EdwardCooke
Copy link
Collaborator

Can you share the code where you initialize the serializer and/or deserializer?

@hach-que
Copy link
Author

hach-que commented Nov 29, 2024

We currently have:

namespace Redpoint.Uet.BuildPipeline.Executors.GitLab
{
    using YamlDotNet.Serialization;

    [YamlStaticContext]
    public partial class GitLabYamlStaticContext : YamlDotNet.Serialization.StaticContext
    {
    }
}

and we serialize it like this:

using (var stream = new StreamWriter(buildServerOutputFilePath))
{
    var aotContext = new GitLabYamlStaticContext();
    var serializer = new StaticSerializerBuilder(aotContext)
        .WithQuotingNecessaryStrings()
        .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
        .Build();
    var yaml = serializer.Serialize(file);
    await stream.WriteLineAsync(yaml).ConfigureAwait(false);
}

You can see the full assembly implementation here: https://github.com/RedpointGames/uet/tree/main/UET/Redpoint.Uet.BuildPipeline.Executors.GitLab

@EdwardCooke
Copy link
Collaborator

Thanks. I’ll see what I can find.

@EdwardCooke
Copy link
Collaborator

Just got where the problem is, it was added with some of the fsharp code. I'll see what I can do to fix this. It shouldn't break anything in c#, just in f#. In theory.

2>"C:\Users\edward\.nuget\packages\runtime.win-x64.microsoft.dotnet.ilcompiler\8.0.11\tools\\ilc" @"obj\Release\net8.0\win-x64\native\YamlDotNetExamplesAOT.ilc.rsp"
2>ILC(0,0): Warning IL2075: YamlDotNet.Helpers.FsharpHelper.GetValue(IObjectDescriptor): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String)'. The return value of method 'YamlDotNet.Serialization.IObjectDescriptor.Type.get' 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.
2>"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\bin\HostX64\x64\link.exe" @"obj\Release\net8.0\win-x64\native\link.rsp"

@EdwardCooke
Copy link
Collaborator

I just pushed this fix out, you should see it in nuget shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants