Skip to content

NuGet: Remove redundant GetPackageGraphForDependencies and use discovery DependencyGraph#15122

Merged
brettfo merged 2 commits into
mainfrom
dev/brettfo/nuget-duplicate-package-graph
May 22, 2026
Merged

NuGet: Remove redundant GetPackageGraphForDependencies and use discovery DependencyGraph#15122
brettfo merged 2 commits into
mainfrom
dev/brettfo/nuget-duplicate-package-graph

Conversation

@brettfo
Copy link
Copy Markdown
Contributor

@brettfo brettfo commented May 22, 2026

Now that project discovery generates a package graph, this PR removes duplicate code and instead uses the existing package graph to determine what update operations were performed. One package parent test was moved to be a discovery test since that's where the bulk of the work now happens.


Replace the GetPackageGraphForDependencies method (which created a temp project, ran dotnet restore, and parsed project.assets.json) with BuildReverseGraph, a pure in-memory function that derives the same (PackageParents, PackageVersions) data from the discovery's existing DependencyGraph property.

Changes

  • PackageReferenceUpdater.cs: Removed GetPackageGraphForDependencies and replaced with BuildReverseGraph which converts the forward dependency graph (already available from project discovery) into the reverse parent/version lookup. ComputeUpdateOperations is now synchronous and no longer performs I/O.
  • FileWriterWorker.cs: Updated the call site to pass finalProjectDiscovery.DependencyGraph directly.
  • PackageReferenceUpdaterTests.cs: Replaced the integration test with a unit test for BuildReverseGraph and updated ComputeUpdateOperations tests to supply an explicit dependency graph.
  • DiscoveryWorkerTests.cs: Added a new discovery test (TestDependencyGraphWithDifferentTargetFrameworks) that verifies the dependency graph is correctly populated when a project targets a different-but-compatible TFM (e.g., net10.0-android) from its packages.

Copilot AI review requested due to automatic review settings May 22, 2026 19:09
@brettfo brettfo requested a review from a team as a code owner May 22, 2026 19:09
@github-actions github-actions Bot added the L: dotnet:nuget NuGet packages via nuget or dotnet label May 22, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the NuGet updater’s update-operation attribution logic by removing the redundant GetPackageGraphForDependencies (temp project + dotnet restore + project.assets.json parsing) and instead deriving parent/version lookup data from the already-discovered in-memory DependencyGraph, making ComputeUpdateOperations synchronous and I/O-free.

Changes:

  • Replace restore-based package graph generation with BuildReverseGraph(dependencyGraph) in PackageReferenceUpdater.
  • Update FileWriterWorker to pass finalProjectDiscovery.DependencyGraph into ComputeUpdateOperations.
  • Rewrite/update tests to unit-test BuildReverseGraph and to validate DependencyGraph discovery across compatible TFMs.
Show a summary per file
File Description
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs Removes restore-based graph generation and builds reverse-lookup structures from discovery DependencyGraph.
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/FileWriters/FileWriterWorker.cs Adjusts call site for new synchronous ComputeUpdateOperations signature and supplies DependencyGraph.
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/PackageReferenceUpdaterTests.cs Replaces integration-style graph test with a unit test for BuildReverseGraph and updates operation tests to inject a graph.
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.cs Adds coverage ensuring discovery populates DependencyGraph when project/package TFMs differ but remain compatible.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 4

@brettfo brettfo force-pushed the dev/brettfo/nuget-duplicate-package-graph branch from 868a7fd to 65c7894 Compare May 22, 2026 19:17
brettfo and others added 2 commits May 22, 2026 15:57
…endencyGraph

Replace the GetPackageGraphForDependencies method (which created a temp
project, ran dotnet restore, and parsed project.assets.json) with
BuildReverseGraph, a pure in-memory function that derives the same
(PackageParents, PackageVersions) data from the discovery's existing
DependencyGraph property.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…graph-derived versions

The dependency graph can contain multiple versions of the same package
name (from multi-TFM merging), making a name-keyed version lookup
ambiguous. Instead, use the per-TFM resolvedVersions which are already
available and unambiguous. Remove PackageVersions from BuildReverseGraph
since it is no longer needed.

Also adds a test exercising the multi-version-per-package scenario.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@brettfo brettfo force-pushed the dev/brettfo/nuget-duplicate-package-graph branch from 298b274 to bf7c50b Compare May 22, 2026 21:57
@brettfo brettfo merged commit 1d785ff into main May 22, 2026
106 checks passed
@brettfo brettfo deleted the dev/brettfo/nuget-duplicate-package-graph branch May 22, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L: dotnet:nuget NuGet packages via nuget or dotnet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants