ReferenceTrimmer 3.1.23

There is a newer version of this package available.
See the version list below for details.
dotnet add package ReferenceTrimmer --version 3.1.23                
NuGet\Install-Package ReferenceTrimmer -Version 3.1.23                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ReferenceTrimmer" Version="3.1.23">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReferenceTrimmer --version 3.1.23                
#r "nuget: ReferenceTrimmer, 3.1.23"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ReferenceTrimmer as a Cake Addin
#addin nuget:?package=ReferenceTrimmer&version=3.1.23

// Install ReferenceTrimmer as a Cake Tool
#tool nuget:?package=ReferenceTrimmer&version=3.1.23                

ReferenceTrimmer

NuGet Version NuGet Downloads

Easily identify which dependencies can be removed from an MSBuild project.

How to use

Add a package reference to the ReferenceTrimmer package in your projects. The package contains build logic to emit warnings when unused dependencies are detected.

If you're using Central Package Management, you can it as a GlobalPackageReference in your Directory.Packages.props to apply it to the entire repo.

  <ItemGroup>
    <GlobalPackageReference Include="ReferenceTrimmer" Version="{SomeVersion}" />
  </ItemGroup>

Alternately, you can add a PackageReference to your Directory.Build.props or Directory.Build.targets to apply to the entire repo.

You'll need to enable C# documentation XML generation to ensure good analysis results. If your repo is not already using docxml globally, this can introduce a large number of errors and warnings specific to docxml. Additionally, turning on docxml adds additional output I/O that can slow down large repos. You can turn off specific docxml related warnings and errors while defaulting ReferenceTrimmer to off using a block of code like this in your Directory.Build.props. Turn on the ReferenceTrimmer build by setting /p:EnableReferenceTrimmer=true on the MSBuild command line or setting the same property value as an environment variable. You could create a separate build pipeline for your repo to run ReferenceTrimmer builds.

  
  <PropertyGroup Label="ReferenceTrimmer">
    <EnableReferenceTrimmer Condition=" '$(EnableReferenceTrimmer)' == '' ">false</EnableReferenceTrimmer>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(EnableReferenceTrimmer)' == 'true' and '$(GenerateDocumentationFile)' != 'true' " Label="ReferenceTrimmer">
    
    <GenerateDocumentationFile>true</GenerateDocumentationFile>

    
    <NoWarn>$(NoWarn);419;1570;1573;1574;1584;1591;SA1602</NoWarn>
  </PropertyGroup>

Note: To get better results, enable the 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).

Configuration

$(EnableReferenceTrimmer) - Controls whether the build logic should run for a given project. Defaults to true.

Rules

Id Description
RT0000 Enable documentation generation for accuracy of used references detection
RT0001 Unnecessary reference
RT0002 Unnecessary project reference
RT0003 Unnecessary package reference

How does it work?

There are two main pieces to the package. First there is an MSBuild task which collects all refernces passed to the compiler. There is also a Roslyn Analyzer which uses the GetUsedAssemblyReferences analyzer API which is available starting with Roslyn compiler that shipped with Visual Studio 2019 version 16.10, .NET 5. (see https://github.com/dotnet/roslyn/blob/main/docs/wiki/NuGet-packages.md#versioning). This is the compiler telling us exactly what references were needed as part of compilation. The analyzer then compares the set of references the Task gathered with the references the compiler says were used.

Future development

The outcome of https://github.com/dotnet/sdk/issues/10414 may be of use for ReferenceTrimmer future updates.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on ReferenceTrimmer:

Repository Stars
dfederm/ReferenceTrimmer
Easily identify which dependencies can be removed from a .NET project
Version Downloads Last updated
3.3.6 63,275 4/1/2024
3.3.3 3,009 3/27/2024
3.3.2 459 3/23/2024
3.3.1 1,718 3/8/2024
3.3.0 203 3/6/2024
3.2.4 15,075 2/2/2024
3.2.3 661 11/1/2023
3.2.0 829 10/2/2023
3.1.26 437 9/19/2023
3.1.23 246 9/15/2023
3.1.22 390 9/9/2023
3.1.17 1,286 6/7/2023
3.1.16 400 3/8/2023
3.1.15 328 2/24/2023
3.1.12 548 2/3/2023
3.1.11 912 1/28/2023
3.1.10 379 1/25/2023
3.1.8 389 1/21/2023
3.1.7 312 1/19/2023
3.1.5 362 1/18/2023
3.1.4 353 1/17/2023
3.1.2 356 1/12/2023
3.1.0 360 1/5/2023
3.0.8 485 10/11/2022
3.0.7 433 10/11/2022
3.0.5 438 9/9/2022
3.0.4-g961fd51237 196 9/9/2022
2.0.0 3,167 10/5/2018