Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins not restoring NuGet packages with new MSBuild restore target

We have a .net full framework WPF application that we've moved from .net 4.6.2 to 4.7.1 along with changing to PackageReference in the csproj file instead of packages.config.

Building on the development machines appears to be fine and packages are downloaded and restored, but when we build on our Windows Server 2012 build server with Jenkins, the nuget packages don't seem to be restored correctly.

We're using MSBuild v15.5 with the latest "msbuild /restore" command to restore packages at build time. Note: Using the previous way of calling "nuget restore" does work, but we should be able to use msbuild /restore now.

The package restore process appears to be looking at the correct NuGet servers and appears to go through the restore without errors (this is a test solution compiled on Jenkins to isolate the issue):

Restore:   Restoring packages for c:\Jenkins\workspace\Test\ConsoleApp1\ConsoleApp1.csproj...   Committing restore...   Generating MSBuild file c:\Jenkins\workspace\Test\ConsoleApp1\obj\ConsoleApp1.csproj.nuget.g.props.   Generating MSBuild file c:\Jenkins\workspace\Test\ConsoleApp1\obj\ConsoleApp1.csproj.nuget.g.targets.   Writing lock file to disk. Path: c:\Jenkins\workspace\Test\ConsoleApp1\obj\project.assets.json   Restore completed in 577.05 ms for c:\Jenkins\workspace\Test\ConsoleApp1\ConsoleApp1.csproj.    NuGet Config files used:       c:\Jenkins\workspace\Test\NuGet.Config       C:\Windows\system32\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config    Feeds used:       http://devbuild/NuGetHost/nuget       https://api.nuget.org/v3/index.json Done Building Project "c:\Jenkins\workspace\Test\ConsoleApp1.sln" (Restore target(s)). 

But when msbuild comes to compile the code we get the following errors which looks like the NuGet hasn't been downloaded:

CSC : error CS0006: Metadata file 'C:\Windows\system32\config\systemprofile\.nuget\packages\log4net\2.0.8\lib\net45-full\log4net.dll'  could not be found [c:\Jenkins\workspace\Test\ConsoleApp1\ConsoleApp1.csproj] 

Any idea why the nuget packages aren't getting restored?

like image 279
mips Avatar asked Feb 21 '18 00:02

mips


People also ask

Does MSBuild restore NuGet packages?

msbuild -t:Restore will restore nuget packages for projects with PackageReference nuget management format.

How do I force a NuGet package to restore?

Enable package restore by choosing Tools > Options > NuGet Package Manager. Under Package Restore options, select Allow NuGet to download missing packages. In Solution Explorer, right click the solution and select Restore NuGet Packages.

How do I fix NuGet recovery failed?

Quick solution for Visual Studio usersSelect the Tools > NuGet Package Manager > Package Manager Settings menu command. Set both options under Package Restore. Select OK. Build your project again.

Was not found it might have been deleted since NuGet restore?

It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. Here are some actions you can take to resolve this error: Add the /restore option to your MSBuild.exe command.


1 Answers

After many hours of searching and sifting through NuGet issue posts and filtering out the .net core noise, I have a fix!

According to some NuGet and msbuild msbuild issues raised, when restoring with NuGet (or msbuild /restore) under the local system account in Windows Server 2012, the folder NuGet uses isn't accessible or it's a different folder due to 32 bit vs 64 bit process that is running so it can't download nugets to that local cache folder.

This folder that msbuild wants to look in at compile time seems to be C:\Windows\system32\config\systemprofile\.nuget\packages.

The solve for us was to set the NuGet package cache folder using the System wide environment variable NUGET_PACKAGES to a different, accessible folder such as C:\NugetPackageCache eg

NUGET_PACKAGES=C:\NugetPackageCache 

You can also set this per Jenkins project by setting the Build Environment->Inject environment variables to the build process->Properties Content to:

NUGET_PACKAGES=C:/NugetPackageCache 

Another potential solve according to this NuGet issue post is to set the environment variable to the folder that msbuild is looking for the nugets ie

NUGET_PACKAGES=C:\Windows\system32\config\systemprofile\.nuget\packages 

Note: The environment variables take precedence with NuGet. It doesn't look like they've updated the NuGet docs just yet to mention the precedence.

Note: To inject/set the environment variables we are using the EnvInject Jenkins plugin which looks like this:

Jenkins plugin with environment variables

like image 121
mips Avatar answered Sep 24 '22 00:09

mips