Skip Navigation

User banner
.NET

Members
0
Posts
23
Created
3 yr. ago

Determine Unused Directory.Packages.props declarations via Nushell

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:

 nushell
# 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
}
 nushell
โฏ 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).

I built a small library to assert EF Core SQL query counts in integration tests (catch N+1 in 3 lines)

I kept running into the same issue when writing integration tests for ASP.NET Core APIs โ€” thereโ€™s no clean way to assert how many SQL queries an endpoint executes. Most of the time it ends up being: โ€ข custom DbCommandInterceptor โ€ข wiring it into WebApplicationFactory โ€ข manually counting queries So I wrapped that into a small library. Example:

 
await using var guard = factory.TrackQueries<Program, AppDbContext>();

var client = guard.CreateClient();

await client.GetAsync("/api/orders");

guard.AssertCount(exact: 1);

Thatโ€™s it โ€” no manual interceptor or log parsing. What it does: โ€ข Counts SELECT/INSERT/UPDATE/DELETE queries triggered by HTTP requests โ€ข Starts counting from CreateClient() (so startup/seeding queries are ignored) โ€ข Works with any EF Core provider โ€ข Designed for WebApplicationFactory-based integration tests GitHub: https://github.com/KiwiDevelopment/KiwiQuery NuGet: dotnet add package KiwiQuery.EFCore MIT, very small codebase. Would love feedback โ€” especially if youโ€™re already solving this in a different way.