NuGet Command-line options for uninstalling/reinstalling packages for TFS/VS2013 and NuGet 3.4.3 - visual-studio-2013

I modified a solution and deleted one of the .csproj files and instead packaged the .dlls into a NuGet package. Then I added the package to the two other .csproj files that reference the .dlls. All was good - it builds locally, but I can't get it to build on the build server. When I look at the code gotten out of TFS for the build I see that the packages.config for both projects have the correct reference to the NuGet package, but when I open the solution in VS the references have little yellow exclamation marks next to them because they're broken references. The only way I can get it to build is to open the NuGet CMI and execute Uninstall-package package-name and the Install-Package package-name. Then the references are good. When I look in the packages.config of the main project it contains the correct reference to the Package. So I've given up on getting TFS to correctly grab the package, but since Install-Package and Uninstall-Package are CMI commands only I can't automate that (or can I)? Does anyone know if a way that I could automate that to happen after the source is pulled from TFS but before the build?

The yellow exclamation marks issue should be related to the reference path. When you download the source from TFS to another location, the system cannot find the references as the original reference path changed.
So, you need to reinstall the package, you can use the NuGet command line in the Package Manager Console:
Update-Package -reinstall

since Install-Package and Uninstall-Package are CMI commands only I can't automate that (or can I)? Does anyone know if a way that I could automate that to happen after the source is pulled from TFS but before the build?
The simple answer is you can not automate that. You can use the command Install-Package and Uninstall-Package to reinstall the packages to your project in the Package Manager Console, but it seems impossible to automate that. Please forgive me for the lengthy explanation below.
First, we need to know the different the operation Install packages between NuGet CLI and Package Manager, although NuGet CLI and Package Manager both support the operation Install packages.
The operation Install packages on NuGet CLI:
Obviously, NuGet will not reinstall the references when you using the Install-packages operation on the NuGet CLI, just download the package to the packages folder. See NuGet CLI reference:
The install command does not modify a project file or packages.config;
in this way it's similar to restore in that it only adds packages to
disk but does not change a project's dependencies.
Conversely, operation Install packages on Package Manager:
Installs a package and its dependencies into a project.
If you want to automate that, you have to do this operations via NuGet CLI. Since Install package on NuGet CLI will not modify the reference of project, so we could not automate the operation install package to update the reference of the project.
Besides, we also do not recommend you automate that. Once you have automate that, NuGet execute the uninstall/install operations every time before you build the project. We only need to do an uninstall/install the operation after get the project from TFS. Even we do not need to do this operation if the references of the project are not broken after NuGet restore. So according to the reference of the project to determine whether or not to use an command:
Update-Package -reinstall
in the Package Manager Console should be the best choice.

Related

NuGet reinstall updates the package to newer version

I have cleared all the local NuGet caches and Visual Studio 16.9 is unable to restore the packages.
It says:
All packages are already installed and there is nothing to restore.
When I build the project, I get many errors as the NuGet packages are not restored.
The project has to contain the exact same packages as they are in the PackageReferences inside the csproj file and I've read that using the Package Manager Console can help by executing the Update-Package -reinstall command.
The Install-Package command does not provide an option to force a reinstall, so use Update-Package -reinstall instead.
I tried it and now I have this in my log:
No package updates are available from the current package source for project....
Updates? I don't want to update anything...
Restored D:\Dev\MyProject01\MyProject01.csproj (in 29 ms).
Successfully uninstalled 'Acr.UserDialogs 7.1.0.475' from MyProject01
Successfully installed 'Acr.UserDialogs 7.1.0.481' to MyProject01
How is this a reinstall? Seems more like updating the package to a newer version...
How could I reinstall the same packages?
UPDATE:
When I use the Rebuild command in the Solution Explorer, this is what I see.
Actually, that right-click on the Solution-->Restore nuget packages is for the missing project packages folder with packages.config. And it cannot detect the global cache missing packets alone.
That is why Microsoft has enabled these two settings:
So you do not have to worry about restore step. Just click Rebuild button rather than Build button. The two restore settings are under Rebuild process.
The project has to contain the exact same packages as they are in the
PackageReferences inside the csproj file
update-package -reinstall command is for non-sdk style projects with packages.config nuget management format and always reinstall the same version. And I wonder if your solution has the non-sdk project that used packages.config and that project can use the command.
I have a non-sdk project with net framework 4.5.2. See:
But update-package -reinstall does not work on new-sdk projects.
ConsoleApp1 is a net core project.
So, I wonder if you use Update-Package under it. And that command is to update the old version to the new one which can works on both non-sdk projects and new-sdk projects.
Conclusion
When you face the problem next time, just click Rebuild button. It is more easier.
Try deleting your project's obj/ directory (this is where NuGet stores information about which packages it's decided to use), and then rerunning the restore.
If you have source dependencies (i.e. ProjectReference) you may need to delete their obj/ directories too

How reproduce VS2017 Package Manager command "Update-Package -reinstall" with Nuget CLI

