Skip to content

Commit 4880fee

Browse files
Add trimming guidance for developers (#40902)
Co-authored-by: Aditya Mandaleeka <[email protected]>
1 parent 1d38f87 commit 4880fee

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ The table below outlines the different docs in this folder and what they are hel
2525
| Submodules | Documentation on working with submodules in Git | Anyone working with submodules in the repo |
2626
| [Triage process](TriageProcess.md)| Overview of the issue triage process used in the repo | Anyone looking to understand the triage process on the repo |
2727
| [Updating Major Version & TFM](UpdatingMajorVersionAndTFM.md)| Instructions for updating the repo branding & TFM in preparation for a new major release | Repo developers who want to know more about our branding & release process |
28+
| [Assembly trimming guide](Trimming.md)| Guidance on adding trimming support to an ASP.NET Core assembly | Repo developers who want to help add support for trimming to ASP.NET Core |

docs/Trimming.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Guide to trimming ASP.NET Core
2+
3+
This guide discusses the steps required to enable, annotate, and verify trimming in ASP.NET Core assemblies. An assembly that supports trimming knows all the types and members it needs at runtime. Unused code is removed when an app is published, reducing app size. Trimming is also a precursor to supporting AOT, which requires knowledge of the APIs used by an app on build.
4+
5+
[Trim self-contained deployments and executables](https://docs.microsoft.com/dotnet/core/deploying/trimming/trim-self-contained) has general documentation for trimming. It includes:
6+
7+
* How to output trim warnings. Typically done by publishing an app.
8+
* How to react to trim warnings. Attributes can be placed on APIs to provide type info, suppress warnings, or verify that an API is incompatible with trimming.
9+
* How to trim libraries.
10+
11+
## Assembly trimming order
12+
13+
Trim assemblies from the bottom up. Order is important because annotating an assembly impacts its dependents and their annotations. Annotating from the bottom up reduces churn.
14+
15+
For example, `Microsoft.AspNetCore.Http` depends on `Microsoft.AspNetCore.Http.Abstractions` so `Microsoft.AspNetCore.Http.Abstractions` should be annotated first.
16+
17+
## Trim an ASP.NET Core assembly
18+
19+
The first step to trimming an ASP.NET Core assembly is adding it to `LinkabilityChecker`. `LinkabilityChecker` is a tool in the ASP.NET Core repo that runs ILLink on its referenced assemblies and outputs trim warnings.
20+
21+
1. Update the project file to enable trimming by adding `<IsTrimmable>true</IsTrimmable>`. This property configures the build to add metadata to the assembly to indicate it supports trimming. It also enables trimming analysis on the project.
22+
2. Run `eng/scripts/GenerateProjectList.ps1` to update the list of projects that are known to be trimmable. The script adds the project to the list of known trimmable projects at [`eng/TrimmableProjects.props`](../eng/TrimmableProjects.props).
23+
3. Open `Tools.slnf`
24+
4. Add the project to `Tools.slnf`.
25+
1. Right-click `LinkabilityCheck` and select *Load Direct Dependencies*.
26+
2. Update the solution filter.
27+
5. Build `LinkabilityChecker`.
28+
29+
`LinkabilityChecker` is required to get a complete list of trim warnings because there isn't enough type information when building an assembly on its own. It's possible to introduce new trim warnings during typical dev work after annotating an assembly for trimming. `LinkabilityChecker` automatically runs on the build server and catches new warnings.
30+
31+
## Fix trim warnings
32+
33+
[Introduction to trim warnings](https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming/fixing-warnings) and [Prepare .NET libraries for trimming](https://docs.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming) discuss how to fix trim warnings. There is also a complete list of all the trim warnings with some more detail.
34+
35+
## Updating the baselines
36+
37+
If a suppressed warning has been resolved, or if new trimmer warnings are to be baselined, run the following command:
38+
39+
```
40+
dotnet build /p:GenerateLinkerWarningSuppressions=true
41+
```
42+
43+
This should update the `WarningSuppressions.xml` files associated with projects.
44+
45+
⚠️ Note that the generated file sometimes messes up formatting for some compiler generated nested types and you may need to manually touch up these files on regenerating. The generated file uses braces `{...}` instead of angle brackets `<...>`:
46+
47+
```diff
48+
- LegacyRouteTableFactory.&lt;&gt;c.{Create}b__2_1(System.Reflection.Assembly)
49+
+ LegacyRouteTableFactory.&lt;&gt;c.&lt;Create&gt;b__2_1(System.Reflection.Assembly)
50+
```
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
1-
## Trimmer baseline verification
1+
# Trimmer baseline verification
22

33
This project is used to verify that ASP.NET Core APIs do not result in additional trimmer warnings other than the ones that are already known. It works by running the trimmer in "library mode", rooting all of it's public APIs, using a set of baselined suppressions and ensuring no new trimmer warnings are produced.
44

5-
### Enabling trimming on a new project
5+
## Enabling trimming on a new project
66

7-
* Update the project file to enable trimming `<Trimmable>true</Trimmable>
8-
* Run `eng/scripts/GenerateProjectList.ps1` to update the list of projects that are known to be trimmable
9-
* Build this project
10-
11-
### Updating the baselines
12-
13-
If a suppressed warning has been resolved, or if new trimmer warnings are to be baselined, run the following command:
14-
15-
```
16-
dotnet build /p:GenerateLinkerWarningSuppressions=true
17-
```
18-
19-
This should update the WarningSuppressions.xml files associated with projects.
20-
21-
⚠️ Note that the generated file sometimes messing up formatting for some compiler generated nested types and you may need to manually touch up these files on regenerating. The generated file uses braces `{...}` instead of angle brackets `<...>`:
22-
23-
```diff
24-
- LegacyRouteTableFactory.&lt;&gt;c.{Create}b__2_1(System.Reflection.Assembly)
25-
+ LegacyRouteTableFactory.&lt;&gt;c.&lt;Create&gt;b__2_1(System.Reflection.Assembly)
26-
```
7+
For more information about how this tool can be used to add trimming support to ASP.NET Core, see [docs/Trimming.md](../../../docs/Trimming.md).

src/Tools/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ This folder also contains the infrastructure for our partners' service reference
2525
- [dotnet-getdocument](dotnet-getdocument/README.md) the outside man of OpenAPI document generation tool.
2626
- [GetDocument.Insider](GetDocumentInsider/README.md) the inside man of OpenAPI document generation tool.
2727

28+
## Internal tools
29+
30+
The following tools support the internal development of ASP.NET Core. They aren't designed to be used outside of this repository.
31+
32+
- [LinkabilityChecker](LinkabilityChecker/README.md) for testing and validating trimming of ASP.NET Core assemblies.
33+
2834
## Usage
2935

3036
The command line tools can be invoked as a subcommand of `dotnet`.

0 commit comments

Comments
 (0)