NuGet reinstall updates the package to newer version - visual-studio

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

Related

Re-download nuget packages

I am working with a nuget feed where they sometimes change the package but they don't change the version number.
This means I often want a code change but Visual Studio doesn't show it as an update available.
I have many packages in several projects referenced. What is quickest way to re-download all packages?
I have tried this command:
Update-Package -reinstall
But it doesn't seem to force it to re-download the package as I am not getting new code.
How do I delete existing packages and force it to re-download?
Found out way to do it is this command:
dotnet nuget locals all -c
Then restart VS and rebuild

Project references are not resolved after updating NuGet packges through CLI

I have a nightly process running on TeamCity that will loop through my packages on a locally hosted NuGet repo and update all packages in all solutions to the latest prerelease version.
When I pull the latest code the morning after. The solution builds fine, but my project references are not loaded.
Unresolved project references: https://imgur.com/a/v7Klbkm
Unresolved code: https://imgur.com/a/EEzWgUe
I'm using packages.config to manage my NuGet packages.
In order to continue working I have to "reload" my Visual Studio projects by either manually asking the properties of the missing reference, or by changing something in the .csproj file.
That is my little workaround, but for some solutions with 10+ projects this becomes quite cumbersome.
Other means of updating packages work fine, if I try to update the packages through Visual Studio I have no issues. It only fails when using the NuGet.exe command line utility.
Here's the command we use to update the packages through NuGet.exe on TeamCity during a nightly build:
NuGet.exe update <path_to_packages.config> -Id <current_package_to_update> -pre -NonInteractive
This is the version of NuGet we're using:
NuGet Version: 4.8.1.5435
Here's an excerpt of the logging:
found PFW.Utilities with version 2019.9.0-build0009 in file C:\Sources\Xmp\ModXmp\packages.config
http://srvppratonexus.prato.be:8888/nexus/service/rest/repository/browse/nuget-prato-prereleases/PFW.Utilities/2019.9.0-build0009
MSBuild auto-detection: using msbuild version '16.1.76.45076' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\bin'.
Feeds used:
http://srvppratonexus:8888/nexus/service/local/nuget/nuget-group/
https://api.nuget.org/v3/index.json
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
Attempting to gather dependency information for multiple packages with respect to project `C:\Sources\Xmp\ModXmp\ModXmp.csproj`, targeting `.NETFramework,Version=v4.6.1`
Gathering dependency information took 10.79 sec
Attempting to resolve dependencies for multiple packages.
Resolving dependency information took 0 ms
Resolving actions install multiple packages
Found package 'PFW.Utilities 2019.9.0-build0019' in 'C:\Sources\Xmp\packages'.
Removed package 'PFW.Utilities 2019.9.0-build0009' from 'packages.config'
Successfully uninstalled 'PFW.Utilities 2019.9.0-build0009' from ModXmp
Package 'PFW.Utilities.2019.9.0-build0019' already exists in folder 'C:\Sources\Xmp\packages'
Added package 'PFW.Utilities.2019.9.0-build0019' to 'packages.config'
Successfully installed 'PFW.Utilities 2019.9.0-build0019' to ModXmp
Executing nuget actions took 192.26 ms
When I pull the latest code the morning after. The solution builds
fine, but my project references are not loaded.
When you call nuget.exe to update nuget packages for the project using packages.config format outside the VS IDE, it not only modified the packages.config file, but also modified the project file(.csproj). So VS IDE can't load them well unless a reload.
Not sure what's the real solution you have, you can Go Tools => option => Environment => Documents, make sure you've enabled the Detect when file is changed outside the environment. Then you can get reload all dialog when you modify the project file outside VS IDE.
Reload all dialog looks like this:
That is my little workaround, but for some solutions with 10+ projects
this becomes quite cumbersome.
If for some reason that dialog didn't occur, you can manually open one .csproj using notepad, then add one empty line into it and save. VS will detect the change and show the dialog, then click reload all to reload all projects in the solution.(For the solution with many projects)

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

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.

Restore nuget packages defined in .nuget folder

There are some nuget packages (e.g. OpenCover or ReportGenerator) installed without changing packages.config in any of the project, but there is a [Solution Dir]\.nuget\packages.config created with the package reference information.
When VS builds the solution, the packages defined in that file are not downloaded at all (even I have auto restore nuget enabled).
How can I restore them automatically?
The MSBuild based package restore, that uses NuGet.targets, which is enabled in Visual Studio by selecting Enable NuGet Package Restore, does not seem to support restoring solution level packages, which are those that are defined in the [SolutionDir]\.nuget\packages.config file.
Looking at the NuGet.targets file on build it restores the packages for the project using the project's packages.config file but does not use the solution's packages.config file.
So your options are:
Stop using the MSBuild based package restore. Visual Studio, with recent versions of NuGet, will automatically restore all packages, including all solution level packages, when you build.
Run NuGet.exe restore YourSolution.sln from the command line, or PowerShell console, or as a pre-build step, to restore all packages. NuGet.exe can be used to restore all packages on a build server if you are using one.
The MSBuild based package restore has been deprecated by the NuGet team so I would use option 1) if this is possible.

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