Skip to content

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Jul 29, 2025

Adds new analyzer warnings for references to assemblies not marked as trim or aot compatible.

The new warnings are opt-in under the following settings:

  • <VerifyReferenceTrimCompatibility>true</VerifyReferenceTrimCompatibility>
  • <VerifyReferenceAotCompatibility>true</VerifyReferenceAotCompatibility>

The analyzer uses AssemblyMetadataAttribute in the referenced assemblies to detect whether they are marked as trim/aot compatible. To mark a project as trim/aot compatible, it should be built with the usual recommended property settings:

  • <IsTrimmable>true</IsTrimmable> adds [assembly: AssemblyMetadata("IsTrimmable", "True")]
  • <IsAotCompatible>true</IsAotCompatible> adds [assembly: AssemblyMetadata("IsAotCompatible", "True")

Fixes #117712

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 29, 2025
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jul 29, 2025
@sbomer sbomer marked this pull request as ready for review July 31, 2025 16:33
@Copilot Copilot AI review requested due to automatic review settings July 31, 2025 16:33
@sbomer sbomer requested a review from marek-safar as a code owner July 31, 2025 16:33
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces new analyzer warnings to detect when assemblies referenced by a project are not marked as trim or AOT compatible. The warnings are opt-in via the VerifyReferenceTrimCompatibility and VerifyReferenceAotCompatibility MSBuild properties, helping developers identify potentially problematic dependencies during trimming or AOT compilation scenarios.

Key changes:

  • Adds two new diagnostic IDs for trim and AOT compatibility warnings
  • Extends the existing trim and AOT analyzers to check referenced assemblies for compatibility metadata
  • Provides comprehensive test coverage for the new functionality

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
ReferenceTrimCompatibilityTests.cs Test cases for the new trim compatibility analyzer warnings
ReferenceCompatibilityTestUtils.cs Utility class to create test scenarios with external assembly references
ReferenceAotCompatibilityTests.cs Test cases for the new AOT compatibility analyzer warnings
ILLink.RoslynAnalyzer.Tests.csproj Simplifies reference assembly copying for tests
SharedStrings.resx Adds resource strings for the new diagnostic messages
DiagnosticId.cs Defines new diagnostic IDs for trim and AOT compatibility warnings
Microsoft.NET.ILLink.Analyzers.props Exposes the new MSBuild properties to the compiler
RequiresUnreferencedCodeAnalyzer.cs Implements trim compatibility checking for referenced assemblies
RequiresDynamicCodeAnalyzer.cs Implements AOT compatibility checking for referenced assemblies
RequiresAnalyzerBase.cs Adds infrastructure to support compilation-level analysis actions
MSBuildPropertyOptionNames.cs Defines constants for the new MSBuild property names
Comments suppressed due to low confidence (2)

src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnreferencedCodeAnalyzer.cs:67

  • The variable name 'isTrimmable' is misleading as it stores the attribute object, not a boolean value. Consider renaming it to 'trimCompatibilityAttribute' or 'isTrimmableAttribute'.
                var isTrimmable = refAssembly.GetAttributes().FirstOrDefault(attr =>

src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresDynamicCodeAnalyzer.cs:46

  • The variable name 'isAotCompatible' is misleading as it stores the attribute object, not a boolean value. Consider renaming it to 'aotCompatibilityAttribute' or 'isAotCompatibleAttribute'.
                var isAotCompatible = refAssembly.GetAttributes().FirstOrDefault(attr =>

The IsAotCompatible opt-out was accidentally
added back after being intentionally removed
due to a bad merge.
@sbomer
Copy link
Member Author

sbomer commented Jul 31, 2025

@ViktorHofer @dotnet/illink I've changed the analyzer tests to run against a live ref pack (the one we just built) to get the latest IsTrimmable/IsAotCompatible metadata. This means the dotnet-linker-tests pipeline will need to build libs before running analyzer tests. Any concerns with this?

@sbomer sbomer requested a review from a team July 31, 2025 16:43
@sbomer sbomer requested a review from MichalStrehovsky July 31, 2025 16:43
@ViktorHofer
Copy link
Member

Any concerns with this?

No concerns with that. You can build just the libs.sfx subset if you want to optimize.

- Don't mention attribute in messages
- Build libs.sfx instead of libs
jonathanpeppers added a commit to dotnet/android that referenced this pull request Aug 4, 2025
Context: dotnet/runtime#118180

The new warnings are opt-in under the following settings:

* `<VerifyReferenceTrimCompatibility>true</VerifyReferenceTrimCompatibility>`
* `<VerifyReferenceAotCompatibility>true</VerifyReferenceAotCompatibility>`
jonathanpeppers added a commit to dotnet/android that referenced this pull request Aug 4, 2025
Context: dotnet/runtime#118180

The new warnings are opt-in under the following settings:

* `<VerifyReferenceTrimCompatibility>true</VerifyReferenceTrimCompatibility>`
* `<VerifyReferenceAotCompatibility>true</VerifyReferenceAotCompatibility>`
@sbomer sbomer merged commit e911894 into dotnet:main Aug 4, 2025
152 of 154 checks passed
@sbomer sbomer deleted the transitiveTrimWarning branch August 7, 2025 17:38
@github-actions github-actions bot locked and limited conversation to collaborators Sep 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
linkable-framework Issues associated with delivering a linker friendly framework needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emit AOT/trim analyzer warning when reference doesn't have IsTrimmable/IsAotCompatible
4 participants