21
votes

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?

9

9 Answers

15
votes

Try this package:

Install-Package NuGetEnablePackageRestore 
14
votes

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>
5
votes

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.

1
votes

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>
0
votes

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

<RestorePackages Condition="  '$(RestorePackages)' == '' ">true</RestorePackages>
0
votes

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.

0
votes

Run this command to fix the NuGet Enable Package

Install-NuGetEnablePackageRestoreFix

Then after that run Enable command

Install-Package NuGetEnablePackageRestore

0
votes

Install-Package NuGetEnablePackageRestore

-2
votes

-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