Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet auto package restore does not work with MSBuild

I'm trying to build a solution with packages content missing (except repositories.config inside) with MSBuild 12.0. I expect it to auto restore all missing packages before building but this is not the case - MsBuild reports tons of errors:

"are you missing a using directive or an assembly reference?"

NuGet Manager is 2.7 (I see this in Visual Studio 2013 about box). I even tried to pass EnableNuGetPackageRestore=true parameter - no luck. What am I missing?

like image 542
UserControl Avatar asked Oct 15 '22 08:10

UserControl


People also ask

How do I restore a NuGet package from MSBuild?

MSBuild -t:restore (which nuget restore and dotnet restore use with .NET Core projects), restores packages referenced in the project file as follows: Pass MSBuild data to NuGet.Build.Tasks.dll. The restore target works only for projects using the PackageReference format.

What are MSBuild NuGet targets?

These targets allow you to work with NuGet as you would with any other MSBuild task or target. For instructions creating a NuGet package using MSBuild, see Create a NuGet package using MSBuild. (For NuGet 3.x and earlier, you use the pack and restore commands through the NuGet CLI instead.)

How to restore MSBuild and vs addin?

MSBuild alone won't restore and the VS addin either. You need to enable package restore as @KMoraz said, and then as Sumeshk said the .nuget folder appears and packages can be restored. Make sure you check .nuget in to source control.

What is the MSBuild-integrated package restore approach?

The MSBuild-integrated package restore approach is the original Package Restore implementation and though it continues to work in many scenarios, it does not cover the full set of scenarios addressed by the other two approaches. This is no longer recommended by nuget. See the docs. docs.nuget.org/docs/workflows/…


1 Answers

If you are using Visual Studio 2017 or later which ships with MSBuild 15 or later, and your .csproj files are in the new PackageReference format, the simplest method is to use the new MSBuild Restore target.


No-one has actually answered the original question, which is "how do I get NuGet packages to auto-restore when building from the command-line with MSBuild?" The answer is: unless you are using the "Enable NuGet package restore" option (which is now deprecated as per this reference), you can't (but see below). If you are trying to do e.g. automated builds on a CI server, this sucks.

However there is a slightly roundabout way to get the desired behaviour:

  1. Download the latest NuGet executable from https://dist.nuget.org/win-x86-commandline/latest/nuget.exe and place it somewhere in your PATH. (You can do this as a pre-build step.)
  2. Run nuget restore which will auto-download all the missing packages.
  3. Run msbuild to build your solution.

Aside: while the new and recommended way to do auto package restore involves less clutter in your version control, it also makes command-line package restore impossible unless you jump through the extra hoop of downloading and running nuget.exe. Progress?

like image 248
Ian Kemp Avatar answered Oct 17 '22 20:10

Ian Kemp