19
votes

When enabling NuGet package restore, it adds the following to my solution:

/.nuget
/.nuget/NuGet.exe
/.nuget/NuGet.targets

As these are part of my solution, committing the solution to my repository without either of these files means I'll have missing references in my solution, but removing the folder or either of these files means NuGet package restore is not enabled.

Given that part of the point of setting up package restore is so that I don't have to commit binaries to the repository, what is the intention behind forcing NuGet.exe into the solution?

Is the assumption that committing one small .exe to the repository is a small price to pay to remove the need to commit other binaries? Or am I supposed to exclude these from the repository also and tell a developer checking out my repository for the first time to go and enable package restore? What about when continuous integration encounters a missing reference in my solution?

4

4 Answers

14
votes

The point behind committing the .nuget folder to the repository is that you can set up your solution to use package restore, using the exact same version of nuget.exe you installed when enabling this feature.

Your project files depend on the nuget.targets MSBuild instructions that use nuget.exe, and both files are considered linked together because the MSBuild instructions use the nuget.exe API. It's perfectly possible to end up with a different version of nuget.exe and nuget.targets one day. It uses the NuGet.Build and NuGet.Commandline packages under the hood. That's also the reason why package restore is typically set up once for the solution, and not by every developer individually.

Also, you won't need to install nuget.exe on each build server, and your build server can build solutions with different versions of nuget.exe, which is an added benefit that requires no additional effort. And yes, one could consider it a small price to pay as well :-)

If your CI encounters a missing reference, check the following:

  • your build server cannot reach the NuGet package source
  • your build server cannot find the NuGet package on the NuGet package source (check whether it's there or whether you're pointing to the correct package source)
9
votes

You don't have to commit nuget.exe to source control. NuGet.targets downloads the latest NuGet.exe from nuget.org if it's missing.

8
votes

If you are following @Richard Szalay's answer (not committing nuget.exe), and for some reasons Visual Studio does not automatically download the nuget.exe, make sure you have the following set to true in the nuget.targets file:

<!-- Download NuGet.exe if it does not already exist --> 
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">true</DownloadNuGetExe>

Close the VS solution, reopen it and build it. Visual Studio should download nuget.exe automatically now.

2
votes

For .gitignore, add below lines.

.nuget/
!.nuget/packages.config