Skip to content

Support Central Package Management with conditional ItemGroups per target framework #529

Closed

Description

@flensrocker

I have a monorepo with some libraries which multi targets .NET 6 and 8.

They use Central Package Management with different packages per target framework.
When I use dotnet outdate -vl Minor it reports correctly the available updates:

dotnet outdated -vl Minor
» cpm-conditional
  [net8.0]
  Microsoft.EntityFrameworkCore.SqlServer  8.0.1 -> 8.0.2
  [net6.0]
  Microsoft.EntityFrameworkCore.SqlServer  6.0.26 -> 6.0.27

But if I try the update with -u both versions are updated to 8.0.2.

It's kind of a regression because it works if I don't have central package management enabled and use the conditions directly in the project file.

Project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net6.0;net8.0</TargetFrameworks>
    <RootNamespace>cpm_conditional</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
  </ItemGroup>

</Project>

Directory.Packages.props

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
    <PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.26" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
    <PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
  </ItemGroup>
</Project>

Activity

  1. msherms2 commented on Jul 22, 2025

    @msherms2

    This was seemingly lost to time but seemingly still an issue, but may be a duplicate of dotnet/sdk#49614 and/or NuGet/Home#14438 for the current dotnet SDK/product. This may be a dealbreaker to the ability to multi target and use CPM, so I'm posting everywhere I can to try to get it noticed.

  2. flensrocker commented on Jul 22, 2025

    @flensrocker
    Author

    @msherms2 building multi targeting projects with different package versions (or even different package references) per TFM does work for me without problems.

    For me it's just dotnet-outdated, which cannot distinct the references per TFM.

    Or I don't understand your issue, which may be totally possible... 😎

  3. mpickers commented on Oct 15, 2025

    @mpickers

    It's also just dotnet-outdated for me as well.

    I've just hit this issue when moving to CPM. An example is:

    <PackageVersion Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.20" Condition="'$(TargetFramework)' == 'net8.0'" />
    <PackageVersion Include="Microsoft.AspNetCore.WebUtilities" Version="9.0.9" Condition="'$(TargetFramework)' == 'net9.0'" />
    

    Which is valid and works for CPM: https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management#using-different-versions-for-different-target-frameworks

    What happens when running dotnet-outdated is that the Version of both of those entries gets set to the same...usually to the latest but sometimes not.

  4. dougwaldron commented on Jan 8, 2026

    @dougwaldron
    Contributor

    This is still an issue with the latest version, v4.6.9.

    To be clear, dotnet-outdated correctly identifies the latest update compatible with each target framework, but dotnet outdated --upgrade incorrectly sets them all to the same version, possibly even downgrading!

    I think my example will be instructive.

    AppLibrary.csproj

    <TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>

    Directory.Packages.props

      <ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
        <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
      </ItemGroup>
      <ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
        <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
      </ItemGroup>
      <ItemGroup Condition=" '$(TargetFramework)' == 'net10.0' ">
        <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="10.0.0" />
      </ItemGroup>

    Both dotnet-outdated by itself or dotnet-outdated --upgrade correctly identify the appropriate version updates, but the latter applies them incorrectly:

    ❯ dotnet outdated --upgrade
    Discovering projects...
    Analyzing project(s)...
    Analyzing dependencies...
    » AppLibrary
      [net8.0]
      Microsoft.EntityFrameworkCore  8.0.0 -> 9.0.11
      [net10.0]
      Microsoft.EntityFrameworkCore  10.0.0 -> 10.0.1
      [net9.0]
      Microsoft.EntityFrameworkCore  9.0.0 -> 9.0.11
    
    Version color legend:
    <red>   : Major version update or pre-release version. Possible breaking changes.
    <yellow>: Minor version update. Backwards-compatible features added.
    <green> : Patch version update. Backwards-compatible bug fixes.
    
    Upgrading package Microsoft.EntityFrameworkCore...
    Project AppLibrary [net8.0] upgraded successfully
    
    Upgrading package Microsoft.EntityFrameworkCore...
    An error occurred while upgrading AppLibrary
    Failed to restore project after upgrading!
    
    Upgrading package Microsoft.EntityFrameworkCore...
    Project AppLibrary [net9.0] upgraded successfully
    

    Note the interesting order for the target frameworks. This is random, and a different order can occur each time it's run.

    Also note that v9.0.11 of the NuGet package is indeed compatible with net8.0, so the analysis is correct. But the result is that all packages are up/downgraded to v9.0.11:

      <ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
        <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
      </ItemGroup>
      <ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
        <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
      </ItemGroup>
      <ItemGroup Condition=" '$(TargetFramework)' == 'net10.0' ">
        <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
      </ItemGroup>

    In fact, keeping the Directory.Packages.props file open in a text editor reveals that all the targeted entries are updated multiple times. In this example, all three are changed to v9.0.11, then v10.0.1, then v9.0.11 again, reflecting the order listed in the analysis.

  5. dougwaldron commented on Jan 8, 2026

    @dougwaldron
    Contributor

    Inside the UpgradePackages method, different AddPackage methods are called depending on whether CPM is used:

    status = package.IsVersionCentrallyManaged
    ? _centralPackageVersionManagementService.AddPackage(project.ProjectFilePath, package.Name, package.LatestVersion, NoRestore)
    : _dotNetPackageService.AddPackage(project.ProjectFilePath, package.Name, project.Framework.ToString(), package.LatestVersion, NoRestore, IgnoreFailedSources);

    You can see that the standard method includes a project.Framework parameter, but the CPM method does not.

    These two AddPackage methods work very differently. The former calls the built-in dotnet package add command, which respects the target framework parameter. On the other hand, the latter uses regex to modify the Directory.Packages.props file without regard to framework.

    I wonder why these methods were written so differently and why they were bifurcated in the first place? According to the documentation, dotnet package add is compatible with CPM.

    Actually, I just looked at the history of the CPM version, and it looks like it was done this way because the dotnet CLI did not initially support CPM. From PR #243:

    This PR aims to fix #6 or at the very least provide the rudimentry support needed to fix it while the dotnet CLI does not support CPVM.

    (Bolding in the original.) So it looks like a fix could be as simple as consolidating the two separate versions to only use the CLI command.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions