Edit

Share via


IL2125: Referenced assembly is not annotated for trim compatibility

Cause

A project has <VerifyReferenceTrimCompatibility>true</VerifyReferenceTrimCompatibility> set, and one or more referenced assemblies don't have the IsTrimmable assembly metadata attribute set to true.

Rule description

When you publish a trimmed app using <PublishTrimmed>true</PublishTrimmed> or mark your project as trimmable with <IsTrimmable>true</IsTrimmable>, you can optionally enable verification that all referenced assemblies are also annotated for trim compatibility. This helps ensure that all dependencies in your project are annotated for trim compatibility.

To enable this verification, set the VerifyReferenceTrimCompatibility property to true in your project file:

<PropertyGroup>
  <PublishTrimmed>true</PublishTrimmed>
  <VerifyReferenceTrimCompatibility>true</VerifyReferenceTrimCompatibility>
</PropertyGroup>

When this property is enabled, the analyzer checks that all referenced assemblies have been built with <IsTrimmable>true</IsTrimmable>, which adds the assembly-level attribute [assembly: AssemblyMetadata("IsTrimmable", "True")] to the assembly.

Example

// Assembly reference: MyLibrary.dll (built without <IsTrimmable>true</IsTrimmable>)

public class Program
{
    public static void Main()
    {
        var obj = new MyLibrary.SomeClass();
    }
}
warning IL2125: Referenced assembly 'MyLibrary' is not built with `<IsTrimmable>true</IsTrimmable>` and may not be compatible with trimming.

How to fix violations

You have several options to fix this warning:

  • Update the referenced library to be built with <IsTrimmable>true</IsTrimmable>. This is the preferred approach if you control the library source code. For guidance on making libraries trim-compatible, see Prepare .NET libraries for trimming.
  • Disable the verification by setting <VerifyReferenceTrimCompatibility>false</VerifyReferenceTrimCompatibility> in your project file if you're confident that the library works correctly with trimming even without the attribute.

See also