Skip to content

Commit 824f3ec

Browse files
authored
Update readme (#130)
1 parent 9922312 commit 824f3ec

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ The following warnings are generated by this package:
1010
| Id | Description |
1111
|--------|-------------|
1212
| RT0000 | Enable documentation generation for accuracy of used references detection |
13-
| RT0001 | Unnecessary reference |
13+
| RT0001 | Unnecessary reference |
1414
| RT0002 | Unnecessary project reference |
1515
| RT0003 | Unnecessary package reference |
1616

1717
## 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`.
1919

2020
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.
2121

@@ -54,15 +54,27 @@ You can turn off specific docxml related warnings and errors while defaulting Re
5454

5555
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).
5656

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`:
5870

59-
Example:
6071
```xml
6172
<ItemGroup>
6273
<ReferenceTrimmerIgnorePackageBuildFiles Include="Microsoft.Extensions.Logging.Abstractions" />
6374
</ItemGroup>
6475
```
65-
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.
6678

6779
### C++ (.vcxproj projects)
6880
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
115127
If you find an unused .lib that is from a package, remove the reference to that .lib from your project to speed up linking.
116128

117129
## 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:
119131

120132
```xml
121133
<ProjectReference Include="../Other/Project.csproj" NoWarn="RT0002" />
122134
```
123135

136+
Alternatively, you can add `TreatAsUsed` metadata to suppress all ReferenceTrimmer warnings for that reference:
137+
138+
```xml
139+
<PackageReference Include="SomePackage" TreatAsUsed="true" />
140+
```
141+
124142
## Configuration
125143
`$(EnableReferenceTrimmer)` - Controls whether the build logic should run for a given project. Defaults to `true`.
126144

127145
`$(ReferenceTrimmerEnableVcxproj)` - Controls whether MSVC link flags are set up to print out unused libraries and delay-load DLLs. Defaults to `true`.
128146

147+
`$(EnableReferenceTrimmerDiagnostics)` - When set to `true`, writes used and unused reference lists to the intermediate output directory for debugging. Defaults to `false`.
148+
129149
## How does it work?
130150

131151
### C#

0 commit comments

Comments
 (0)