NuGet; keep your package folder out of the VCS

I am using Mercurial since long time ago, and yesterday I was fighting to push a repository to codeplex. It is well known fact than Mercurial is bad when it comes to large files (yes, it is worse than Git or even SVN).

After all you might wonder if it is right to push all those files to the [D]CVS. The important information for recreating the whole package folder is in your “packages.config” file.

My friend Simon Cropp gave me yesterday this chunk of msbuild configuration:

  <Target Name="BeforeBuild">
    <Exec Condition="Exists('$(ProjectDir)packages.config')" 
            Command="&quot;$(SolutionDir)Tools\nuget.exe&quot; install &quot;$(ProjectDir)packages.config&quot; -o &quot;$(SolutionDir)Packages&quot;" />

Which come from a David Ebbo post. Paste this chunk of code at the end of your csproj files.. before the closing </Project> tag.

What it does? It will read your “packages.config” file and look for your Packages folder. If your packages folder doesn’t have one of the packages, nuget.exe will fetch those from the nuget repository. Be aware that this is not going to fetch the latest version of a package but the same version your packages.config say you are using.

As you can see it is a “BeforeBuild” task, it means that this task will run before compiling your code.

The next thing you should do is to ignore the packages folder in hgignore with:


The “condition” of the task is interesting. I agree with my friend Simon, if you are not longer using nuget in a project or you are not using it yet you don’t want to be inserting or removing the task. It could be even in your default project template.

The first time you build your solution, you will see something like this:


Do I need Internet to work this way? Yes! and you need internet for a bunch of other things.

Note: Use this method for new projects. If you already have the package folder in source control it is not worth deleting it, for obvious reasons :)

blog comments powered by Disqus