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

Breaking change for trimming in .NET 7 Preview 7 #30401

Closed
agocke opened this issue Jul 28, 2022 · 2 comments · Fixed by #30408
Closed

Breaking change for trimming in .NET 7 Preview 7 #30401

agocke opened this issue Jul 28, 2022 · 2 comments · Fixed by #30408
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 7 Work items for the .NET 7 release

Comments

@agocke
Copy link
Member

agocke commented Jul 28, 2022

Breaking change notice

In .NET 7, trimming will trim all assemblies in the app by default. This breaking change only affects apps which are published with PublishTrimmed=true, and it only affects apps with trim warnings.

Description

This is a behavior change for console apps (i.e., plain .NET core apps that don't use WindowsDesktop, Android, iOS, WASM, or ASP.NET SDKs). The change is that trimming will now trim all the assemblies in the app by default, as opposed to the pre-.NET 7 behavior, which only trimmed assemblies that had opted-in with <IsTrimmable>true</IsTrimmable> in the library project file.

Effects

Apps which may have previously worked with PublishTrimmed may not in .NET 7. Only apps with trim warnings may be affected. If your app has no trim warnings, the change in behavior should not cause any adverse affects, and will likely decrease the app size.

Example

An app which uses Newtonsoft.Json or System.Text.Json without source generation to serialize and deserialize a type in the user project may function before the change because types in the user project were fully preserved. One or more trim warnings (warning codes ILxxxx) would have been present.

After the change, types in the user project will be trimmed and serialization may fail or produce unexpected results.

Fixes

The best fix is to resolve all trim warnings in your application. For your own libraries, that means resolving the warnings yourself (see https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming/fixing-warnings for more information). For other libraries, you may contact the author to request that they resolve the warnings, or you may choose a different library which already supports trimming.

For instance, instead of Newtonsoft.Json you could choose System.Text.Json with source generation, which supports trimming.

Workaround

To go back to the previous behavior, use the TrimMode property. The pre-.NET 7 behavior is

<TrimMode>partial</TrimMode>

The .NET 7+ behavior is

<TrimMode>full</TrimMode>
@PRMerger7 PRMerger7 added the Pri3 label Jul 28, 2022
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Jul 28, 2022
@gewarren gewarren added breaking-change Indicates a .NET Core breaking change and removed ⌚ Not Triaged Not triaged Pri3 labels Jul 28, 2022
@gewarren gewarren self-assigned this Jul 28, 2022
@gewarren gewarren added the 🏁 Release: .NET 7 Work items for the .NET 7 release label Jul 28, 2022
@gewarren gewarren added Pri1 and removed Pri3 labels Jul 28, 2022
@gewarren
Copy link
Contributor

@agocke This section doesn't list full and partial as options for TrimMode. Does it need to be updated?

https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options#trimming-granularity

@agocke
Copy link
Member Author

agocke commented Jul 28, 2022

It's in PR here: #30279

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 7 Work items for the .NET 7 release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants