Configure NuGet in Version Control

Meng Lin, in 15 April 2013
When enjoying the convenience NuGet brings us, it is easy to forget the fact that the references will come back to bite you the hardest, specially when working with version control.

This time, I hit a jackpot checkout my project from TFS in a new device, and realise all my dependencies all gong like they never existed, only leaving the NuGet config file proving I wasn’t day dreaming before.

This actually prevents me from using Ninject.MVC3 like I used to do in MVC 3. Even more unfortunately is that Ninject package for MVC 4 is not out yet, that why this alternative solution is used.

Therefore, I would highly recommend the following practices for the people who’d like to keep their project clean and tidy: no unnecessary package or exes checked in, but still be able to use the dependencies when the project is checked out.

Enabling package restore during build in Visual Studio options

Enable package restore in options
Enable package restore in options

Enabling package restore in solution

Enable package restore in solution
Enable package restore in solution

And you should be able to see this

Files generated by enabling pakcage restore
Files generated by enabling pakcage restore

But it is not the end of the story, simply because you don’t want to break the rules to check in any .dll or .exe.

Force NuGet.exe to download whenever needed

Open up NuGet.targets, find DownloadNuGetExe, and set the value to true.

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

Configure proxy to allow download

If you are working within an enterprise network, the auto-download of ‘nuget.exe’ might not work. In which case, you will have to configure a proxy for it.

Open up NuGet.targets again, find task DownloadNuGet, and configure the snippet with your proxy and credentials.

<Code Type="Fragment" Language="cs">
    <![CDATA[
    try {
        WebProxy proxy = new WebProxy(<proxy address>, <port>);
        proxy.Credentials = new NetworkCredential(<user name>, <password>, <domain>);
        OutputFilename = Path.GetFullPath(OutputFilename);

        Log.LogMessage("Downloading latest version of NuGet.exe...");
        WebClient webClient = new WebClient();
        webClient.Proxy = proxy;
        webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);

        return true;
    }
    catch (Exception ex) {
        Log.LogErrorFromException(ex);
        return false;
    }
]]>
</Code>

Some more tips

To keep things neat and tidy, you can now delete the ‘packages’ folder, because that is the whole point of doing this, right? To remove the redundant dependency files, which saves not just space but also time checking out or cloning depends on what version control mechanism you use, instead allowing build event to retrieve them back automatically.

You can now check in the changes to TFS, and you will find your TFS build server and your check out are working again!

References

Using Nuget without committing packages

Prevent needing to add nuget.exe to source control