Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

Summary

The trim analyzer is only supported in .NET 6+, and AOT compatibility analysis is only supported in .NET 8+, but most libraries must support earlier target frameworks like .NET Standard 2.0 or .NET Framework. The documentation lacked guidance on this essential multi-targeting requirement.

Adds a concise "Multi-targeting for trimming" section to the trimming documentation that clarifies when libraries need to multi-target to net6.0. If a library targets any framework earlier than net6.0 (such as netstandard2.0 or net472), it should also multi-target to net6.0 to avoid gaps where apps targeting net6.0 or above would consume an older framework asset. Additionally, the documentation now recommends including the latest .NET version (net10.0) to ensure the library is analyzed with the latest analyzer. The section shows how to conditionally enable IsTrimmable using the IsTargetFrameworkCompatible MSBuild function.

<PropertyGroup>
  <TargetFrameworks>netstandard2.0;net6.0;net10.0</TargetFrameworks>
  <IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
</PropertyGroup>

This enables IsTrimmable only for frameworks where the trim analyzer is supported (net6.0+) while avoiding warnings for .NET Standard, .NET Framework, and earlier .NET Core targets. The example demonstrates proper multi-targeting without gaps, ensuring that any app targeting net6.0 or above gets a library asset that supports the trim analyzer, while also benefiting from the latest analyzer improvements.

Additionally, adds a "Multi-targeting for AOT compatibility" subsection to the Native AOT documentation with similar guidance for conditionally enabling IsAotCompatible in multi-targeted projects. Since AOT compatibility analysis was introduced in .NET 8, the AOT section targets net8.0 and also recommends including the latest .NET version:

<PropertyGroup>
  <TargetFrameworks>netstandard2.0;net8.0;net10.0</TargetFrameworks>
  <IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
</PropertyGroup>

Also updates the .NET SDK error list documentation to specify the correct target frameworks in the example code:

  • NETSDK1212 (trimming): Shows net6.0 in the example
  • NETSDK1210 (AOT): Shows net8.0 in the example
Original prompt

This section details on the original issue you should resolve

<issue_title>Prepare .NET libraries for trimming: Add multi-targeting H2 with our recommendations</issue_title>
<issue_description>
See this comment copied below

Finally, recently we've learned that multi-targeting is not only very common, but basically a must have when enabling trimming. (This is based on experience trying to make some NuGets trim compatible). I think the document should have a section about the multi-targeting and our recommendations around it. But the core point is that very few libraries will only target latest TFM. By far the most common case is either library targets netstandard2.0 or it multi-targets net472, netstandard2.0 and net6.0 (or some combination of these).


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Comments on the Issue (you are @copilot in this section)

@sbomer Leaving a note here to mention the new behavior in the `true` part of https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming. @sbomer I'm adding a breaking change notice that mentions multi-targeting: https://github.com//pull/36775. We still need a dedicated section in the general trimming docs though. When that's added, we should also link to it from the breaking change notice.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Prepare .NET libraries for trimming with multi-targeting recommendations Add multi-targeting section to trimming documentation Nov 20, 2025
Copilot finished work on behalf of sbomer November 20, 2025 22:36
Copilot AI requested a review from sbomer November 20, 2025 22:36
Copilot AI requested a review from sbomer December 2, 2025 19:22
Copilot finished work on behalf of sbomer December 2, 2025 19:22
@sbomer sbomer requested a review from a team December 2, 2025 19:39
Copilot AI requested a review from sbomer December 2, 2025 21:13
Copilot finished work on behalf of sbomer December 2, 2025 21:13
Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

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

@copilot Add a similar note for IsAotCompatible (alongside the existing docs for that option).

Copy link
Contributor Author

Copilot AI commented Dec 2, 2025

@copilot Add a similar note for IsAotCompatible (alongside the existing docs for that option).

Added in 78f6612. The note directs users to apply the same multi-targeting guidance when using IsAotCompatible in multi-targeted projects, with a link to the "Multi-targeting for trimming" section.

Copilot AI requested a review from sbomer December 2, 2025 21:51
Copilot finished work on behalf of sbomer December 2, 2025 21:51
Copy link
Contributor Author

Copilot AI commented Dec 2, 2025

@copilot also adjust the warning text from the documentation for ".NET SDK error list" - the trimming one should mention 'net6.0' and the AOT one should mention 'net8.0'.

Updated in e033ba5. NETSDK1212 (trimming) now shows net6.0 and NETSDK1210 (AOT) now shows net8.0 in the example code.

