Skip to content

Find redundant package references and version conflicts in the restored graph - #1

Closed
ChrisonSimtian wants to merge 3 commits into
mainfrom
feature/dependency-hygiene-analyzer
Closed

ChrisonSimtian wants to merge 3 commits into
mainfrom
feature/dependency-hygiene-analyzer

Conversation

@ChrisonSimtian

Copy link
Copy Markdown
Owner

Review PR, raised against our own fork first. Not for Dennis yet.

This is the port of the analyzer from the closed Fallout PR #423, reshaped onto PackageGuard's existing model. The matching API proposal issue is drafted but not filed, so this is the thing to argue with before either goes upstream.

What this is

The analysis engine only. It answers two questions analyze does not ask:

  • Is this PackageReference needed? It is redundant when a referenced project, or another direct package, already provides it.
  • Does one package resolve to different versions across projects?

How much we dropped

The whole point. #423 was +2076 across 18 files. This slice is +553 across 6 files, because PackageGuard already does the hard half.

#423 fate
ProjectAssetsReader.cs (226 lines) deleted — DotNetLockFileLoader already restores-if-stale and returns NuGet.ProjectModel.LockFile
GraphNode, Edge, DirectDependency, AnalyzedProject deleted — LockFileTargetLibrary, PackageDependency, LibraryDependency, LockFile
ParseMinVersion, NormalizeRange, ParseVersionOrZero deleted — NuGet.Versioning
name-index / node-key indirection deleted — within one LockFileTarget a package appears once, so the name is the key
the three finding kinds and their rules kept, this is the actual value

What survived, and why it matters

Two correctness rules worth not losing:

  1. A PrivateAssets="all" or AutoReferenced package is never a provider, and is never reported. It is not a dependency of what the project ships, so removing another reference because it pulls it in would drop a shipped dependency. This was a real false positive on the first run against a production solution. Ignores_a_package_that_is_only_there_at_build_time covers it.
  2. Safe to remove means the resolved version does not change. When the project asks for a higher version than any transitive requester, the finding says so and names the version it would drop to.

Tests

9 specs, MSTest + FluentAssertions, Arrange/Act/Assert, Can_do_something naming, matching the Specs project. They build a LockFile in memory through a small LockFileBuilder, so no restore runs and they finish in about 120 ms.

Passed! - Failed: 0, Passed: 9  (dependency specs)
Passed! - Failed: 2, Passed: 441, Skipped: 3  (full suite)

The 2 failures are pre-existing. I verified both fail on a clean checkout with my changes stashed: one needs yarn on the PATH, the other is Resolves_the_root_component_name_from_the_solution_file_name_without_its_extension.

PackageGuard.Core builds with zero warnings under TreatWarningsAsErrors, so StyleCop, Roslynator, CSharpGuidelinesAnalyzer and Meziantou are all clean.

Still to do before this goes to Dennis

  • The packageguard dependencies CLI command, as a DependenciesCommand / DependenciesCommandSettings pair, wired through ProjectAnalyzer.OnProjectLockFileLoaded.
  • Docs under website/.
  • File the API proposal issue, and let him pick the command shape and the naming before we commit to either.

Two things to look at

Naming. PackageGuard.Core already has NuGetPackageAnalyzer, which fetches per-package metadata from feeds, and ProjectAnalyzer, which orchestrates strategies. Mine is a third thing that reads the graph offline. I went with DependencyHygieneAnalyzer, but "hygiene" is a new word in that codebase.

AcceptApiChanges.sh does the wrong thing on .NET 10. Verify writes net8.0.DotNet10_0.received.txt while the approved file is net8.0.verified.txt, so the script's s/received/verified/ would create a new runtime-specific file and leave the real one stale. I updated net8.0.verified.txt by hand instead. The diff is exactly my 26 lines. Possibly worth telling him separately.

ChrisonSimtian and others added 3 commits October 8, 2026 21:53
…ed graph

Adds the analysis engine behind the dependency hygiene feature. It answers two
questions the existing analysis does not ask: is this PackageReference needed,
and does one package resolve to different versions across projects.

It works off the LockFile that DotNetLockFileLoader already produces, so it adds
no new input path, runs no second restore and never touches the network.

Two correctness rules carried over from the original implementation:

A package marked PrivateAssets="all" or AutoReferenced is never treated as the
thing that provides another reference, and is never reported itself. It is not a
dependency of what the project ships, so removing the other reference would drop
a shipped dependency. A spec covers it.

A reference is only safe to remove when removing it keeps the resolved version.
When the project asks for a higher version than any transitive requester, the
finding says so and reports the version it would drop to.

Version conflicts are grouped per target framework, so a solution that resolves
differently per framework is not reported as a conflict.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TK6dHqkZwhjWUrZNnnb8MG
Two things were awkward rather than wrong.

FindProviders was handed a memoization dictionary that the calling method created,
so a mutable cache was threaded by hand between two methods. The reachable set of
a provider belongs to the graph, not to the caller, so ResolvedTarget now owns both
the walk and its cache and exposes FindProvidersOf. The analyzer asks a question
instead of driving the traversal.

Version conflict detection built a Dictionary of SortedDictionary of SortedSet by
hand, which needed a GetOrAdd extension method to stay readable. Grouping expresses
the same thing directly, so both the nested mutable lookups and the extension class
are gone.

No behaviour change. The nine specs pass unchanged, and the public API is identical.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TK6dHqkZwhjWUrZNnnb8MG
Review of the previous two commits turned up four things.

DependencyFinding.Providers held the providing projects and packages on a
redundancy, but the conflicting versions on a version conflict. One property
meaning two unrelated things is hard to use and hard to document, so conflicts
now report ConflictingVersions and leave Providers empty.

Analyze is public, so a caller can hand it any lock file. One without a project
section made it dereference a null PackageSpec. Such a lock file declares no
direct dependencies, so there is nothing to analyze and it is now skipped. A spec
covers it.

NameEquals was left behind when the provider lookup moved to ResolvedTarget, and
the NuGet.Frameworks using went with it. Both are removed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TK6dHqkZwhjWUrZNnnb8MG
@ChrisonSimtian

Copy link
Copy Markdown
Owner Author

Superseded by dennisdoomen#282, which is the same branch raised upstream. Closing so there is one place to review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant