Getting the VSTS Hosted Mac Agent to restore from your custom NuGet sources

While preparing for my recent talk on Visual Studio App Center, I realised that there were a couple of limitations in the current offering that would make it difficult to implement an upcoming requirement in one of my projects. Knowing that VSTS recently announced a hosted macOS build agent preview - and indeed, that App Center's Build offering is implemented by VSTS behind the scenes - I thought I'd give setting up a build in VSTS a try, but I hit a small hitch.

It's very easy to get started, with dedicated Xamarin build templates in VSTS and plenty of posts from James Montemagno on the topic (protip - check his blog as well as the official Xamarin one for a bunch of good posts by him). In a short amount of time I had VSTS builds working using macOS agent running on my own machine, hurrah! Switching the deployment pool over to 'Hosted macOS Preview', I queued another build and prepared myself for another green bar, but alas! The build failed during NuGet restore.

the dreaded error

Imagine this error repeats for 12 more packages because I ran out of redaction tape after covering the first.

In this project I have a couple of custom NuGet sources, with details specified in a NuGet.config file. The last source - which contains the missing packages - requires authentication, and I suppose that is related to the issue. In any case I was stuck, but confused - the restore works fine on my local agent, but also via App Center - which is likely to be running on machines in the same pool - so what gives?

Many builds were lost to the tedious process of switching between the NuGet Restore task and the 'Restore NuGet packages' option on the build definition, as well as bunch of other experiments involving the way the NuGet.config path was specified, without any luck. Fortunately, I found a workaround by inspecting the outputs of a successful restore for an earlier build run in App Center:

the output from app center
oh i seeeeeeee..

So it appears that for whatever reason, App Center does not use the 'NuGet Restore' task or the flag available on the 'Build Xamarin iOS solution' build task. Instead, it calls out to NuGet from a command line task. NuGet proper picks up the NuGet.config next to the solution and everything restores hunky dory. So, I switched out the 'Restore NuGet Packages' task in my VSTS build definition with that one.

the winning task

If you have the problem I had, you can use this task definition:
Task: Command Line
Tool: /bin/bash
Arguments: -c "/usr/bin/find . -name '*.sln' -print0 | /usr/bin/xargs -0 -n1 nuget restore -DisableParallelProcessing"
(Note the quotation marks around the shelled command - not displayed in the app center logs)

That's it - finally, back to the green bar 😎