How can I get MSBuild to restore any needed NuGet packages - visual-studio

I've looked at a ton of articles on this including here and here. None provide a solution that I can get to work.
I have everything turned on for automatically doing this in VisualStudio. But on our build machine we build the .sln files using MSBuild, not by opening up VisualStudio. So in MSBuild how can I best do this?
All of our .sln files are for VisualStudio 2019 and set to defaults on handling NuGet (I believe).

msbuild -t:Restore will restore nuget packages for projects with PackageReference nuget management format.
And your situation looks like packages.config nuget management format which you have used it.
If so, you only should use nuget restore to restore nuget packages with msbuild.exe.
1) first download nuget.exe cli from the website and config it. You can check this about it.
2) then use MSBuild command line, type
nuget xxx\xxx.sln restore
msbuild xxx\xxx.sln -t:build
Another is to use msbuild -t:restore with -p:RestorePackagesConfig=true.
msbuild xxx\xxx.sln -t:restore,build -p:RestorePackagesConfig=true

Related

packages.config file is ignored during Visual Studio Build and nuget restore commands

I am trying to setup automated nuget package restoration on a vs build or command line build.
When following the specified Microsoft article:
https://learn.microsoft.com/en-us/nuget/consume-packages/package-restore#migrate-to-automatic-package-restore-visual-studio
I am unable to get Visual Studio 2019 or the following commands (nuget restore mysolution.sln, msbuild -t:restore, msbuild -restore) to read from a packages.config file in each of my projects
I have tried the following:
removing any references to PackageReference in each of the project files
deleting the bin and obj folders in each project
adding a local nuget.config with the following:
deleting the local packages folder
deleting the global packages folder
running the following command: nuget locals all -clear
Updated Visual Studio -> Tools -> Options -> NuGet Package Manager and set "Allow Nuget to download missing packages" and "Automatically check for missing packages during build in Visual" and set the Default Package management format to "Packages.config"
Restarting Visual Studio a few times.
When I run the nuget restore command for my solution it attempts to restore from packagereference inside each of the project files but since I do not have any it does nothing but still creates the corresponding nuget.g.props and project.assets.json files. I cannot use packagereferences inside of project files since this is not portable to other projects.
I require the ability to use packages.config file with one of the build commands.
I am stumped as to why this is not working
You have to delete the .vs hidden folder from the solution folder, it still restores the old nuget management format PackageReference, and still use it.
Try the following steps:
1) Close VS, delete .vs hidden folder and every bin and obj folder.
2) make sure the every packages.config is in the project folder so that it will work.
3) If you use your own nuget.config, please make sure its package source contains all the nuget packages. Or the file is redundant, you should remove it and then add the nuget package source under VS IDE.
3) restart VS, run update-package -reinstall under Tools-->Nuget Package Manager-->Package Manager Console
4) After that, you can delete global nuget folder and local nuget folder and then run nuget restore xxx.sln to check it.
EDIT: Make sure your project is not an SDK-style project; SDK projects are not supported with packages.config files (see this).

Nuget update with PackageReference package management format

I'm using the PackageReference package management format available in VS2017 rather than packages.config.
The Nuget restore command works fine, however, the Nuget update seems to be searching from projects that have a packages.config even though I'm explicitly providing the .sln file
The command I'm using is
\NuGet\4.0.0\x64\nuget.exe update "Test.sln"
The output I get is
Scanning for projects...
MSBuild auto-detection: using msbuild version '15.3.409.57025' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin'.
Found 0 projects with a packages.config file.
Does anyone know if this should work or some other way of forcing my packages to update?
Does anyone know if this should work or some other way of forcing my packages to update?
At the moment, NuGet CLI does not support automatic package updates to the the new .NET Core .csproj format, you can refer to the below GitHub issue for detail:
https://github.com/NuGet/Home/issues/4358
If you want to force your packages to update, you can use the command line:
dotnet add package PackageName --version <version>
to update the package to the version that you specify. See the Github issue 4361 for detail.
Update to the Comment:
If you want to update to the latest version (without having to specify a specific version) of your nuget packages, you could use above command line without the option "--version":
dotnet add package PackageName
Besides, you can also use the command line update-package from the package manager console to update the package.

How to update nuget packages without package manager of visual studio?

How to update nuget packages without package manager of visual studio, if the projects referenced in solution have different nuget package folders, then updating nuget packages from solution becomes tedious job. You need to update nuget packages per project. Is there a way to update nuget packages without using visual studio?
To update the nuget packages in solution, you can create a batch file with multiple commands or can execute command like mentioned below using command prompt:
Open command prompt and change the folder where Nuget.Exe is present.
NuGet.Exe Update [Solution File] -Id [Nuget package Id]
There are more switches to Nuget.Exe Update switch, like you can specify the nuget source. Make changes as required and you can successfully update nuget packages without opening the visual studio.

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.

Does "enable nuget package restore" realy necessary for package recovery?

I can successfully restore package even if the option "enable nuget package restore" is not switched on in the project. Plus it generates additional files within my solution.
With the latest version of NuGet (2.7 or above) you do not need to use the Enable NuGet Package Restore menu option if you do not want to.
The latest version of NuGet, when installed in Visual Studio, will automatically restore any missing packages when your build your project. It does this without needing to add any new files to your solution and does not use MSBuild. It is now the recommended way of restoring NuGet packages.
You can also restore from the command line using NuGet with a command line similar to:
nuget restore YourSolution.sln

Resources