Nuget: Switching from "Enable Package Restore" to "Automatic Package Restore"

So it seems that starting with Nuget 2.7 and Visual Studio 2013, it’s no longer advised to use the “Enable NuGet Package Restore” feature (see image below). 

image 

Why?  Well now there is “Automatic Package Restore”, which is a feature that allows VS 2013 to automatically download missing packages as part of the build.  Again, see the image below.

image

But… there are a few catches.

First, this means that any build servers that use MSBuild will no longer automatically download missing packages.  You’ll need to script that out using “nuget.exe restore”.  (The positive side of this is that in some situations, like ours, the Enable Package Restore option was causing conflicts in our build server.  I won’t go into details but it had to do with multiple solutions referencing the same projects and bad HintPath values getting set.

Second, you need to go through some pains to extricate yourself from “Enable Package Restore” before you can successfully work with “Automatic Package Restore”.

What pains you ask?

Well first in your solution you should delete the .nuget/Nuget.exe and .nuget/Nuget.targets files.

Second, your project files (vbproj, csproj etc) you should remove these lines:

<RestorePackages>true</RestorePackages>

 

AND
 
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">  
    <PropertyGroup>    
        <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>  
    </PropertyGroup>  
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Actually I’m not sure if the first part dealing with the RestorePackages element needs to be removed.  I had found a few people saying you should remove it, but leaving it seems to not cause any problems (for me).

If you are doing a build and you get seeing errors like this:

Error    2    This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is C:\Data\Whatever\\.nuget\NuGet.targets.   

Then it means that you have some of the stuff in the project file still and you need to remove it before you can try building.

After doing this, if you then delete your solutions Packages folder and do a rebuild you should see Visual Studio automatically downloading the missing packages:

image

There have been a couple times where it seemed after making these changes I needed to close Visual Studio and reopen it.  But after that everything worked.

One thought on “Nuget: Switching from "Enable Package Restore" to "Automatic Package Restore"

  1. Pingback: Abandoning Nuget Automatic Package Restore | Chris May · { .Net Development;}

Leave a comment