Skip to content

Add support for batch "Add a reference to a project file" invocation #12629

Open

Description

@YuliiaKovalova

NuGet Product(s) Involved

NuGet SDK

The Elevator Pitch

Hi Nuget Team,

Template Engine crew sees increased demand for Cental Package Management Support:
dotnet/templating#6187
dotnet/aspnetcore#48569

Currently, it is possible to resolve the version correctly through post-action. It is a wrapper around your functionality.

As we can see, there is no support for specifying a set of project links, so users need to specify dependencies one by one in post actions (e.g. add reference).

Once you extend your API, we can utilize it for supporting batch "add reference" operations.

cc: @baronfel

Activity

  1. heng-liu commented on Jun 6, 2023

    @heng-liu
    Contributor

    Hi @YuliiaKovalova, may I know which API you're using right now in post-action? Do you mean adding package reference or project reference? Thanks!

  2. added
    Functionality:SDK
    and removed on Jun 6, 2023
  3. YuliiaKovalova commented on Jun 7, 2023

    @YuliiaKovalova
    Author

    Hi @heng-liu ,

    I mean adding package reference to project file :)

  4. nkolev92 commented on Jun 12, 2023

    @nkolev92
    Member

    @YuliiaKovalova Is this ask related to a Visual Studio API such as IVsPackageInstaller

  5. added
    WaitingForCustomer
    on Jun 12, 2023
  6. YuliiaKovalova commented on Jun 14, 2023

    @YuliiaKovalova
    Author

    @YuliiaKovalova Is this ask related to a Visual Studio API such as IVsPackageInstaller

    I don't think so.
    @baronfel, please correct me if I am wrong.

  7. ghost removed
    WaitingForCustomer
    on Jun 14, 2023
  8. ghost added
    WaitingForClientTeam
    on Jun 14, 2023
  9. nkolev92 commented on Jun 14, 2023

    @nkolev92
    Member

    @YuliiaKovalova

    If it's not VS, I'm not really sure I understand what the ask is here.

    I'm not really sure what Add Reference functionality on NuGet end you're referring to.

    In add package we use MSBuild APIs to write references, and that's it.
    Can you point to the code in the NuGet repo that you're calling that needs additional APIs?

  10. ghost removed
    WaitingForClientTeam
    on Jun 14, 2023
  11. ghost added
    WaitingForCustomer
    on Jun 14, 2023
  12. ghost added
    Status:No recent activity
    on Jun 29, 2023
  13. baronfel commented on Jun 29, 2023

    @baronfel

    @nkolev92 this is coming from the Template Engine, which has two hosts - the dotnet CLI, and the VS New Project Dialog. Specifically, we'd like to have some kind of batch/multi-package-add capability in the NuGet CLI so that we can make project templates more correct for a larger portion of our user base. Let me walk through the problem end to end.

    Currently, many project templates (MS and 3rd party) hard code PackageReference items in generated project files, but they'll often have a variable for the 'version' of that PackageReference. This is bad for a few reasons (versions getting out of date, hard to maintain) but for this conversation we mostly find it bad because hard-coding <PackageReference Include="..." Version="..." /> makes it very hard for a project to participate in CPM if the rest of the repo/solution/etc is onboarded to CPM. Package Authors would need to go through a number of complex conditional statements in the Template Engine to make sure they handle all cases, which increases the authoring burden for templates.

    If we look at what the actual need of the template author is, they want to ensure that a generated project gets a PackageReference to a certain package with a certain version. Turns out that we already have that capability in the CLI via dotnet add package, and better yet the NuGet team has already done the work to make this command interact well with CPM in every case! This is basically perfect for us - it means we can ask template authors to remove hard-coding their templates, and instead move to a model where, after the project file is generated, we orchestrate calling dotnet add package for any packages that they would like to add. This nicely abstracts needing to know how to add the packages for template authors.

    Which brings us to this issue. If we do this, then if a template author adds let's say 5 packages to a project that project will be evaluated at least 5 times - once for each invocation of dotnet add package. This seems unnecessary to me, and means a lot of overhead for templating operations. As a back-of-napkin estimate, I created a new console app and timed how long it took to run dotnet add package Newtonsoft.Json via Measure-Command. For me, on my work laptop this operation took 4.2 seconds. Doing that 5 times would dwarf the actual time required to generate the template content and restore (which today takes 2.25s on the same laptop).

    So that's the request. It would be awesome to have an ability or syntax like dotnet add package [email protected] Serilog@3 ... so that a single evaluation could be done and multiple PackageReferences could be added in one logical operation.

  14. added and removed
    Status:No recent activity
    WaitingForCustomer
    Functionality:SDK
    on Jun 29, 2023
  15. nkolev92 commented on Jun 29, 2023

    @nkolev92
    Member

    Thanks for clarifying it @baronfel!

    As an immediate minor improvement you can make use of the --no-restore feature to add references multiple times so you at least avoid multiple restores.

  16. added
    Priority:2
    and removed on Jun 29, 2023
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions