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).
no comments (yet)