You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+26-6Lines changed: 26 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,12 +10,12 @@ The following warnings are generated by this package:
10
10
| Id | Description |
11
11
|--------|-------------|
12
12
| RT0000 | Enable documentation generation for accuracy of used references detection |
13
-
| RT0001 | Unnecessary reference |
13
+
| RT0001 | Unnecessary reference |
14
14
| RT0002 | Unnecessary project reference |
15
15
| RT0003 | Unnecessary package reference |
16
16
17
17
## How to use
18
-
Add a package reference to the [ReferenceTrimmer](https://www.nuget.org/packages/ReferenceTrimmer) package in your projects, or as a common package reference in the repo's [`Directory.Packages.props`](./Directory.Build.props).
18
+
Add a package reference to the [ReferenceTrimmer](https://www.nuget.org/packages/ReferenceTrimmer) package in your projects, or as a common package reference in the repo's `Directory.Packages.props`.
19
19
20
20
If you're using [Central Package Management](https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management), you can use it as a `GlobalPackageReference` in your `Directory.Packages.props` to apply it to the entire repo.
21
21
@@ -54,15 +54,27 @@ You can turn off specific docxml related warnings and errors while defaulting Re
54
54
55
55
Note: To get better results, enable the [`IDE0005`](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0005) unnecessary `using` rule. This avoids the C# compiler seeing a false positive assembly usage from unneeded `using` directives causing it to miss a removable dependency. See also the note for why IDE0005 code analysis rule requires `<GenerateDocumentationFile>` property to be enabled. Documentation generation is also required for accuracy of used references detection (based on https://github.com/dotnet/roslyn/issues/66188).
56
56
57
-
Packages are prevented from being trimmed if they contain build files, as there's no reliable way to determine whether it's safe to do so. You can explicitly ignore specific packages' build files and cause them to be solely judged based on whther the libraries are used by adding the package to the `ReferenceTrimmerIgnorePackageBuildFiles` item on a project level, or in `Directory.Build.props`.
57
+
#### What makes a reference non-trimmable?
58
+
59
+
ReferenceTrimmer automatically skips certain references that cannot be reliably evaluated. These are not reported as unused even if the compiler does not directly use them:
60
+
61
+
-**Implicitly defined (SDK-provided) references** - References added by the SDK (e.g. `System.Runtime`) are not under your control and are skipped.
62
+
-**Target framework assemblies** - Assemblies that are part of the target framework itself (e.g. from `FrameworkList.xml`) are skipped, since they may have been substituted in during package conflict resolution and may not be directly removable.
63
+
-**References originating from NuGet package build logic** - If a `Reference` item resolves to a path under the NuGet package root, it was likely injected by a package's `.props` or `.targets` file rather than declared by you directly, so it is skipped.
64
+
-**Transitive project references** - Project references that are indirect (transitive) dependencies, determined by NuGet restore, are skipped because you don't have direct control over them.
65
+
-**Packages with build files** - NuGet packages that ship `.props` or `.targets` files in their `build/` folder can influence the build in ways that go beyond providing compile-time assemblies. For example, they might define custom MSBuild properties, targets, items, or code generators. Because there is no reliable way to determine whether these build-time contributions are still needed, ReferenceTrimmer skips these packages by default.
66
+
67
+
#### `ReferenceTrimmerIgnorePackageBuildFiles`
68
+
69
+
Many commonly used packages ship build files that are inconsequential - they don't materially affect the build and only prevent the package from being evaluated for trimming. You can tell ReferenceTrimmer to ignore a package's build files and evaluate it solely based on whether its compile-time assemblies are used. Add the package to the `ReferenceTrimmerIgnorePackageBuildFiles` item in your project file or `Directory.Build.props`:
A default set of `ReferenceTrimmerIgnorePackageBuildFiles` items is already included for well-known package, notably various `Microsoft.Extensions.*` packages.
76
+
77
+
A default set of `ReferenceTrimmerIgnorePackageBuildFiles` items is already included for well-known packages, notably various `Microsoft.Extensions.*` packages. See the full list in the [ReferenceTrimmer.props](./src/Package/build/ReferenceTrimmer.props) file.
66
78
67
79
### C++ (.vcxproj projects)
68
80
ReferenceTrimmer for C++ is an MSBuild [distributed logger](https://learn.microsoft.com/en-us/visualstudio/msbuild/logging-in-a-multi-processor-environment?view=vs-2022). It writes guidance to the MSBuild stdout stream (not to the MSBuild internal logger at this time) and writes `ReferenceTrimmerUnusedMSVCLibraries.json.log` to the build working directory.
@@ -115,17 +127,25 @@ If you find an unused .lib that is created by a .vcxproj in your repo, you shoul
115
127
If you find an unused .lib that is from a package, remove the reference to that .lib from your project to speed up linking.
116
128
117
129
## Disabling a rule on a reference
118
-
To turn off a rule on a specific project or package reference, add the relevant RTxxxx code to a NoWarn metadata attribute. For example:
130
+
To turn off a rule on a specific project or package reference, add the relevant RTxxxx code to a `NoWarn` metadata attribute. For example:
`$(EnableReferenceTrimmer)` - Controls whether the build logic should run for a given project. Defaults to `true`.
126
144
127
145
`$(ReferenceTrimmerEnableVcxproj)` - Controls whether MSVC link flags are set up to print out unused libraries and delay-load DLLs. Defaults to `true`.
128
146
147
+
`$(EnableReferenceTrimmerDiagnostics)` - When set to `true`, writes used and unused reference lists to the intermediate output directory for debugging. Defaults to `false`.
0 commit comments