MSBuild restore target - MSB4057: The target "restore" does not exist in the project
Asked Answered
I

2

19

We have over 20 solutions in our main product portfolio (over 880 projects), and we have a complex set of build scripts that work well, but we are trying to automate the restore of nuget packages from within the msbuild pipeline. Currently this is done with a manual call out to nuget to restore the packages.

According to https://learn.microsoft.com/en-us/nuget/schema/msbuild-targets I should be able to run this command and have the restore run:

# works
& $script:msBuildPath $solutionName /t:build /v:q /clp:ErrorsOnly /nologo /p:...
# doesn't works
& $script:msBuildPath $solutionName /t:restore,build /v:q /clp:ErrorsOnly /nologo /p:...

However, when I add the restore, to the above it throws an error

MSB4057: The target "restore" does not exist in the project

Where do I start researching to understand why it can't find this target? We are predominantly on VS 2015 and .NET 4.6.2, so anything specific to VS2017 is not an option for me at this time.


If I omit the /v:q and /clp:ErrorsOnly flags I do get this (slightly sanitized solution/project names and paths)

PS Z:\git\company> build c
building Common\Common.sln
Build started 11/15/2017 11:08:54 AM.
     1>Project "Z:\git\company\Common\Common.sln" on node 1 (restore;build target(s)).
     1>ValidateSolutionConfiguration:
         Building solution configuration "DEBUG|Any CPU".
     1>Z:\git\company\Common\Common.sln.metaproj : error MSB4057: The target "restore" does not exist in the project. [Z:\git\company\Common\Common.sln]
     1>Done Building Project "Z:\git\company\Common\Common.sln" (restore;build target(s)) -- FAILED.

Build FAILED.

       "Z:\git\company\Common\Common.sln" (restore;build target) (1) ->
         Z:\git\company\Common\Common.sln.metaproj : error MSB4057: The target "restore" does not exist in the project. [Z:\git\company\Common\Common.sln]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.03
runBuildPackage :  ... failed
At Z:\git\company\psincludes\buildFunctions.ps1:11 char:5
+     runBuildPackage "Common\Common.sln"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,runBuildPackage


msbuild failed
At Z:\git\company\psincludes\buildInternals.ps1:63 char:21
+                     throw "msbuild failed";
+                     ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (msbuild failed:String) [], RuntimeException
    + FullyQualifiedErrorId : msbuild failed

I realize some of this around is our internal tooling, I just didn't feel the need to obscure that part.

According to "the internet" this should just be "free" as part of the build process, and I'm not sure what we are missing in the csproj files here.

Iridescent answered 15/11, 2017 at 17:15 Comment(0)
B
22

The msbuild-integrated NuGet functionality is available in NuGet 4.0+ in conjunction with MSBuild 15, which means only in VS 2017. There is no support for the restore target for VS 2015.

The integrated restore in VS 2017 / NuGet 4 only works for projects using the new PackageReference style of referencing NuGet packages. It does not work for projects using packages.config. This new way of referencing packages is the default option for ASP.NET Core (both on .NET Framework / .NET Core), .NET Core and .NET Standard projects. It is opt-in for all other project types by selecting the style before the first install in the NuGet properties (Tools->Options->NuGet).

Note that calling /t:Restore,Build is not a good way to call this target, since restore may generate or change files that msbuild then doesn't reload. MSBuild 15.5 (upcoming update to VS 2017) introduces a /restore option instead that will call the restore target, then clear all caches which could not be cleared before and execute the normal build as requested. Before 15.5, it is best to make two distinct calls to msbuild.

Bohi answered 15/11, 2017 at 18:50 Comment(5)
So to confirm, with VS2015 the only way to get restore functionality is from running this manually via nuget restore? Sorry if you feel this is obvious, but I'm still a tiny bit fuzzy so want to make sure I'm solid. Thanks again!Iridescent
exactly. (I totally understand your confusion considering the current state of the documentation)Bohi
+1 because of the /restore. so many changes and so many options...had to try ever single combination for my project to build in the server and create the package the way I wanted.Roesler
Recommended is msbuild -t:build -restore, see Restoring and building with one MSBuild commandObtuse
Please note that msbuild -restore -p:RestorePackagesConfig=true is needed if the packages are specified using packages.config files. (As mentioned in the documentation page linked from the previous comment.) If desired, these parameters can instead be added to a file named “Directory.Build.rsp” – see: learn.microsoft.com/en-us/visualstudio/msbuild/…Dekeles
C
0

This does not seem to work for simple .proj files with MSBuild 17.9 (VS2022) However, i've found a workaround adding Sdk="Microsoft.NET.Sdk" to the <Project/> element. Thus, PackageReference items are now restored successfully with msbuild -restore

Crisscross answered 10/4 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.