108

With newer versions of NuGet it is possible to configure a project to automatically restore NuGet packages so that the packages folder doesn't need to be included in the source code repository. Good.

However, this command adds a new .nuget folder and there is a binary there, NuGet.exe. This can also be re-created automatically by Visual Studio and so it doesn't feel correct to add that to version control. However, without this folder Visual Studio won't even load the solution properly.

How do you people deal with this? Add .nuget to source control? Run some command line script before opening the solution?

Borek Bernard
  • 50,745
  • 59
  • 165
  • 240
  • This is the most authentic link http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages and since it is an old thread. I would only like to share the information in the comment... – Naveed Butt Aug 08 '14 at 12:53

7 Answers7

48

@Richard Szalay's answer is right - you don't need to commit nuget.exe. If 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.

shytikov
  • 9,155
  • 8
  • 56
  • 103
Gan
  • 4,827
  • 3
  • 34
  • 50
  • By the way, anyone knows why it's not set to `true` by default? – ajukraine Apr 05 '13 at 14:42
  • 2
    It's more of a privacy concern. "The simple act of making a request over the Internet can reveal information about the user (for example, from the user’s IP address, we can approximate her location)." See [Package Restore and Consent article at Nuget blog](http://blog.nuget.org/20120518/package-restore-and-consent.html) – Gan Apr 09 '13 at 02:19
  • 1
    FYI: When NuGet.exe is not present in the .nuget folder, the Solution's context menu will show "Enable NuGet Package Restore", even though NuGet Package Restore is already configured. After a build the option will go away. – comecme May 10 '13 at 19:23
  • This should be the accepted answer, IMO... If NuGet.exe was super tiny I would maybe say stick it in source control and deal with whatever you have to do in your ignore file. But it's 1.5 megs, that's big enough for me to always do it Gan's way. – Brian MacKay May 20 '14 at 13:58
  • Where does it download the nuget.exe form? What if my buildserver has no internet? – bitbonk Feb 22 '17 at 15:43
48

This post is old, you should not be using solution level NuGet package restore anymore. As of version 2.7+ there is an option in the NuGet setup to automatically restore packages on build. So the .nuget folder can be deleted and the option removed from your projects.

http://docs.nuget.org/docs/reference/package-restore

UPDATE: With the release of NuGet 4.x and .NET Standard 2.0, when you use the new csproj format you can now use package references, ironically reintroducing the dependency on msbuild to restore packages, but now packages are a first class citizen of msbuild. The link above also makes mention of the PackageReference, but the following announcement details it better:

https://blog.nuget.org/20170316/NuGet-now-fully-integrated-into-MSBuild.html

And the NuGet 4.x RTM announcement, which ironically isn't as useful:

https://blog.nuget.org/20170308/Announcing-NuGet-4.0-RTM.html

UPDATE 2: Apparently with VS2017 you can even use package references with classic csproj projects, but they aren't backwards compatible anymore, and there have been some problems with restoring package sub-dependencies. I'm sure that will all be resolved.

Jeremy
  • 843
  • 11
  • 16
  • 8
    [This is a clearer explanation](http://docs.nuget.org/consume/package-restore/migrating-to-automatic-package-restore) of how to migrate. There is a [handy PowerShell script on Github](https://github.com/owen2/AutomaticPackageRestoreMigrationScript) – CAD bloke Mar 08 '15 at 21:52
  • @CAD Bloke, yes that is in the reading list at the bottom, thank you for narrowing it down. – Jeremy Nov 23 '15 at 19:57
  • You can easily update Nuget in VS using `Tools > Extensions & Updates > Updates`. – jocull Jan 03 '17 at 21:31
21

According to this thread, the .nuget folder should be version controlled.

Borek Bernard
  • 50,745
  • 59
  • 165
  • 240
20

You need to commit .nuget\nuget.targets, but not nuget.exe. The targets will download the exe if it doesn't exist, as long as you change DownloadNuGetExe to true in nuget.targets

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
4

Although I usually don't like the idea of adding exe's to source control, I would suggest that source control should contain anything that is required in order to open, build and execute the project.

In this case it sounds like the .nuget folder is a required dependency. Therefore it ought to be under source control.

The only question left, that you need to research, is how NuGet is going to react if that folder is marked read-only, which TFS will do once it has been checked in.


Update: I did a little more research on this as I've never used NuGet before. http://blog.davidebbo.com/2011/03/using-nuget-without-committing-packages.html

I would suggest that probably what you want to do is make NuGet a requirement that has to be installed on every developers workstation.

Further, you should place in source control the batch file required to get a workstation ready to start editing the project. The batch file is going to run the commands necessary to get and install the dependency packages.

Beyond that I'd say you might want to contact NuGet directly to ask them how, exactly, this is supposed to work.

NotMe
  • 87,343
  • 27
  • 171
  • 245
1

We have the nuget.config file in the folder, as it has the references to our internal Nuget server, using the Package Sources area: https://docs.nuget.org/consume/nuget-config-settings

Apart from this reason, you should let Visual Studio handle the downloading of packages.

Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
1

Now that nuget supports package restoration we're looking at it more closely.

We use Subversion for source control, and my initial thoughts are that .nuget should be added to our repository, but added using svn:externals so that it points to a single location.

That way we can automatically push out new versions to all developers and projects. For projects on release branches, rather than HEAD, we can specify the revision of svn:externals reference if we want to leave nuget alone.

We have a lot of projects, so it also means not duplicating nuget.exe multiple times in the repo.

si618
  • 16,580
  • 12
  • 67
  • 84
  • I couldn't get NuGet to restore external project packages. Did this work for you?. – Doguhan Uluca Feb 28 '12 at 23:25
  • Yes, although NuGet.exe seems to be having problems authenticating to our local repo (IIS 6 + SSL + AD authentication) whilst the Powershell or Extension Plugin work fine. – si618 Feb 29 '12 at 01:34