I've tried with Nuget Update, Nuget restore, but they are actually not the same and here's why:
I've created a Nuget pkg only to distribute an executable file. When I install the Nuget the executable is added to my project so I can reference it and run it within my application.
Now, I don't want that every developer has to install the Nuget to have the executable in their project, but I just want the .exe to be added when the project is built. And of course, the reference to the package in the config file is present.
If I run Update-Package -Id -reinstall(using the Package Manager of VS) a folder with the .exe is added to the project root, but if I do nuget update or nuget restore (using the nuget CLI) it only re-add the .exe into the packages folder.
The reason why I need to run the nuget CLI is because I'm using TeamCity and I want to add the .exe when the automatic build runs.
I hope it makes sense.
How reproduce VS2017 Package Manager command “Update-Package -reinstall” with Nuget CLI
I am afraid you can NOT do such things with NuGet CLI. That because NuGet CLI does not modify a project file or packages.config. When we modify the project file, we need to do it by NuGet API in Visual Studio, but only package manager console can provide access to visual studio objects:
https://github.com/NuGet/Home/issues/1512
That is the reason why I said the easiest way is using the command line Update-Package -Id -reinstall in your previous thread. Then I also gave you another way to resolve this issue from the root cause, using .targets file to copy the executable file to the the project root, please check my update answer.
Of course, there is another simple idea to resolve this issue, just add a pre-build event in your project to copy the executable file to the the project root:
xcopy /Y "$(SolutionDir)packages\MyToolPackage.1.0.0\Tools\wkhtmltopdf.exe" "$(ProjectDir)"
Hope this helps.

How to resolve Nuget Package Version and Path in Pre-Build Event of a Project?

I have a NuGet Package containing content files that is referenced in my project. When getting latest on a new machine, the build fails because NuGet Restore doesn't copy content files. So what I want to do is this:
In PreBuild
If my Content file doesn't exist,
run this nuget Command: "Update-Package My.Nuget.Sources -reinstall"
I'm struggling with determining path differences due to versioning of the Nuget files and getting access to nuget. How do I actually generate the SourceOnlyNugetVersion and NugetPath variables below?
if not exist "$(ProjectFolder)App_Packages\My.NuGet.Sources.$(SourceOnlyNugetVersion?)\somefile.cs" (
"$(NugetPath)nuget.exe" Update-Package My.NuGet.Sources -reinstall
)
How to resolve Nuget Package Version and Path in Pre-Build Event of a Project?
If you want to reinstall package automatically in Pre-build event, I am afraid you can`t achieve it currently.
We could use the command Update-Package -Id <package_name> –reinstall to reinstall the packages to your project in the Package Manager Console, but it is impossible to automate that.
If you want to automate it in the build event, you have to call the NuGet CLI rather than Package Manager Console. Because NuGet CLI does not modify a project file or packages.config; in this way it's similar to restore in that it only adds packages to disk but does not change a project's dependencies. See NuGet CLI reference.
The operation Install packages on NuGet CLI:
Conversely, operation Install packages on Package Manager:
Installs a package and its dependencies into a project.
So we could not use NuGet CLI to reinstall NuGet packages for project.
Besides, we could not use the Package manager console powershell outside visual studio, because package manager console is providing is access to visual studio objects.
https://github.com/NuGet/Home/issues/1512
Similarly, we could not use Package manager console in the build event, build events are run by MSBuild so it needs to work when the build is run from the command line.
So it seems impossible to automate reinstall NuGet packages, or the alternative approach would be to write a console app that uses NuGet.Core.dll to do the same thing that the PowerShell script is doing.

How to update Nuget tools-only packages via command line

Is it possible to update a "tools-only" Nuget package via the command line?
By "tools-only", I mean a package that does not contain a "lib" or "content" folder (according to the definition on the Nuget site), and contains scripts or tools rather than libraries that are referenced by Visual Studio Projects. A typical use is for tools that assist with build processes.
I am able to add tools-only packages in Visual Studio by managing the Nuget packages at the Solution level. That will download the package correctly, and also let me see and install updates to it.
But I've not been able to get the "update" command of the "nuget.exe" command line app to do the package update. It appears to drive all updates from the project files... but the package is not referenced by a project because it's not a code library.
I understand the focus of Nuget is code libraries, but the doco does make mention of tools packages... Given that it can be done within Visual Studio, I'd have thought the same should be possible on the command line.
E.g.
Tool package "A" (no lib, just tools folder)
In VS in project "B", manage Nuget packages on solution and install A
(Will download, and create ".nuget" folder with packages.config)
Release new version of "A"
In this scenario, I can see and install the update within VS, but cannot get the right command line to install the update.

Reinstalling NuGet packages with NuGet installed as VS Extension

I would like to be able to install all of the NuGet packages in the packages.config, as per The NuGet docs. NuGet is installed as a VS Extension, and I can't seem to find nuget.exe. Is it possible to run:
nuget i packages.config -o Packages
Without maintaining a seperate copy of nuget.exe on a per project basis?
Reinstall all packages in all projects of the current solution:
Update-Package -Reinstall
You can find more information about reinstalling nuget packages here
Warning - using
Update-Package -Reinstall
or
Update-Package -Reinstall -IgnoreDependencies
may remove all of your packages and package.config files!
Always make sure that you have your backups performed first.
Scenario:
Solution with multiple projects
Each contains their own Nuget entries, some with the same packages (e.g., SharpRepository, Entity Framework)
Now copy folder without the packages folder for "distribution" somewhere else
Assume the packages folder wasn't included with the distribution
Now try the command Update-Package -Reinstall or if you have some alpha packages and/or are sure your dependencies are good, try Update-Package -Reinstall -IgnoreDependencies
Result:
Because the packages folder doesn't exist, the entries for your packages methodically go away, too. This can surprise some people - so be careful, is all I'm saying.
As an update to this post, NuGet 1.6 added support for the workflow to restore the packages at build time if missing.
Right-Click on the solution, click "Enable Package Restore Mode" to set it on.
More details at:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
Found the solution on This blog entry. I needed to install NuGet.CommandLine, which makes nuget.exe. globally available in the VS command line. I can then set this up as a pre-build event to ensure that dependencies are downloaded.
A much easier option that you can keep enabled in Visual Studio during development to ease off your NuGet package installation related pain.
Keep both below mentioned options under NuGet Package Manager > General in checked state -
Allow NuGet to download missing packages
Automatically check for missing packages during build in Visual Studio
Have a look at the screenshot below:

Resources