Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
YuliiaKovalova opened this issue Jun 5, 2023 · 8 comments
Open

Comments

@YuliiaKovalova
Copy link

YuliiaKovalova commented Jun 5, 2023

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

@heng-liu
Copy link
Contributor

heng-liu commented Jun 6, 2023

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!

@heng-liu heng-liu added Partner:DotNet Functionality:SDK The NuGet client packages published to nuget.org and removed Triage:Untriaged labels Jun 6, 2023
@YuliiaKovalova
Copy link
Author

Hi @heng-liu ,

I mean adding package reference to project file :)

@nkolev92
Copy link
Member

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

@nkolev92 nkolev92 added WaitingForCustomer Applied when a NuGet triage person needs more info from the OP Product:VS.Client labels Jun 12, 2023
@YuliiaKovalova
Copy link
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.

@ghost ghost added WaitingForClientTeam Customer replied, needs attention from client team. Do not apply this label manually. and removed WaitingForCustomer Applied when a NuGet triage person needs more info from the OP labels Jun 14, 2023
@nkolev92
Copy link
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?

@ghost ghost added WaitingForCustomer Applied when a NuGet triage person needs more info from the OP and removed WaitingForClientTeam Customer replied, needs attention from client team. Do not apply this label manually. labels Jun 14, 2023
@ghost ghost added the Status:No recent activity No recent activity. label Jun 29, 2023
@ghost
Copy link

ghost commented Jun 29, 2023

This issue has been automatically marked as stale because we have not received a response in 14 days. It will be closed if no further activity occurs within another 14 days of this comment.

@baronfel
Copy link

@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 Newtonsoft.Json@13.0.3 Serilog@3 ... so that a single evaluation could be done and multiple PackageReferences could be added in one logical operation.

@nkolev92 nkolev92 added Product:dotnet.exe and removed Status:No recent activity No recent activity. WaitingForCustomer Applied when a NuGet triage person needs more info from the OP Functionality:SDK The NuGet client packages published to nuget.org labels Jun 29, 2023
@nkolev92
Copy link
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.

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

No branches or pull requests

4 participants