Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

Overview

This PR implements support for resolving multiple NuGet package references in a single call, with automatic version conflict resolution following NuGet's standard resolution rules. This addresses the issue request to support resolving multiple references efficiently.

Problem

Previously, the INuGetHelper interface only supported resolving a single NuGet package at a time. When multiple packages needed to be resolved, callers had to:

  1. Make multiple individual calls
  2. Manually handle version conflicts between packages and their dependencies
  3. Deal with duplicate dependency resolution

This was inefficient and error-prone, especially when dealing with complex dependency graphs.

Solution

Added a new method to INuGetHelper:

Task<string[]> ResolvePackageReferences(string targetFramework,
    IEnumerable<NuGetReference> references, CancellationToken cancellationToken = default);

The implementation provides:

1. Automatic Version Resolution

Packages without specified versions automatically resolve to the latest stable version:

var references = new[] { new NuGetReference("Newtonsoft.Json") };
var assemblies = await nugetHelper.ResolvePackageReferences("net8.0", references);
// Automatically uses latest stable version

2. Version Conflict Resolution

When the same package is specified multiple times with different versions, the highest version is selected:

var references = new[]
{
    new NuGetReference("Newtonsoft.Json", "12.0.1"),
    new NuGetReference("Newtonsoft.Json", "13.0.3")
};
// Resolves to 13.0.3

3. Transitive Dependency Management

Dependencies from all packages are collected and merged, with version conflicts resolved by selecting the highest minimum version required by any dependency. Top-level packages take precedence over transitive dependencies.

4. Efficient Parallel Resolution

All packages and their dependencies are resolved to assemblies in parallel for optimal performance.

Version Resolution Strategy

The implementation follows NuGet's standard version resolution rules:

  1. Explicit package references: When the same package is specified multiple times with different versions, use the highest version
  2. Transitive dependencies: When dependencies require different versions of the same package, use the highest minimum version
  3. Top-level vs dependency: Top-level packages always take precedence over transitive dependencies when versions differ

Changes

  • INuGetHelper.cs: Added new method signature for resolving multiple package references
  • NuGetHelper.cs: Implemented comprehensive resolution logic with detailed inline documentation explaining the version resolution strategy at each step
  • NuGetHelperTest.cs: Added 4 comprehensive test cases covering various scenarios including version conflicts, automatic version resolution, and edge cases

Testing

All tests pass successfully:

  • ✅ Multiple packages with specified versions resolve correctly
  • ✅ Packages without versions automatically use latest stable version
  • ✅ Version conflicts correctly resolve to highest version
  • ✅ Empty list handled properly
  • ✅ Security analysis: 0 alerts

Example Usage

var nugetHelper = new NuGetHelper(loggerFactory);

var references = new[]
{
    new NuGetReference("WeihanLi.Common", "1.0.72"),
    new NuGetReference("Newtonsoft.Json", "13.0.3"),
    new NuGetReference("Dapper") // Version auto-resolved to latest stable
};

var assemblies = await nugetHelper.ResolvePackageReferences(
    "net8.0", references, cancellationToken);

// Returns all assembly paths needed for compilation, with all
// dependencies resolved and version conflicts handled automatically

This provides a more efficient and developer-friendly API for scenarios where multiple NuGet packages need to be resolved together, such as in script compilation or dynamic code generation scenarios.

Original prompt

This section details on the original issue you should resolve

<issue_title>nuget packages resolver</issue_title>
<issue_description>- support resolving multiple references

    Task<string[]> ResolvePackageReferences(string targetFramework,
        IEnumerable<NuGetReference> references, CancellationToken cancellationToken = default);
```</issue_description>

<agent_instructions>Research how nuget resolve the package versions amonth multiple package reference, may need to consider the package version conflict</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits October 24, 2025 12:45
Co-authored-by: WeihanLi <7604648+WeihanLi@users.noreply.github.com>
Co-authored-by: WeihanLi <7604648+WeihanLi@users.noreply.github.com>
Co-authored-by: WeihanLi <7604648+WeihanLi@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for resolving multiple package references Add support for resolving multiple NuGet package references with version conflict resolution Oct 24, 2025
Copilot AI requested a review from WeihanLi October 24, 2025 12:56
Copilot finished work on behalf of WeihanLi October 24, 2025 12:56
@WeihanLi WeihanLi closed this Oct 30, 2025
@WeihanLi WeihanLi deleted the copilot/support-multiple-package-references branch October 30, 2025 03:15
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.

nuget packages resolver

2 participants