Blogs

Enable NuGet package restore in Visual Studio and TFS 2012 RC to building Windows 8 Metro Apps

In the previous post (only in Italian for now, sorry) we created a new build machine with Windows 8 RP and TFS Build Agent 2012 RC to building new Windows 8 Metro Applications.

Having a build process for each solution is very useful, but we can have some errors like these:

  • C:\Builds\[…]\.nuget\nuget.targets(76,9): error : Package restore is disabled by default. To give consent, open the Visual Studio Options dialog, click on Package Manager node and check ‘Allow NuGet to download missing packages during build.’ You can also give consent by setting the environment variable ‘EnableNuGetPackageRestore’ to ‘true’.
  • C:\Builds\[…]\.nuget\nuget.targets(76,9): error MSB3073: The command “”C:\Builds\[…]\.nuget\nuget.exe” install “C:\Builds\[…]\packages.config” -source “” -o “C:\Builds\[…]\packages”” exited with code 1.

We use NuGet in Visual Studio 2012 RC with TFS 2012 RC: we want to enable missing NuGet packages restoring and we would prefer to not check-in packages into source control. So, in Visual Studio Solution Explorer, right click on solution and click on “Enable NuGet Package Restore”:

Solution: Enable NuGet Package Restore

Do you want to configure this solution to download and restore missing NuGet packages during build? Yes, of course:

NuGet Package Manager: Enable NuGet Package Restore

A .nuget folder is added to the root of the solution.

NuGet Package Manager: finished configuring this solution to restore NuGet packages on build

In Visual Studio, Check In the pending changes and go to Tools –> Options –> Package Manager: “Allow NuGet to download missing packages during build” is flagged:

Package Manager: Package Restore: Allow Nuget to download missing packages during build

All right! In the Solution Explorer check if there is the “.nuget” folder in your Solution. In the “packages” folder on Source Control, delete all the packages you don’t want they exist in source control, but keep the “repositories.config” file:

NuGet folders and files in Visual Studio 2012 RC

NOTE: When you delete something from Source Control in Visual Studio, you are doing a logical delete. You can phisically delete version-controlled files and folders from Team Foundation version control by using the “tf destroy” command.

Now, right click on the Solution –> Manage NuGet Packages for Solution…

Solution: Manage NuGet Packages for Solution

If some NuGet packages are missing from you solution, click on Restore button:

Solution - Manage NuGet Packages: Restore

NuGet downloads missing packages:

Solution - Manage NuGet Packages: Downloading missing packages

And new packages are restored:

Solution - Manage NuGet Packages: New packages restored by NuGet

Let we see the Source Control: we have a .nuget folder with the NuGet application files (NuGet.exe, NuGet.targets and NuGet.Config):

.nuget folder: NuGet.Config, NuGet.exe, NuGet.targets

In the “packages” folder we have a “repositories.config” file:

packages folder: repositories.config

NuGet uses this file (repositories.config) to locate the packages.config files of each project in the solution:

<?xml version="1.0" encoding="utf-8"?> 
<repositories> 
 <repository path="..\ProjectName1\packages.config" /> 
 <repository path="..\ProjectName2\packages.config" /> 
 […] 
</repositories>

Each project with at least one NuGet package has the packages.config file:

project folder: packages.config

Each packages.config file contains the packages list with the id and other properties:

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
 <package id="package1"version="1.0.0" /> 
 <package id="package2" version="1.0.1" />  
 […] 
</packages>

Lastly, on the Windows 8 build machine add the environment variable to Enable NuGet Package Restore:

  • Variable name: EnableNuGetPackageRestore
  • Variable value: true

Environment Variable: EnableNuGetPackageRestore

IMPORTANT: Use System Variable! Otherwise, you can add a User Variable if you are logged with the TFS Build Service account (for example: domain\tfsbuild).

Restart the build machine and queue the build!

2


Discussion

  1. Sviatlana  July 10, 2013

    Nice article.
    To Enable NuGet Package Restore you could use option packageRestore in NuGet.config from your solution (the NuGet version >= 2.5, to update use “nuget update -self” ):
    1) add to NuGet.config section

    2) add to RestoreCommand in NuGet.targets option like “-ConfigFile NuGet.Config”, eg.
    $(NuGetCommand) install “$(PackagesConfig)” -source “$(PackageSources)” $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir “$(SolutionDir) ” -ConfigFile “$(NuGetToolsPath)\NuGet.Config”

    (reply)
  2. shashank  December 6, 2013

    Dear Mr. Markp,
    I have seen your blog related to visual studio, it has come out very well. It has lot of information related to .net which is helpful to developers like us.

    We recently developed a product called ‘Tremplin’. This tool will develop complete database application without involving you to write single line of code. I request you to write one article about our product and publish in your site. Since your site attracts lot of visitors, this will be of great help to us and intern we can offer one year free license to you.

    You can also take complete 30 minute course about this product at
    https://www.udemy.com/codeless-database-application-development-tremplin

    Please see this intro video http://www.youtube.com/watch?v=vrbJ0HzcLZI

    If you want more information you may visit http://www.erachana.net

    Thanks and Regards
    Shashank Kamath
    Customer Relationship Manager
    ERachana Software
    http://www.erachana.net
    Email Id:Shashank.ess@gmail.com
    Mobile: +918147533797

    (reply)

Add a Comment