Skip to content

Commit

Permalink
Add note that publishtrimmed enables source generators (dotnet#40731)
Browse files Browse the repository at this point in the history
  • Loading branch information
gewarren authored May 3, 2024
1 parent e212981 commit 606ea35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
46 changes: 20 additions & 26 deletions docs/core/deploying/trimming/trimming-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,22 @@ Trimming with `PublishTrimmed` was introduced in .NET Core 3.0. The other option

- `<PublishTrimmed>true</PublishTrimmed>`

Enable trimming during publish. This also turns off trim-incompatible features and enables [trim analysis](#roslyn-analyzer) during build.
Enable trimming during publish. This setting also turns off trim-incompatible features and enables [trim analysis](#roslyn-analyzer) during build. In .NET 8 and later apps, this setting also enables the configuration binding and request delegate source generators.

> [!NOTE]
> If you specify trimming as enabled from the command line, your debugging experience will differ and you may encounter additional bugs in the final product.
> If you specify trimming as enabled from the command line, your debugging experience will differ and you might encounter additional bugs in the final product.
Place this setting in the project file to ensure that the setting applies during `dotnet build`, not just `dotnet publish`.

:::zone pivot="dotnet-8-0,dotnet-7-0"

This setting enables trimming and will trim all assemblies by default. In .NET 6, only assemblies that opted-in
to trimming via `[AssemblyMetadata("IsTrimmable", "True")]` would be trimmed by default. You can return to the
previous behavior by using `<TrimMode>partial</TrimMode>`.
This setting enables trimming and trims all assemblies by default. In .NET 6, only assemblies that opted-in to trimming via `[AssemblyMetadata("IsTrimmable", "True")]` were trimmed by default. You can return to the previous behavior by using `<TrimMode>partial</TrimMode>`.

:::zone-end

:::zone pivot="dotnet-6-0"

This setting trims any assemblies that have been configured for trimming. With `Microsoft.NET.Sdk` in .NET 6, this includes any assemblies with `[AssemblyMetadata("IsTrimmable", "True")]`, which is the case for the .NET runtime assemblies. In .NET 5, assemblies from the netcoreapp runtime pack are configured for trimming via `<IsTrimmable>` MSBuild metadata. Other SDKs may define different defaults.
This setting trims any assemblies that have been configured for trimming. With `Microsoft.NET.Sdk` in .NET 6, this includes any assemblies with `[AssemblyMetadata("IsTrimmable", "True")]`, which is the case for the .NET runtime assemblies. In .NET 5, assemblies from the netcoreapp runtime pack are configured for trimming via `<IsTrimmable>` MSBuild metadata. Other SDKs might define different defaults.

:::zone-end

Expand Down Expand Up @@ -93,13 +91,9 @@ In .NET 6+, `PublishTrimmed` trims assemblies with the following assembly-level

The framework libraries have this attribute. In .NET 6+, you can also opt in to trimming for a library without this attribute, specifying the assembly by name (without the `.dll` extension).

:::zone-end

:::zone pivot="dotnet-6-0"

## Trimming settings for individual assemblies

When publishing a trimmed app, the SDK computes an `ItemGroup` called `ManagedAssemblyToLink` that represents the set of files to be processed for trimming. `ManagedAssemblyToLink` may have metadata that controls the trimming behavior per assembly. To set this metadata, create a target that runs before the built-in `PrepareForILLink` target. The following example shows how to enable trimming of `MyAssembly`.
When publishing a trimmed app, the SDK computes an `ItemGroup` called `ManagedAssemblyToLink` that represents the set of files to be processed for trimming. `ManagedAssemblyToLink` might have metadata that controls the trimming behavior per assembly. To set this metadata, create a target that runs before the built-in `PrepareForILLink` target. The following example shows how to enable trimming of `MyAssembly`.

```xml
<Target Name="ConfigureTrimming"
Expand All @@ -112,27 +106,27 @@ When publishing a trimmed app, the SDK computes an `ItemGroup` called `ManagedAs
</Target>
```

You can also use this to override the trimming behavior specified by the library author, by setting `<IsTrimmable>false</IsTrimmable>` for an assembly with `[AssemblyMetadata("IsTrimmable", "True"])`.
You can also use this target to override the trimming behavior specified by the library author, by setting `<IsTrimmable>false</IsTrimmable>` for an assembly with `[AssemblyMetadata("IsTrimmable", "True"])`.

Do not add or remove items to/from `ManagedAssemblyToLink`, because the SDK computes this set during publish and expects it not to change. The supported metadata is:
Do not add or remove items from `ManagedAssemblyToLink`, because the SDK computes this set during publish and expects it not to change. The supported metadata is:

- `<IsTrimmable>true</IsTrimmable>`

Control whether the given assembly is trimmed.

- `<TrimMode>copyused</TrimMode>` or `<TrimMode>link</TrimMode>`

Control the [trimming granularity](#trimming-granularity) of this assembly. This takes precedence over the global `TrimMode`. Setting `TrimMode` on an assembly implies `<IsTrimmable>true</IsTrimmable>`.
Control the [trimming granularity](#trimming-granularity) of this assembly. This metadata takes precedence over the global `TrimMode`. Setting `TrimMode` on an assembly implies `<IsTrimmable>true</IsTrimmable>`.

- `<TrimmerSingleWarn>True</TrimmerSingleWarn>` or `<TrimmerSingleWarn>False</TrimmerSingleWarn>`
- `<TrimmerSingleWarn>True</TrimmerSingleWarn>`

Control whether to show [single warnings](#show-detailed-warnings) for this assembly.

:::zone-end

## Root assemblies

If an assembly is not trimmed it is considered "rooted", which means that it and all of its statically understood dependencies will be kept. Additional assemblies may be "rooted" by name (without the `.dll` extension):
If an assembly is not trimmed, it's considered "rooted", which means that it and all of its statically understood dependencies will be kept. Additional assemblies can be "rooted" by name (without the `.dll` extension):

```xml
<ItemGroup>
Expand All @@ -150,7 +144,7 @@ Another way to specify roots for analysis is using an XML file that uses the tri
</ItemGroup>
```

For example, `MyRoots.xml` might root a specific method that is dynamically accessed by the application:
For example, `MyRoots.xml` might root a specific method that's dynamically accessed by the application:

```xml
<linker>
Expand All @@ -168,7 +162,7 @@ For example, `MyRoots.xml` might root a specific method that is dynamically acce

Enable trim analysis warnings.

Trimming removes IL that is not statically reachable. Apps that use reflection or other patterns that create dynamic dependencies may be broken by trimming. To warn about such patterns, set `<SuppressTrimAnalysisWarnings>` to `false`. This will include warnings about the entire app, including your own code, library code, and framework code.
Trimming removes IL that's not statically reachable. Apps that use reflection or other patterns that create dynamic dependencies might be broken by trimming. To warn about such patterns, set `<SuppressTrimAnalysisWarnings>` to `false`. This setting will surface warnings about the entire app, including your own code, library code, and framework code.

## Roslyn analyzer

Expand All @@ -180,11 +174,11 @@ Setting `PublishTrimmed` in .NET 6+ also enables a Roslyn analyzer that shows a

## Suppress warnings

You can suppress individual [warning codes](https://github.com/dotnet/runtime/blob/main/docs/tools/illink/error-codes.md#warning-codes) using the usual MSBuild properties respected by the toolchain, including `NoWarn`, `WarningsAsErrors`, `WarningsNotAsErrors`, and `TreatWarningsAsErrors`. There is an additional option that controls the ILLink warn-as-error behavior independently:
You can suppress individual [warning codes](https://github.com/dotnet/runtime/blob/main/docs/tools/illink/error-codes.md#warning-codes) using the usual MSBuild properties respected by the toolchain, including `NoWarn`, `WarningsAsErrors`, `WarningsNotAsErrors`, and `TreatWarningsAsErrors`. There's an additional option that controls the ILLink warn-as-error behavior independently:

- `<ILLinkTreatWarningsAsErrors>false</ILLinkTreatWarningsAsErrors>`

Don't treat ILLink warnings as errors. This may be useful to avoid turning trim analysis warnings into errors when treating compiler warnings as errors globally.
Don't treat ILLink warnings as errors. This might be useful to avoid turning trim analysis warnings into errors when treating compiler warnings as errors globally.

## Show detailed warnings

Expand All @@ -202,7 +196,7 @@ Symbols are usually trimmed to match the trimmed assemblies. You can also remove

Remove symbols from the trimmed application, including embedded PDBs and separate PDB files. This applies to both the application code and any dependencies that come with symbols.

The SDK also makes it possible to disable debugger support using the property `DebuggerSupport`. When debugger support is disabled, trimming will remove symbols automatically (`TrimmerRemoveSymbols` will default to true).
The SDK also makes it possible to disable debugger support using the property `DebuggerSupport`. When debugger support is disabled, trimming removes symbols automatically (`TrimmerRemoveSymbols` will default to true).

## Trimming framework library features

Expand Down Expand Up @@ -250,13 +244,13 @@ Several feature areas of the framework libraries come with trimmer directives th

- `<UseSystemResourceKeys>true</UseSystemResourceKeys>`

Strip exception messages for `System.*` assemblies. When an exception is thrown from a `System.*` assembly, the message will be a simplified resource ID instead of the full message.
Strip exception messages for `System.*` assemblies. When an exception is thrown from a `System.*` assembly, the message is a simplified resource ID instead of the full message.

These properties cause the related code to be trimmed and also disable features via the [runtimeconfig](../../runtime-config/index.md) file. For more information about these properties, including the corresponding *runtimeconfig* options, see [feature switches](https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/feature-switches.md). Some SDKs may have default values for these properties.
These properties cause the related code to be trimmed and also disable features via the [runtimeconfig](../../runtime-config/index.md) file. For more information about these properties, including the corresponding *runtimeconfig* options, see [feature switches](https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/feature-switches.md). Some SDKs might have default values for these properties.

## Framework features disabled when trimming

The following features are incompatible with trimming because they require code that is not statically referenced. These are disabled by default in trimmed apps.
The following features are incompatible with trimming because they require code that's not statically referenced. These features are disabled by default in trimmed apps.

> [!WARNING]
> Enable these features at your own risk. They are likely to break trimmed apps without extra work to preserve the dynamically referenced code.
Expand All @@ -267,7 +261,7 @@ The following features are incompatible with trimming because they require code

- `<CustomResourceTypesSupport>`

Use of custom resource types is not supported. ResourceManager code paths that use reflection for custom resource types is trimmed.
Use of custom resource types isn't supported. ResourceManager code paths that use reflection for custom resource types are trimmed.

- `<EnableCppCLIHostActivation>`

Expand All @@ -279,4 +273,4 @@ The following features are incompatible with trimming because they require code

- `<StartupHookSupport>`

Running code before `Main` with `DOTNET_STARTUP_HOOKS` is not supported. For more information, see [host startup hook](https://github.com/dotnet/runtime/blob/main/docs/design/features/host-startup-hook.md).
Running code before `Main` with `DOTNET_STARTUP_HOOKS` isn't supported. For more information, see [host startup hook](https://github.com/dotnet/runtime/blob/main/docs/design/features/host-startup-hook.md).
2 changes: 1 addition & 1 deletion docs/core/whats-new/dotnet-8/runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ The COM source generator doesn't support apartment affinity, using the `new` key

The source generator probes for <xref:Microsoft.Extensions.Options.ConfigureOptions%601.Configure(%600)>, <xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Bind%2A>, and <xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Get%2A> calls to retrieve type info from. When the generator is enabled in a project, the compiler implicitly chooses generated methods over the pre-existing reflection-based framework implementations.

No source code changes are needed to use the generator. It's enabled by default in AOT'd web apps. For other project types, the source generator is off by default, but you can opt in by setting the `EnableConfigurationBindingGenerator` property to `true` in your project file:
No source code changes are needed to use the generator. It's enabled by default in AOT-compiled web apps, and when [`PublishTrimmed`](../../deploying/trimming/trimming-options.md#enable-trimming) is set to `true` (.NET 8+ apps). For other project types, the source generator is off by default, but you can opt in by setting the `EnableConfigurationBindingGenerator` property to `true` in your project file:

```xml
<PropertyGroup>
Expand Down

0 comments on commit 606ea35

Please sign in to comment.