6
votes

While trying to rig up a solution to build with Cake v0.19.1 on a machine that has only ever known Visual Studio 2017, I can't seem to get NuGetRestore to accept a setting of MSBuildVersion = NuGetMSBuildVersion.MSBuild15.

Is there some magic step to getting a specific MSBuild version into NuGetRestore that I am missing?

Output

...

========================================
RestoreNuGet
========================================
Executing task: RestoreNuGet
Failed to load msbuild Toolset
  Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
An error occurred when executing task 'RestoreNuGet'.
Error: NuGet: Process returned an error (exit code 1).

Trimmed-down build.cake

var target = Argument("target", "Default");
var solution = "./some-random.sln";

Task("Default")
.Does(() => {
    NuGetRestore(
        solution,
        new NuGetRestoreSettings {
            MSBuildVersion = NuGetMSBuildVersion.MSBuild15,
        }
    );
});

RunTarget(target);

Update: getting NuGet v4

Per @devlead's answer, I pointed the build.ps1 file at the v4.0.0 of NuGet and got this output.

Cannot find the specified version of msbuild: '15'
An error occurred when executing task 'RestoreNuGet'.
Error: NuGet: Process returned an error (exit code 1).

In my full build.cake, I use vswhere for later MSBuildSettings which I can get to dump out the MSBuild path it found (and I confirmed that exe exists in Explorer).

C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/Bin/amd64/MSBuild.exe
2

2 Answers

4
votes

What you could try is to use the MSBuild alias with the restore target, latest version of MSBuild should have build in NuGet support.

MSBuild(
    "./some.sln",
    configurator => configurator.WithTarget("restore"));
2
votes

Make sure you're using the latest version of NuGet.exe, currently it's v4.0.0 which is the latest version, but you can also see a list of available at https://dist.nuget.org

If you're using the default build.ps1 you could modify it to always download specific version of NuGet.exe

You can do this be remove the Test.Path parts - so it won't look for nuget.exe any where else but your tools folder. Then change the download uri to not use latest stable (currently v3.5.0) but a specific version by in build.ps1 changing

$NUGET_URL = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"

to

$NUGET_URL = "https://dist.nuget.org/win-x86-commandline/v4.0.0/nuget.exe"

will ensure you always download v4.0.0 of the exe.

It's also possible with a little PowerShell to verify correct version in tools, example

if ((Get-ChildItem $NUGET_EXE `
    | % VersionInfo `
    | % ProductVersion `
    | ? { $_ -eq '4.0.0' }|Measure-Object).Count -eq 1)
{
   'Correct version'
} else {
   'Incorrect version'
}