Azure Pipeline 在“dotnet build”上失败并出现错误 "command or file was not found"

Azure Pipeline fails on `dotnet build` with error "command or file was not found"

最近我的 Azure Pipeline 构建开始失败,我的构建没有任何更改 scripts/yaml。错误如下,但在细节上仍然很清楚。

C:\hostedtoolcache\windows\dotnet\sdk.1.406\FSharp\Microsoft.FSharp.Targets(279,9): error MSB6006: "dotnet.exe" exited with code 1. [D:\a\s\src\App.Core\App.Core.fsproj]

##[error]Error: The process 'C:\hostedtoolcache\windows\dotnet\dotnet.exe' failed with exit code 1

为了获得更多信息,我还在构建步骤中添加了 --verbosity detailed,它显示了以下详细信息。

Could not execute because the specified command or file was not found.
Possible reasons for this include:
    * You misspelled a built-in dotnet command.
    * You intended to execute a .NET Core program, but dotnet-@C:\Users\VssAdministrator\AppData\Local\Temp\tmp3147e93b44f0436c9449d0f384ba6079.rsp does not exist.
    * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

所以构建似乎在尝试使用 msbuild response file 时失败了。但是,这似乎是一个内部问题,而不是我的命令的问题。我不太了解 dotnet build 内部如何使用 msbuild

日志中还有警告和信息行。但是,它们不应该适用,因为我正在使用 dotnet cli 进行恢复并且我正在使用 UseDotNet 安装 SDK 版本 3.1.409global.json

中的相同版本
##[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build
Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://docs.microsoft.com/en-us/dotnet/core/tools/ and https://docs.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting

global.json 文件的内容:

{
  "sdk": {
    "version": "3.1.409",
    "rollForward": "latestFeature"
  }
}

在构建管道中,包在构建之前的单独步骤中恢复。还有一个项目 (dbup) 在完整构建 (dotnet build) 成功之前构建。虽然完整的构建步骤失败了。我已经包含了 azure-pipelines.yml 文件中的相关步骤。

    steps:
      - task: UseDotNet@2
        displayName: 'Use .Net Core sdk 3.1.409'
        inputs:
          version: 3.1.409

      - task: DotNetCoreCLI@2
        displayName: 'dotnet restore'
        inputs:
          command: restore
          projects: App.sln

      - task: DotNetCoreCLI@2
        displayName: 'dotnet build dbup'
        inputs:
          command: build
          projects: 'src\App.DatabaseUp\App.DatabaseUp.fsproj'
          arguments: '--no-restore --configuration release --version-suffix $(Build.BuildNumber)'

      - task: DotNetCoreCLI@2
        displayName: 'dotnet build'
        inputs:
          command: build
          projects: App.sln
          arguments: '--no-restore --configuration release --version-suffix $(Build.BuildNumber)'

到目前为止,我已经尝试使用不同的 SDK 版本(3.1.406 和 3.1.409)并且我已经检查了警告和信息消息中的所有建议。还值得注意的是,该项目确实在本地使用 dotnet build 成功构建。解决方案目标中的所有项目 netcoreapp3.1

我认为问题是由于我正在使用 vmImage: 'windows-latest' 时的图像更新引起的,但是我看不到任何问题,而且我 运行 没有想法。


更新

Microsoft.FSharp.Targets(279,9) 中的文件和行号似乎表明这是调用 FSharp 编译器的问题。

这也与 vs 开发者社区中的问题一致 unable to build fsharp project using dotnet build。该线程包含以下相关部分,但是它没有说明 $(PathToFsc) 为何或如何为空。

Existing error message accurately conveys the issue; it’s not F#-specific. Something in the .props/.targets files evaluates to dotnet $(PathToFsc) some/file.rsp and the variable $(PathToFsc) (or whatever is in your build scripts) is evaluating to an empty string. The final command that’s executed is then dotnet some/file.rsp and the normal dotnet behavior is to look for dotnet-<argument> as an executable.

问题实际上是由于 FscToolPath 评估为空字符串。

Existing error message accurately conveys the issue; it’s not F#-specific. Something in the .props/.targets files evaluates to dotnet $(PathToFsc) some/file.rsp and the variable $(PathToFsc) (or whatever is in your build scripts) is evaluating to an empty string. The final command that’s executed is then dotnet some/file.rsp and the normal dotnet behavior is to look for dotnet- as an executable.

第二个因素是 FSC 的位置确实由于 VM 映像上 Visual Studio 的更新而发生了变化。

Not an answer, but I wonder if it's related to this: whosebug.com/questions/67800998/… - it seems things may have moved between VS 16.9 and 16.10.

最后,它影响我的原因是因为我手动设置了 FscCompilerPath,因为 TypeProvider 由于对 System.Data.SqlClient.

的依赖而不支持 dotnet 核心管道
<DisableAutoSetFscCompilerPath>true</DisableAutoSetFscCompilerPath>
<PropertyGroup Condition="'$(IsWindows)' == 'true' AND Exists('C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\CommonExtensions\Microsoft\FSharp\fsc.exe')">
  <FscToolPath>C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\CommonExtensions\Microsoft\FSharp</FscToolPath>
  <FscToolExe>fsc.exe</FscToolExe>
</PropertyGroup>

因此将 FscToolPath 更新为 C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools 解决了问题。