Using custom nuget.exe in VSTS build process

Visual Studio General

7 years ago

Soon after the release of Visual Studio 2017, the Visual Studio Team Services team has added a Hosted VS2017 build agent that has support for all the latest and greatest technologies. Unfortunately although the build task with Visual Studio 2017 is itself present, the newest version of NuGet wasn't added, yet. Fortunately, it is possible to use a custom nuget.exe to circumvent this issue and be able to restore packages for project using the new csproj format with <PackageReference>.

Getting the latest nuget.exe

The supported versions of NuGet are available for download from the official website. Currently the recommended version is 3.5.0, but the version included with Visual Studio 2017 is 4.0.0

Versions of nuget.exe for download

Versions of nuget.exe for download

Because we have to be able to reference the downloaded executable during the build process, we have to check it in the source control. I placed it in a t ools folder in the root of my repository.

NuGet in tools folder

NuGet in tools folder

Setting the custom nuget.exe in VSTS

After the executable is checked in the desired branch of our team project's repository, we go to the Build & Release section of the dashboard and create a build definition or open the desired one for editing. In the task list select NuGet restore task.

NuGet task

NuGet task

Let's find the Advanced section in the details for the task on the right-hand side and configure it to use custom nuget.exe and then set the relative path to nuget.exe in your repository.

Configuring custom nuget.exe

Configuring custom nuget.exe

Last final step is to make sure we are using the new Hosted VS2017 build agent in the Options tab of the build editor.

New HostedVS2017 build agent

New HostedVS2017 build agent

We can now save the changes and queue the build to check if everything works as expected.

Summary

Although the NuGet build task hasn't been updated to support the 4.0.0 version yet, we can easily fix this problem by configuring a custom nuget.exe to be used.