microsoft / microsoft/codecoverage

Microsoft.CodeCoverage.MSBuild introduces new trim warnings in otherwise trim-safe project

Open
#200 0 comments 0 reactions 1 assignee View on GitHub

@fhnaseer is already working on this.

Since Jan 5, 2026.

Dominant language
C#
Stars
125
Forks
17
Avg merge
1h 17m
Merged PRs (30d)
2

Description

Describe the bug

Adding static code coverage instrumentation to a NativeAOT test binary introduces trim warnings where there were none before.

My guess is whatever IL rewriting is going on for static code analysis, it isn't applying appropriate attributes to the modified code to preserve trim safety.

Steps To Reproduce

git clone -b tunit-code-coverage git@github.com:AArnott/Nerdbank.MessagePack.git
cd Nerdbank.MessagePack
./init.ps1
dotnet publish -f net10.0 .\test\Nerdbank.MessagePack.TUnit\

Expected behavior

No trim warnings.

Actual behavior

  Nerdbank.MessagePack.TUnit net10.0 win-x64 succeeded with 3 warning(s) (26.3s) → bin\Nerdbank.MessagePack.TUnit\Debug\net10.0\win-x64\publish\
    E:\src\nerdbank.messagepack\src\Nerdbank.MessagePack\ConverterTypeCollection.cs(116): Trim analysis warning IL2078: Nerdbank.MessagePack.ConverterTypeCollection.ConverterType.Type.get: 'Nerdbank.MessagePack.ConverterTypeCollection.ConverterType.Type.get' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' requirements. The field 'Nerdbank.MessagePack.ConverterTypeCollection.ConverterType.<Type>k__BackingField' 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.
    E:\src\nerdbank.messagepack\src\Nerdbank.MessagePack\UseComparerAttribute.cs(56): Trim analysis warning IL2078: Nerdbank.MessagePack.UseComparerAttribute.ComparerType.get: 'Nerdbank.MessagePack.UseComparerAttribute.ComparerType.get' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor', 'DynamicallyAccessedMemberTypes.PublicProperties' requirements. The field 'Nerdbank.MessagePack.UseComparerAttribute.<ComparerType>k__BackingField' 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.
    E:\src\nerdbank.messagepack\src\Nerdbank.MessagePack\MessagePackConverterAttribute.cs(32): Trim analysis warning IL2078: Nerdbank.MessagePack.MessagePackConverterAttribute.ConverterType.get: 'Nerdbank.MessagePack.MessagePackConverterAttribute.ConverterType.get' method return value does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' requirements. The field 'Nerdbank.MessagePack.MessagePackConverterAttribute.<ConverterType>k__BackingField' 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.

Additional context

If you run git checkout HEAD~1 and then repeat the publish command, you can see how the trim warnings disappear.
The only difference is you've removed the last commit, which is what added the Microsoft.CodeCoverage.MSBuild package dependency and AotMsCodeCoverageInstrumentation property.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.