Repository navigation
Find redundant package references and version conflicts in the restored graph - #1
Closed
ChrisonSimtian wants to merge 3 commits into
Closed
ChrisonSimtian wants to merge 3 commits into
ChrisonSimtian wants to merge 3 commits into
Conversation
…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
Owner
Author
|
Superseded by dennisdoomen#282, which is the same branch raised upstream. Closing so there is one place to review it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
analyzedoes not ask:PackageReferenceneeded? It is redundant when a referenced project, or another direct package, already provides it.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.
ProjectAssetsReader.cs(226 lines)DotNetLockFileLoaderalready restores-if-stale and returnsNuGet.ProjectModel.LockFileGraphNode,Edge,DirectDependency,AnalyzedProjectLockFileTargetLibrary,PackageDependency,LibraryDependency,LockFileParseMinVersion,NormalizeRange,ParseVersionOrZeroNuGet.VersioningLockFileTargeta package appears once, so the name is the keyWhat survived, and why it matters
Two correctness rules worth not losing:
PrivateAssets="all"orAutoReferencedpackage 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_timecovers it.Tests
9 specs, MSTest + FluentAssertions, Arrange/Act/Assert,
Can_do_somethingnaming, matching the Specs project. They build aLockFilein memory through a smallLockFileBuilder, so no restore runs and they finish in about 120 ms.The 2 failures are pre-existing. I verified both fail on a clean checkout with my changes stashed: one needs
yarnon the PATH, the other isResolves_the_root_component_name_from_the_solution_file_name_without_its_extension.PackageGuard.Corebuilds with zero warnings underTreatWarningsAsErrors, so StyleCop, Roslynator, CSharpGuidelinesAnalyzer and Meziantou are all clean.Still to do before this goes to Dennis
packageguard dependenciesCLI command, as aDependenciesCommand/DependenciesCommandSettingspair, wired throughProjectAnalyzer.OnProjectLockFileLoaded.website/.Two things to look at
Naming.
PackageGuard.Corealready hasNuGetPackageAnalyzer, which fetches per-package metadata from feeds, andProjectAnalyzer, which orchestrates strategies. Mine is a third thing that reads the graph offline. I went withDependencyHygieneAnalyzer, but "hygiene" is a new word in that codebase.AcceptApiChanges.shdoes the wrong thing on .NET 10. Verify writesnet8.0.DotNet10_0.received.txtwhile the approved file isnet8.0.verified.txt, so the script'ss/received/verified/would create a new runtime-specific file and leave the real one stale. I updatednet8.0.verified.txtby hand instead. The diff is exactly my 26 lines. Possibly worth telling him separately.