NUKE Build:找不到合适的 MSBuild 实例(Visual Studio 2022)

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

升级 Visual Studio 2019 到 2022 后,当我尝试使用 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 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.

编译方法:

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

目标框架:.NET Framework 4.8

有什么办法可以解决吗?

我必须set process tool path。我通过添加 MSBuild.exe

的路径来修复它
.SetProcessToolPath(@"C:\Program Files\Microsoft Visual Studio22\Enterprise\MSBuild\Current\Bin\MSBuild.exe")

编译方法应该是:

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

要添加到此答案:

我现在正在使用 Visual Studio Community 2022:

对我来说,路径是:C:\Program Files\Microsoft Visual Studio22\Community\MSBuild\Current\Bin\MSBuild.exe

解决此问题的更好方法是使用应指向 MSBuild.exe 的环境变量 MSBUILD_EXE。然后在源中使用 属性 MSBuildTasks.MSBuildPath:

而不是硬编码路径
MSBuildTasks.MSBuild(settings => settings
    .SetProcessToolPath(MSBuildTasks.MSBuildPath)
    .EnableRestore()
    .SetSolutionFile(solution)
    .SetConfiguration(Configuration));