NUKE Build: Could not find a suitable MSBuild instance (Visual Studio 2022)

5.8k Views Asked by At

After upgrade Visual Studio 2019 to 2022, I got the following error when I try to build the project with NUKE Build (Debug/Release):

╬════════════
║ Compile
╬═══

Assertion failed: Could not find a suitable MSBuild instance.
   at Nuke.Common.Tools.MSBuild.MSBuildToolPathResolver.Resolve(Nullable`1 msBuildVersion, Nullable`1 msBuildPlatform)
   at Nuke.Common.Tools.MSBuild.MSBuildSettings.GetProcessToolPath()
   at Nuke.Common.Tools.MSBuild.MSBuildSettings.get_ProcessToolPath()
   at Nuke.Common.Tooling.ProcessTasks.StartProcess(ToolSettings toolSettings)
   at Nuke.Common.Tools.MSBuild.MSBuildTasks.MSBuild(MSBuildSettings toolSettings)
   at Nuke.Common.Tools.MSBuild.MSBuildTasks.MSBuild(Configure`1 configurator)
   at AREGIS.Build.DeagBuild.<get_Compile>b__21_1() in C:\Work.Vertex\Vertex\40 Build\DeagBuild.cs:line 98
   at Nuke.Common.Execution.BuildExecutor.<>c.<Execute>b__4_2(Action x)
   at Nuke.Common.Utilities.Collections.EnumerableExtensions.ForEach[T](IEnumerable`1 enumerable, Action`1 action)
   at Nuke.Common.Execution.BuildExecutor.Execute(NukeBuild build, ExecutableTarget target, IReadOnlyCollection`1 previouslyExecutedTargets, Boolean failureMode)


Repeating warnings and errors:
Assertion failed: Could not find a suitable MSBuild instance.

Compile method:

        Target Compile => _ => _
        .DependsOn(this.RestoreFramework)
        .Executes(() =>
        {
            var compileOutput = MSBuild(x => x
                .SetTargetPath(this.MySolution)
                .Set...
                .Set...
                );
        });

Target framework: .NET Framework 4.8

Is there any way to fix it?

3

There are 3 best solutions below

3
On BEST ANSWER

I have to set process tool path. I fixed it by adding the path of MSBuild.exe

.SetProcessToolPath(@"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe")

The compile method should be:

 Target Compile => _ => _
    .DependsOn(this.RestoreFramework)
    .Executes(() =>
    {
        var compileOutput = MSBuild(x => x
            .SetProcessToolPath(@"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe")
            .SetTargetPath(this.MySolution)
            .Set...
            .Set...
            );
    });
0
On

To add to this answer: https://stackoverflow.com/a/70097525/569302

I am using Visual Studio Community 2022 right now:

For me the path was: C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe

0
On

A better way to solve this issue is using environment variable MSBUILD_EXE which should point to MSBuild.exe. Then instead of hardcoding path in sources use property MSBuildTasks.MSBuildPath:

MSBuildTasks.MSBuild(settings => settings
    .SetProcessToolPath(MSBuildTasks.MSBuildPath)
    .EnableRestore()
    .SetSolutionFile(solution)
    .SetConfiguration(Configuration));