21

I have set up NuGet Package Restore on my solution and it works nicely on my local machine. I followed the instructions supplied here:

http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages

The problem I have is on my build server where the following error occurs:

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'.

Unfortunately I dont have access to the build server as it is controlled off site so cant update the environment variable. Is there any other way around this? Anything I can add to the solution file or similar that would allow the package restore?

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
amateur
  • 43,371
  • 65
  • 192
  • 320

9 Answers9

15

Try this package:

Install-Package NuGetEnablePackageRestore 
Xavier Decoster
  • 14,580
  • 5
  • 35
  • 46
  • Thanks - does this package need to be installed on all projects in the solution or just the main project eg web app? – amateur Oct 08 '12 at 22:10
  • 1
    You can install it once and then follow the instructions that will appear in the Library Package Manager Console. After completing the instructions, you can uninstall it again. – Xavier Decoster Oct 09 '12 at 17:53
  • 1
    Uses a nifty workaround to set the required 'EnableNuGetPackageRestore' environment variable as part of the build – Dan Esparza May 22 '13 at 19:25
  • +1. Using the SET statement on the server, and applying the environment variable through control panel hadn't worked for me, but this did :) One Q @XavierDecoster - why would I want to uninstall it again? Wouldn't this re-break the build? – Fetchez la vache Jul 11 '13 at 09:29
  • because the package will inject the PowerShell scripts into your NuGet PowerShell profile :) it won't break the build as the build is not relying on this package which only adds a new Powershell command in the VS Package Manager Console. – Xavier Decoster Jul 11 '13 at 09:56
14

NuGet can use local settings for it's behavior which can be unpredictable if you're not 100% sure how the server is configured.

I prefer putting the NuGet settings inside the <sln root>/.nuget/NuGet.targets file which is version controlled and at a single location. I got this working with 3 quick edits to <sln root>/.nuget/NuGet.targets, they should look as below after editting:

Change 1:

<!-- Enable the restore command to run before builds -->
<RestorePackages Condition="  '$(RestorePackages)' == '' ">true</RestorePackages>

Change 2:

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">false</RequireRestoreConsent>

My comment: Awkward logic but think of "requires consent not equal to false must be true" (original) as "requires consent equal to true must be true" (translated) and it makes sense to change the last part to "false" (the edit)

Change 3 : I also added/uncommented the <PackageSource ... > tag to to remove any dependencies on the

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <PackageSource Include="https://nuget.org/api/v2/" />        
</ItemGroup>
DeepSpace101
  • 13,110
  • 9
  • 77
  • 127
5

I came across this issue when I tried building one of my projects with Jenkins, and managed to get it work by simply changing one value in the .nuget\NuGet.targets file from true to false.

I changed:

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>

to

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">false</RequireRestoreConsent>

Notice the element value has changed. Hope this helps.

yyoon
  • 3,555
  • 3
  • 21
  • 17
  • This worked for me as my msbuild server are not on-premises . I have a solution with 20+ projects and one of them were added recently with some custom packages (log4net, json.net) and was causing the problem. – jpgrassi Nov 06 '15 at 13:17
1

In %appdata%\NuGet\NuGet.Config add the following section inside

    <packageRestore>
    <!-- Package Restore and MSBuild-Integrated Package Restore -->
    <add key="enabled" value="True" />

    <!-- Automatic Package Restore in Visual Studio -->
    <add key="automatic" value="True" />
  </packageRestore>

Full example

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <activePackageSource>
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
  </activePackageSource>
  <packageRestore>
    <!-- Package Restore and MSBuild-Integrated Package Restore -->
    <add key="enabled" value="True" />

    <!-- Automatic Package Restore in Visual Studio -->
    <add key="automatic" value="True" />
  </packageRestore>
</configuration>
Aussie Ash
  • 1,276
  • 12
  • 10
0

I probably could try to set RestorePackages property to true in .nuget\NuGet.targets file:

<RestorePackages Condition="  '$(RestorePackages)' == '' ">true</RestorePackages>
asa
  • 1,827
  • 2
  • 15
  • 18
  • Just tried this and it didnt do the trick, thanks for your input though. – amateur Oct 08 '12 at 20:06
  • Have you checked the csproj file property `true` ? – asa Oct 08 '12 at 20:09
  • In all projects within the solution? – amateur Oct 08 '12 at 20:12
  • 1
    I believe in all projects which uses NuGet. BTW, you could also try to restore packages localy: just remove all packages from FS and try to run `msbuild yoursolution.sln` command. It should highlight more details. – asa Oct 08 '12 at 20:16
  • 1
    The project has over 30 projects so having to edit each of them is not ideal. Thanks for the suggestion though. – amateur Oct 08 '12 at 20:19
  • You might set restore packages for web project first and when publish it. – asa Oct 08 '12 at 20:39
0

For anyone stumbling upon this question, looking for a way to get packages restored on a build server, NuGet Package Restore gives a nice overview of current options.

I Chose to use the Command-Line Package Restore approach. It's as easy as issuing the following command line:

C:><path to nuget.exe> restore <path to solution.sln>

nuget.exe an be obtained from https://docs.nuget.org/consume/installing-nuget. I used version Command-Line Utility Latest 3.X.

R. Schreurs
  • 8,587
  • 5
  • 43
  • 62
0

Run this command to fix the NuGet Enable Package

Install-NuGetEnablePackageRestoreFix

Then after that run Enable command

Install-Package NuGetEnablePackageRestore

Usman Hayat Khan
  • 196
  • 1
  • 3
  • 14
0

Install-Package NuGetEnablePackageRestore

Anup Shetty
  • 571
  • 8
  • 10
-2

-Go to Tools -> Library Package Manager -> "Package Restore" -> uncheck "Allow NuGet to download missing packages" and "Automatically check..."

  • Rebuild Solution

  • Clean Solution

  • Now check "Allow NuGet to download missing packages" and "Automatically check..."

  • Rebuild Solution

Hernaldo Gonzalez
  • 1,977
  • 1
  • 21
  • 32