Determine Unused `Directory.Packages.props` declarations via Nushell

3 months ago by Kissaki to c/dotnet

When using Central Package Management (Directory.Packages.props holds package versions), package version declarations can remain after package references have been removed. The package versions are unused and misleading.

dotnet nuget why can be used to get a dependency tree across projects to answer how a package comes into the dependency tree. However, the extensive tree list result for a list of version declarations is not viable to simply determine what 'is in use' and what isn't.

I am sharing my Nushell snippet which answers the question of what is declared but not referenced:

# List Directory.Packages.props version declarations that are in no csproj
def "dotnet unused-dirpackprops" [] {
  let defs = open Directory.Packages.props | from xml | get content | where tag == ItemGroup | get content.attributes.Include | flatten | sort --ignore-case --natural
  let refs = ls **/*.csproj | get name | each { open | from xml | get content | select content | flatten | flatten | where tag == PackageReference | get attributes.Include } | flatten | uniq | sort --ignore-case --natural
  $defs | difference $refs
}
❯ dotnet unused-dirpackprops
╭───┬─────────────────────────────────────────╮
│ 0 │ coverlet.collector                      │
│ 1 │ System.Net.Http                         │
│ 2 │ System.ServiceProcess.ServiceController │
╰───┴─────────────────────────────────────────╯

Gaps: In this simple form, only covers the standard csproj package references. Package references included during build through other target or prop files are not covered (probably irrelevant for most users).

dotnet
dotnet

@programming.dev

login for more options
1884
591
13

Getting started

Useful resources

IDEs and code editors

Tools

Rules

  • Rule 1: Follow Lemmy rules
  • Rule 2: Be excellent to each other, no hostility towards users for any reason
  • Rule 3: No spam of tools/companies/advertisements
  • Rule 4: Submit the original source. If a post reports on something found on another site, submit the latter

Related communities

Wikipedia pages

go to feed...