# Monday, 21 January 2013

NuGet, Files and Source Control

I’ve been using NuGet for years, but recently I’ve been playing with around with NuGet to try and explore some of the lesser known features. I’ve just been using NuGet to effortlessly download and add assemblies to my projects, but where do these files go? And how do I make sure that when I zip my sources and send them to another developer, that person also has everything he needs? How does NuGet play with regards to source control?

Default NuGet Workflow

The default process when using NuGet to install a library (for example EntityFramework) in your project is as follows:

  1. Start a new project in a new solution and add the solution to source control.
  2. Open the Package Manager Console.
  3. Type: “install-package EntityFramework”
  4. At solution level NuGet creates a folder named ‘Packages’.
  5. NuGet determines the most recently, non-beta, available version of EntityFramework and downloads this file into a folder, named after your package in the ‘Packages’ folder. It also creates a ‘repositories.config’ file to keep track of which projects are using NuGet. The folder may look something like this: 


    Note that all the files in the ‘Packages’ folder and it’s subfolders are automatically added to source control.
  6. NuGet adds a packages.config file to your project and uses this to keep track of which packages and versions are installed in the project:

  7. When you now check into source control all configuration information as well as all the downloaded packages are preserved and managed in source control.

The default approach works well in most scenarios, but has one downside: if you have a lot packages in your solution, then the size of your source control branch may become larger than desired. This would be less of an issue with say Team Foundation Server, but more of an issue with a DVCS like Mercurial or Git.

Restore During Build Workflow

An alternative approach is offered by using the Enabling Package Restore During Build option. Read the tutorial here to see how it is done.
There are three differences with the default NuGet workflow:

  1. A .nuget folder is added to the solution, which contains NuGet.exe, a config file and a build file. These file will be added to source control upon check in.
  2. Downloaded NuGet packages in the ‘Packages’ folder (they’re still there) are not added to source control.
  3. Every time you (re)build your project the NuGet build step will check to make sure the appropriate NuGet packages have been downloaded.

Zipping projects

As a rule: whenever you want send someone the sources of a project that uses NuGet for maintaining library references, make sure you send the solution, including the packages folder. If you’re using the Restore During Build Workflow, then the zip does not need to contain the actual packages.

#    Comments [0] |
Comments are closed.