Azure DevOps - Always Be Shipping!

Azure DevOps build fails with “The nuget command failed with exit code(1) and error(Cannot determine the packages folder to restore NuGet packages.”

This post was most recently updated on November 11th, 2019.

Reading Time: 2 minutes.

This was another peculiar one – something, that didn’t bring up too many results on Google. Always fun trying to figure out those!

So, when configuring an Azure DevOps pipeline (build) for a .NET project, you run into this annoying error:

##[error]The nuget command failed with exit code(1) and error(Cannot determine the packages folder to restore NuGet packages. Please specify either -PackagesDirectory or -SolutionDirectory.

Job: "The nuget command failed with exit code(1) and error(Cannot determine the packages folder to restore NuGet packages. Please specify either -PackagesDirectory or -SolutionDirectory. System.InvalidOperationException: Cannot determine the packages folder to restore NuGet packages. Please specify either -PackagesDirectory or -SolutionDirectory. at NuGet.CommandLine.RestoreCommand.GetPackagesFolder(PackageRestoreInputs ...

So, this is what I had. And this should work most of the time – just in my case, it didn’t!

- task: [email protected]
  inputs:
    restoreSolution: '$(solution)'

Solution

I hadn’t seen this ever before, and embarked upon furious googling. Very few results showed up, though, which is kinda weird with Azure DevOps – so many people are already sharing their experiences and best practices, that usually you can find someone who’s solved all the issues you might run into!

However, in this particular case I just ended up taking a few stabs in the dark and guessing what’s the kind of a value Azure DevOps might want for “packagesdirectory”. While I don’t know if it made a difference or not, the parameter name is in lowercase for me.

After a few different tries, this is how I fixed it – hopefully it works for you as well!

- task: [email protected]
  inputs:
    restoreSolution: '$(solution)'
    packagesdirectory: '..\packages'

This way, the NuGet packages are restored to the solution level – as I’m actually building a single project inside a folder containing a solution with quite a few projects.

Finally, in case someone is interested, this is what my whole YAML for this very simple build pipeline looks like:

Example YAML for building ASP.NET Projects
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- master
 
# this particular agent was required to make the build run for .NET Framework
pool:
  vmImage: 'VS2017-Win2016'

variables:
  solution: '**/[projectname].csproj'
  buildPlatform: 'AnyCPU'
  buildConfiguration: 'Debug'

steps:
- task: [email protected]

- task: [email protected]
  inputs:
    restoreSolution: '$(solution)'
    packagesdirectory: '..\packages'

- task: [email protected]
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: [email protected]
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

Antti K. Koskela

Antti Koskela is a proud digital native nomadic millennial full stack developer (is that enough funny buzzwords? That's definitely enough funny buzzwords!), who works as a Solutions Architect for Valo Intranet, the product that will make you fall in love with your intranet. Working with the global partner network, he's responsible for the success of Valo deployments happening all around the world.

He's been a developer from 2004 (starting with PHP and Java), and he's been bending and twisting SharePoint into different shapes since MOSS. Nowadays he's not only working on SharePoint, but also on .NET projects, Azure, Office 365 and a lot of other stuff.

This is his personal professional (e.g. professional, but definitely personal) blog.
mm

Leave a Reply

avatar
5000
  Subscribe  
Notify of