2
votes

I am trying to run MSBuild via a command line and cannot get the NuGet packages to restore using the /t:restore switch as documented here.. The solution is a fairly simple ASP.NET MVC app and a MSTest project. If I run the restore flag on either my local dev folder or from the build server I see no NuGet packages being restored, even if I delete the packages, obj, and bin folders.

I have enabled auto download as documented here. If I open the project with Studio and build it the packages are restored as expected. This only fails when MSBuild is being used.

This is all being done with Visual Studio 2017 at the latest patch level. The build machine has Professional, my machine has Enterprise. Both have the same behavior.

Has anyone gotten MSBuild to restore NuGet packages using MSBuild 15.1?

C:\Users\me\Documents\Visual Studio 2017\Projects\cmbs-admin>"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe" /t:Restore
Microsoft (R) Build Engine version 15.1.1012.6693
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "/m"switch.
Build started 6/22/2017 2:01:57 PM.
Project "C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject\MyProject.sln" on node
 1 (Restore target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|Any CPU".
Restore:
  Restoring packages for C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject\MyProject.Tests\MyProject.Tests.csproj...
  Restoring packages for C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject\MyProject\MyProject.csproj...
  Committing restore...
  Committing restore...
  Lock file has not changed. Skipping lock file write. Path: C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject\MyProject\obj\project.assets.json
  Lock file has not changed. Skipping lock file write. Path: C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject\MyProject.Tests\obj\project.assets.json
  Restore completed in 350.01 ms for C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject\MyProject\MyProject.csproj.
  Restore completed in 349.87 ms for C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject\MyProject.Tests\MyProject.Tests.csproj.

  NuGet Config files used:
      C:\Users\me\AppData\Roaming\NuGet\NuGet.Config
      C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

  Feeds used:
      https://api.nuget.org/v3/index.json
      C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
Done Building Project "C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject\MyProject.sln" (Restore target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.18

C:\Users\me\Documents\Visual Studio 2017\Projects\MyProject>
1
What kind of project do you try to restore upon? NuGet.exe restore is still the option for non-.NET Core projects.Lex Li
Its a traditional CSPROJ based project. Are you saying Nuget.exe is THE option or AN option? blog.nuget.org/20170316/…Lane Goolsby
I found that by changing the CSPROJ to use the PackageReference syntax instead of Reference (as noted in the link in my previous comment) that things work. It appears this is a issue with the MVC Web App CSPROJ template, not with MSBuild.Lane Goolsby

1 Answers

1
votes

Confirmed my comment above as a (painful yet effective) fix.

I had to crack open the CSPROJ in a text editor and replace all the Reference elements with PackageReference elements.

For example:

<Reference Include="WebGrease">
  <Private>True</Private>
  <HintPath>..\packages\WebGrease.1.5.2\lib\WebGrease.dll</HintPath>
</Reference>

Becomes:

<PackageReference Include="WebGrease">
  <Version>1.6.0</Version>
</PackageReference>

All future NuGet adds respect the PackageReference syntax and seem work well with MSBuild. This is why I suspect the issue with the MVC app project type template and not MSBuild or NuGet.