Copilot AI requested a review from sbomer December 2, 2025 22:17
Copilot finished work on behalf of sbomer December 2, 2025 22:17
Copilot finished work on behalf of sbomer December 4, 2025 22:54
Copilot AI requested a review from sbomer December 4, 2025 22:54
Copilot AI requested a review from sbomer December 4, 2025 23:06
Copilot finished work on behalf of sbomer December 4, 2025 23:06
@sbomer sbomer marked this pull request as ready for review December 4, 2025 23:12
@sbomer sbomer requested review from a team and meaghanlewis as code owners December 4, 2025 23:12
Copilot AI review requested due to automatic review settings December 4, 2025 23:12
@sbomer sbomer requested a review from adegeo as a code owner December 4, 2025 23:12
@sbomer sbomer requested a review from a team December 4, 2025 23:12
Copilot finished reviewing on behalf of sbomer December 4, 2025 23:14
Copy link
Contributor

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Contributor

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.


## Multi-targeting for trimming

When preparing libraries for trimming, if your library targets any framework earlier than `net6.0` (such as `netstandard2.0` or `net472`), you should also multi-target to `net6.0` to ensure that apps targeting `net6.0` or above get a version of your library that supports the trim analyzer. Additionally, consider including the latest .NET version to ensure your library is analyzed with the latest analyzer. Use the `IsTargetFrameworkCompatible` MSBuild function to conditionally enable `IsTrimmable` for `net6.0` and above:
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

Missing Oxford comma in the list. According to the Markdown writing style guidelines, you must use the Oxford comma in all lists. Change (such as \netstandard2.0` or `net472`)to(such as `netstandard2.0`, or `net472`)` to include the comma before "or".

Copilot generated this review using guidance from repository custom instructions.

### Multi-targeting for AOT compatibility

When preparing libraries for AOT compatibility, if your library targets any framework earlier than `net8.0` (such as `netstandard2.0` or `net472`), you should also multi-target to `net8.0` to ensure that apps targeting `net8.0` or above get a version of your library that supports the AOT analyzers. Additionally, consider including the latest .NET version to ensure your library is analyzed with the latest analyzer. Use the `IsTargetFrameworkCompatible` MSBuild function to conditionally enable `IsAotCompatible` for `net8.0` and above:
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

Missing Oxford comma in the list. According to the Markdown writing style guidelines, you must use the Oxford comma in all lists. Change (such as \netstandard2.0` or `net472`)to(such as `netstandard2.0`, or `net472`)` to include the comma before "or".

Copilot generated this review using guidance from repository custom instructions.
|NETSDK1208|The target platform identifier {0} was not recognized. This is because MSBuildEnableWorkloadResolver is set to false which disables .NET SDK Workloads which is required for this identifier. Unset this environment variable or MSBuild property to enable workloads.|
|NETSDK1209|The current Visual Studio version does not support targeting {0} {1}. Either target {0} {2} or lower, or use Visual Studio version {3} or higher.|
|NETSDK1210|IsAotCompatible and EnableAotAnalyzer are not supported for the target framework. Consider multi-targeting to a supported framework to enable ahead-of-time compilation analysis, and set IsAotCompatible only for the supported frameworks. For example:<br>`<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', '{0}'))>true</IsAotCompatible>`|
|NETSDK1210|IsAotCompatible and EnableAotAnalyzer are not supported for the target framework. Consider multi-targeting to a supported framework to enable ahead-of-time compilation analysis, and set IsAotCompatible only for the supported frameworks. For example:<br>`<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))>true</IsAotCompatible>`|
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

Missing closing quotation mark in the XML example. The Condition attribute is not properly closed. Change:

<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))>true</IsAotCompatible>

to:

<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>

Note the added " before the > closing the Condition attribute.

Suggested change
|NETSDK1210|IsAotCompatible and EnableAotAnalyzer are not supported for the target framework. Consider multi-targeting to a supported framework to enable ahead-of-time compilation analysis, and set IsAotCompatible only for the supported frameworks. For example:<br>`<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))>true</IsAotCompatible>`|
|NETSDK1210|IsAotCompatible and EnableAotAnalyzer are not supported for the target framework. Consider multi-targeting to a supported framework to enable ahead-of-time compilation analysis, and set IsAotCompatible only for the supported frameworks. For example:<br>`<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>`|

Copilot uses AI. Check for mistakes.
Copy link
Contributor

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prepare .NET libraries for trimming: Add multi-targeting H2 with our recommendations

3 participants