Skip to content

Add VersionOverride support for Central Package Management#683

Merged
kzu merged 4 commits into
mainfrom
copilot/add-central-package-support
Jan 14, 2026
Merged

Add VersionOverride support for Central Package Management#683
kzu merged 4 commits into
mainfrom
copilot/add-central-package-support

Conversation

Copilot AI commented Jan 14, 2026

Copy link
Copy Markdown
Contributor
  • Investigate CPM support and understand issue
  • Add support for VersionOverride attribute in _UpdatePackageReferenceVersions target
  • Update InferImplicitPackageReference task to handle VersionOverride
  • Fix CPM version resolution to filter PackageDependencies by ParentTarget
  • Add test for VersionOverride with CPM
  • Build succeeds
  • Verified fix with manual testing

Issue Root Cause

When using CPM (Central Package Management) with multitargeting, the PackageDependencies items from RunResolvePackageDependencies contain entries for ALL target frameworks in the project.assets.json. Without filtering by ParentTarget, the _UpdatePackageReferenceVersions target was selecting the LAST matching dependency regardless of target framework, causing incorrect version resolution.

For example, with:

  • net472: System.Text.Json 8.0.5
  • net9.0: System.Text.Json 9.0.0 (via VersionOverride)

Both TFMs were incorrectly getting 9.0.0 because it appeared last in the PackageDependencies.

Changes Made

src/NuGetizer.Tasks/NuGetizer.Inference.targets

  1. Added ParentTarget to ItemDefinitionGroup for PackageDependencies to ensure the metadata is always defined
  2. Updated the _ResolvedPackageDependency filter condition to also check %(ParentTarget) == $(TargetFramework) to only match dependencies for the current target framework
  3. Added support for VersionOverride metadata to use it directly when present

Testing

Verified manually that:

  • net472: System.Text.Json 8.0.5 (central version)
  • net9.0: System.Text.Json 9.0.0 (VersionOverride)

Each TFM now correctly gets its own resolved version.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add/Improve support for Central Package Management</issue_title>
<issue_description>## Describe the Bug

NuGetizer is ignoring central package management.

Steps to Reproduce

  1. Create an empty solution
  2. Create a Directory.Packages.props and enable central package management for the solution.
<Project>
    <PropertyGroup>
        <EnablePackageVersionOverride>true</EnablePackageVersionOverride>
        <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
    </PropertyGroup>
    <ItemGroup>
        <PackageVersion Include="NuGetizer" Version="1.2.3" />
        <PackageVersion Include="Unity" Version="5.11.10" Condition=" '$(TargetFramework)' == 'net8.0' " />
        <PackageVersion Include="Unity" Version="4.0.1" Condition=" '$(TargetFramework)' == 'net472' " />
    </ItemGroup>
</Project>
  1. Add a class library with the following project file content
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFrameworks>net8.0;net472</TargetFrameworks>
        <ImplicitUsings>enable</ImplicitUsings>
        <PackageId>MyClassLib</PackageId>
        <Version>1.0.0</Version>
        <Nullable>enable</Nullable>
        <LangVersion>12</LangVersion>
    </PropertyGroup>
    
    <ItemGroup>
        <PackageReference Include="NuGetizer" />
        <PackageReference Include="Unity" />
    </ItemGroup>
    
</Project>

Expected Behavior

I expected the following dependencies in the nuspec file when I dotnet pack:

<dependencies>
      <group targetFramework="net8.0">
          <dependency id="Unity" version="5.11.10" />
      </group>
      <group targetFramework=".NETFramework4.7.2">
          <dependency id="Unity" version="4.0.1" />
      </group>
</dependencies>

but I am getting:

<dependencies>
      <group targetFramework="net8.0">
          <dependency id="Unity" version="5.11.10" />
      </group>
      <group targetFramework=".NETFramework4.7.2">
          <dependency id="Unity" version="5.11.10" />
      </group>
</dependencies>

Exception with Stack Trace

Version Info

Additional Info

Central Package Management works fine when I use dotnet pack command alone without NuGetizer.
Similarly, I can conditionally specify different versions in the project file with NuGetizer if the Central Package Management disabled.

The problem is that I can't use both Central Package Management and NuGetizer to use different versions of a dependency per target when multitargeting.

Back this issue
Back this issue

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

@kzu This is a good feature to add (since now CPV is quite a bit more flexible than it used to be, and therefore more useful in general). @kzu If the issue gets sufficient funding, I might work on it.

Happy to take a PR for it too.

Thanks!</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI assigned Copilot and kzu Jan 14, 2026
Copilot AI changed the title [WIP] Add support for central package management in NuGetizer Add VersionOverride support for Central Package Management Jan 14, 2026
Copilot AI requested a review from kzu January 14, 2026 21:05
@kzu

kzu commented Jan 14, 2026

Copy link
Copy Markdown
Member

@copilot The implemented fix is not comprehensive and does not address the main repro steps from #544. Investigate more deeply the NuGet Client repo (https://github.com/NuGet/NuGet.Client) and determine what MSBuild items (or tasks) can be leveraged for CPVM. You can inspect targets in use by using diagnostics logging in a dotnet pack without nugetizer (i.e. dotnet build testproject.csproj -v:diag) and explore the targets that are invoked.

@kzu

kzu commented Jan 14, 2026

Copy link
Copy Markdown
Member

308 passed 308 passed 1 skipped

🧪 Details on Microsoft Windows 10.0.26100

from dotnet-retest v1.0.0 on .NET 10.0.1 with 💜 by @devlooped

Copilot AI commented Jan 14, 2026

Copy link
Copy Markdown
Contributor Author

@copilot The implemented fix is not comprehensive and does not address the main repro steps from #544. Investigate more deeply the NuGet Client repo (https://github.com/NuGet/NuGet.Client) and determi...

I've investigated the NuGet Client's MSBuild targets and identified the root cause. The fix is in commit f1bc649.

Issue: When using CPM with multitargeting, PackageDependencies from RunResolvePackageDependencies contains entries for ALL target frameworks. The _UpdatePackageReferenceVersions target wasn't filtering by TFM, so it picked the LAST matching entry regardless of target framework.

Fix:

  1. Added ParentTarget to PackageDependencies ItemDefinitionGroup
  2. Updated the filter to include $([MSBuild]::ValueOrDefault('%(ParentTarget)', '')) == '$(TargetFramework)'
  3. Added support for VersionOverride attribute (already committed earlier)

Now each TFM correctly resolves its own version:

  • net472: System.Text.Json 8.0.5 (central version)
  • net9.0: System.Text.Json 9.0.0 (VersionOverride)

Copilot AI and others added 4 commits January 14, 2026 19:08
Co-authored-by: kzu <169707+kzu@users.noreply.github.com>
Co-authored-by: kzu <169707+kzu@users.noreply.github.com>
Co-authored-by: kzu <169707+kzu@users.noreply.github.com>
…solution

Co-authored-by: kzu <169707+kzu@users.noreply.github.com>
@kzu kzu force-pushed the copilot/add-central-package-support branch from f1bc649 to 9f21cb9 Compare January 14, 2026 22:08
@kzu kzu marked this pull request as ready for review January 14, 2026 22:08
@kzu kzu enabled auto-merge (squash) January 14, 2026 22:10
@kzu kzu merged commit 40716ad into main Jan 14, 2026
3 checks passed
@kzu kzu deleted the copilot/add-central-package-support branch January 14, 2026 22:18
@kzu kzu added the enhancement New feature or request label Jan 15, 2026
@devlooped devlooped locked and limited conversation to collaborators Feb 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add/Improve support for Central Package Management

2 participants