6

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
Community
  • 1
  • 1
patridge
  • 26,385
  • 18
  • 89
  • 135

2 Answers2

4

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"));
devlead
  • 4,935
  • 15
  • 36
  • It looks like this configurator setting is going to work (in combination with the `$NUGET_URL` redirect answer to get NuGet v4). Any chance you know how to translate this to a `MSBuildSettings` constructor variant? The obvious solution of using the `Targets` property failed since it is read-only. – patridge Apr 02 '17 at 02:44
  • It may have been a fluke that my build worked. I must have already done a restore in VS. Instead this specific step is saying it didn't do anything: `Nothing to do. None of the projects specified contain packages to restore.` – patridge Apr 02 '17 at 02:58
2

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'
}
devlead
  • 4,935
  • 15
  • 36
  • I gave that a shot and updated the question with the new error output. I'm wondering if it is looking in the wrong location for `NuGetMSBuildVersion.MSBuild15`. – patridge Mar 30 '17 at 22:16
  • With NuGet v4 in place, I can restore successfully by simply removing the `MSBuildVersion = NuGetMSBuildVersion.MSBuild15` from my original `Restore` call. – patridge Apr 02 '17 at 03:24