Enable NuGet package restore

Its never to late, recently I am seeing people around me who are misunderstanding NuGet Package Manager in Visual studio.

Shouting

Stop adding NuGet packages / library to source control

Stop adding NuGet packages / library to source control

First version of NuGet was release in January 13, 2011 and lots of things are changed since then. One of my favorite feature of NuGet is package restore. This particular feature is available since Version 1.6 release on 13th December, 2011.

It helps to avoid adding source control memory consuming package files and folders and reduce over all size of my clean solution folder.

Oversized solution in source control

Let me start with an example. I created a demo MVC 4 Internet Application, once created I went into NuGet package manager at solution level and updated all the packages installed by default in the MVC4 project template. After running the update, size of the packages folder in the solution folder was approximately 100 MB. This is not a ignorable value indeed. Now in organizations we normally branch and manage the application life cycle. So if I create 2 branch considering 15 days sprint (agile mind) per month I am adding 200 MB extra and useless to source control. This makes your TFS damn fat on utilized size.

Damn fat TFS

Package restore feature can be enabled at IDE level by navigating to Tools > Library Package Manager > Package manager settings.

Library package manager

In Package Manager > General Settings > Package Restore > Allow NuGet to download missing packages / Automatically check for missing packages during build in Visual Studio.

Allow nuget to download missing packages

If the above option is enabled and when you right click on your solution file in VS Solution explorer, you will find Enable NuGet Package Restore option. This option enables package restore for current solution and projects.

Enable nuget package restore

On click, VS will confirm about the feature enabling and informs about the new folder that will be created in the solution folder.

Nuget package restore confirmation

Click yes and NuGet will start enable package restore for the solution

Configuring restore nuget packages on build

Once this waiting is finished (will usually does not take long time) you will see finish confirmation dialog

Nuget package restore on build enabled

When package restore is enable, new folder named .nuget is created in solution folder and contains three files

  • NuGet.Config - Contains configuration information of NuGet for current solution and projects.

  • NuGet.exe - Manages and processes missing package and package management for current solution and projects.

  • NuGet.targets - This is a NuGet releated sections containing MS-Build file which will be used by solution to restore missing package while solution building is in progress.

.nuget folder in solution

As you all know Nuget Create a packages folder immediately under the solution folder. If you have nuget package restore enabled this folder can be skipped to be added to source control. Thus when you right click on solution and Add solution to source control, .nuget folder and its sub content will be added but packages folder and sub folders / files will be skipped.

This is controlled by a settings added in NuGet.Config configuration file with key disableSourceControlIntegration set to True by default.

If you delete the packages folder when package restore is enabled and try to build solution, on first try you will see below error - "The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see here".

Build again to include these packages in the build

On second build attempt above error will be resolved automatically and all the missing packages will be re-downloaded or installed in the solution.

Hope to see less instances where packages folder will be added to source control.

Reference: NuGet Doc