dotnet 运行 x.csproj “... libhostfxr.dylib 找不到。”

dotnet run x.csproj "... libhostfxr.dylib could not be found."

运行 Mac 安装了 .NET 6.0 的 .NET Core 项目导致奇怪的错误。

 lukepuplett@Lukes-Air > ~/Documents/Git/Repo/src/Corp.App.Mvc > > master ± > dotnet run Corp.App.Mvc.csproj 
A fatal error occurred. The required library libhostfxr.dylib could not be found.
If this is a self-contained application, that library should exist in [/Users/lukepuplett/Documents/Git/Repo/src/Corp.App.Mvc/bin/Debug/net5.0/].
If this is a framework-dependent application, install the runtime in the global location [/usr/local/share/dotnet/x64] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [/etc/dotnet/install_location].

The .NET runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=osx.11.1-x64&apphost_version=5.0.12
 

请注意,项目构建良好。

已安装 macOS Monterey、Apple Silicon M1 和 .NET 6.0。

当项目以较旧的 .NET Core 版本为目标时,可能会发生这种情况。该错误令人困惑,因为项目可能构建正常,并且您希望得到更准确的消息。

直接执行编译后的二进制文件可以看到更好的消息,见下文。

 lukepuplett@Lukes-Air > ~/Documents/Git/Repo/src/Corp.App.Mvc/bin/Debug/net5.0 > > master ± > dotnet Corp.App.Mvc.dll
It was not possible to find any compatible framework version
The framework 'Microsoft.AspNetCore.App', version '5.0.0' (arm64) was not found.
  - The following frameworks were found:
      6.0.0 at [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=5.0.0&arch=arm64&rid=osx.11.1-arm64

例如,当您从面向 .NET Core 5 的 Git 存储库中提取代码时,可能会出现此问题。