Skip to content

Support NuGet Central Package Management #6

Closed

Description

@martincostello

For solutions using the new NuGet Central Packagement feature, trying the --upgrade option will cause dotnet-outdated to add the package versions to the csproj package references, rather than updating the package versions in Directory.Packages.props, and consequently break the build. A simplified example of how this manifests is shown below.

Updating the tool to support this NuGet feature would allow such projects to leverage the --upgrade option, rather than just needing to manually update using the console's output as a guide on what to update.

Happy to help-out with getting this supported!

Before

Directory.Packages.Props

<Project>
  <ItemGroup>
    <PackageVersion Include="Newtonsoft.Json" Version="12.0.2" />
  </ItemGroup>
</Project>

Project.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" />
  </ItemGroup>
</Project>

After

Directory.Packages.Props

<Project>
  <ItemGroup>
    <PackageVersion Include="Newtonsoft.Json" Version="12.0.2" />
  </ItemGroup>
</Project>

Project.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json">
      <Version>12.0.3</Version>
    <PackageReference>
  </ItemGroup>
</Project>

Activity

  1. coderpatros commented on Aug 23, 2020

    @coderpatros
    Member

    @martincostello that would be a great addition. We would welcome this functionality.

  2. martincostello commented on Aug 23, 2020

    @martincostello
    ContributorAuthor

    I had a dig around in the code, and saw that package upgrade is implemented via calling dotnet add package:

    public RunStatus AddPackage(string projectPath, string packageName, string frameworkName, NuGetVersion version, bool noRestore)
    {
    string projectName = _fileSystem.Path.GetFileName(projectPath);
    List<string> arguments = new List<string>{"add", $"\"{projectName}\"", "package", packageName, "-v", version.ToString(), "-f", $"\"{frameworkName}\"" };
    if (noRestore)
    {
    arguments.Add("--no-restore");
    }
    return _dotNetRunner.Run(_fileSystem.Path.GetDirectoryName(projectPath), arguments.ToArray());
    }

    As this calls through into the NuGet client, looks like the way to support this is to actually just wait for the .NET Core SDK to support adding/updating package references this way.

    Looking at what I think is the relevant NuGet code for this, it doesn't currently support doing this and just unconditionally adds the Version metadata onto the PackageReference item, rather than delegating it to the Directory.Packages.props file:

    https://github.com/NuGet/NuGet.Client/blob/3501ddedc274ac10d4b135856b7593a6bb8a72f1/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs#L227

    https://github.com/NuGet/NuGet.Client/blob/3501ddedc274ac10d4b135856b7593a6bb8a72f1/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs#L256

    Looking at the NuGet repo's issues, it looks like that is tracked by NuGet/Home#9022.

  3. slang25 commented on Aug 23, 2020

    @slang25
    Member

    Yeah, I was going to say that I thought this would "just work" because we shell out to the sdk. NuKeeper for example needed explicit support added (due to the custom implementation), but we should get it here for free.

    It's good we have the issue to track it with.

  4. steveoh commented on Oct 26, 2020

    @steveoh

    Would it be outrageous to edit the Directory.Packages.target from within the tool and then shell out for a restore?

  5. coderpatros commented on Oct 27, 2020

    @coderpatros
    Member

    @steveoh I don't think it would be outrageous. It could be a while before the standard dotnet CLI tools support central package management. If you you are interested in raising a PR in the meantime it would be most welcome.

  6. added
    enhancement
    on Nov 12, 2020
  7. added a commit that references this issue on Nov 17, 2020
  8. ransagy commented on Jan 26, 2022

    @ransagy

    Now that NuKeeper has deprecated itself and is no longer maintained, This became something of a thorn in our workflow, as we don't use any kind of bots of auto-PR creation tools right now, just developers updating their NuGets in their feature branches as needed.

    In our case, we have the "half-way" CPVM system - With csprojs having packages with Include and no Version tags and simply using a Directory.Build.Targets file with packages that have Update and Version tags.
    I'm not sure if that's entirely supported either way (Though it did work with NuKeeper's explicit support, as mentioned).

    So i raise this again for consideration. As for being able to PR - Can we start a discussion of how this would be implemented, theoratically? So anyone who might be able to contribute will have more of a stepping to start from.

  9. ransagy commented on Jan 28, 2022

    @ransagy

    I went ahead and opened PR #243 to add some of the basic "placeholder' support for upgrading CPVM controlled packages as expected by the CPVM preview spec, in lieu of SDK CLI support.

  10. leonardochaia commented on Mar 11, 2022

    @leonardochaia

    Hello all.

    Just wanted to hop in and say that I'd really like this feature to be implemented and would be happy to help any way I can.
    I use central package management in all my projects, since it simplifies managing versions on >1 project solutions.

    For dotnet-affected, we implemented a feature where we can detect which projects are affected by adding/deleting/updating a nuget package. I think that works great together with dotnet-outdated: you update all your packages, and then in CI or locally you only build/test/publish the projects that were changed/affected by the package changes.

    I wanted to share our implementation for reading Directory.Packages.props, which uses MSBuild in order to do so. This supports conditions as well, which is quite nice to have (we use conditions in props files for multi framework targeting)

    We also have tests where we create Directory.Packages.props files, see here
    Particularly, we ensure conditions are interpreted

    This is the code we use for updating packages, I wouldn't use that in production (its for testing only) but perhaps we can get some inspiration for dotnet-outdated.

    Should we create a package to share this code between these two tools?

    Regards,
    Leo.

  11. steveoh commented on Sep 22, 2022

    @steveoh

    Is there a PR or something to see where this was implemented?

  12. martincostello commented on Sep 22, 2022

    @martincostello
    ContributorAuthor

    #243, linked in the comment above which closed the issue.

  13. leonardochaia commented on Sep 23, 2022

    @leonardochaia

    Hey all. I was taking a very quick look at the PR and I was thinking:

    1. Are multiple Directory.Packages.props supported?
    2. Does it work when packages have conditions in Directory.Packages.props? i.e multiple target frameworks.
    3. Are VersionOverride taken into account?
    4. Is ManagePackageVersionsCentrally respected? May have a Directory.Packages.props but projects ManagePackageVersionsCentrally=false.

    We've made several changes at dotnet affected to improve how we evaluate Directory.Packages.props files in order to support these "edge cases".

    Regards,
    Leo.

  14. ransagy commented on Sep 23, 2022

    @ransagy
    1. Hierarchiel ones you mean? as long as Import tags are used, it should be fine.
    2. Probably not, unless the support for that was outside the area i updated.
    3. Nope - They werne't a thing until after i submitted the PR and the tools weren't updated yet.
    4. The nuget restore outputs give us if a version is centrally managed or not; if thats respected there, it will apply here.

    All in all, as the OP of #243 says, this was done to add rudimentry suppport until the underlying dotnet CLI fully supports those scenarios.
    For myself/the usage we have of this tool internally, we currently don't need the edge cases.
    I'm sure other PR contributions will be happily accepted :)

  15. leonardochaia commented on Sep 23, 2022

    @leonardochaia

    hi @ransagy thanks for the prompt reply. I think this is a great addition to this project.

    For the first point I meant this:

    What this means is that if you had multiple Directory.Packages.props files in your repository, the file that is closest to your project’s directory will be evaluated for it. This allows you extra control at various levels of your repository.

    source

    Thanks for clarifying the rest of the points 👍

  16. ransagy commented on Sep 23, 2022

    @ransagy

    Yup, it should always look at the closest available file going up the folder hierarchy.
    Thank you for your input as well :)

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions