At one of our recent events I was asked if I had any experience using Nuget within a TFS 2010 build. At the time I had not, but I thought it worth a look.
For those of you who don’t know Nuget is a package manager that provides a developer with a way to manage assembly references in a project for assemblies that are not within their solution. It is most commonly used to manage external commonly used assemblies such a nHibernate or JQuery but you can also use it manage your own internal shared libraries.
The issue the questioner had was that they had added references via Nuget to a project
Their project then contained a packages.config file that listed the Nuget dependencies. This was in the project root with the <project>.csproj file.
<?xml version="1.0" encoding="utf-8"?>
<packages> <package id="Iesi.Collections" version="188.8.131.5200" /> <package id="NHibernate" version="184.108.40.20600" />
This packages.config is part of the Visual Studio project and so when the project was put under source control so was it.
However, when they created a TFS build to build this solution all seems OK until the build ran, when they got a build error along the lines
Form1.cs (16): The type or namespace name 'NHibernate' could not be found (are you missing a using directive or an assembly reference?)
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets (1490): Could not resolve this reference. Could not locate the assembly "Iesi.Collections". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets (1490): Could not resolve this reference. Could not locate the assembly "NHibernate". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
Basically the solution builds locally but not on the build box, the assemblies referenced by Nuget are missing. A quick look at the directory structure show why. Nuget stores the assemblies it references in the solution folder, so you end up with
Packages – the root of the local cache of assemblies created by Nuget
If you look in the <project>.csproj file you will see a hint path pointing back up to this folder structure so that the project builds locally
The problem is that this folder structure is not known to the solution (just to Nuget), so this means when you add the solution to source control this structure is not added, hence the files are not there for the build box to use.
To fix this issue there are two options
- Add the folder to source control manually
- Make the build process aware of Nuget and allow it to get the files it needs as required.
For now lets just use the first option, which I like as in general in do want to build my projects against a known version of standard assemblies, so putting the assemblies under source control is not an issue for me. It allows me to easily go back to the specific build if I have to.
(A quick search with your search engine of choice will help with the second option, basically using the nuget.exe command line is the core of the solution)
To add the files to source control, I when into Visual Studio > Team Explorer > Source Control and navigated to the correct folder. I then pressed the add files button and added the whole Packages folder. This is where I think my questioner might have gone wrong. When you add the whole folder structure the default is to exclude .DLLs (and .EXEs)
If you don’t specifically add these files you will still get the missing references on the build, but could easily be thinking ‘ but I just added them!’, easy mistake to made, I know I did it.
Once ALL the correct files are under source control the build works as expected.