MacOS下dotnet build找不到Xamarin targets及rzc.dll缺失问题求助
问题解决:MacOS下dotnet build及VS构建报错处理
一、终端执行dotnet build提示找不到Xamarin targets文件
报错信息
/path/to/repo/Toolkit/My.Company.Toolkit.Platform.Android/My.Company.Toolkit.Platform.Android.csproj(91,3): error MSB4019: The imported project "/usr/local/share/dotnet/sdk/7.0.102/Xamarin/Android/Xamarin.Android.CSharp.targets" was not found. Confirm that the expression in the Import declaration "/usr/local/share/dotnet/sdk/7.0.102//Xamarin/Android/Xamarin.Android.CSharp.targets" is correct, and that the file exists on disk. /path/to/repo/Toolkit/My.Company.Toolkit.Platform.iOS/My.Company.Toolkit.Platform.iOS.csproj(195,3): error MSB4019: The imported project "/usr/local/share/dotnet/sdk/7.0.102/Xamarin/iOS/Xamarin.iOS.CSharp.targets" was not found. Confirm that the expression in the Import declaration "/usr/local/share/dotnet/sdk/7.0.102//Xamarin/iOS/Xamarin.iOS.CSharp.targets" is correct, and that the file exists on disk.
原因
系统级dotnet SDK(路径/usr/local/share/dotnet/sdk)不包含Xamarin的MSBuild targets文件,Xamarin组件的targets实际存放在Visual Studio for Mac的安装目录中。终端直接调用dotnet build时,无法正确解析$(MSBuildExtensionsPath)变量指向VS的Xamarin路径。
解决办法
- 使用Visual Studio自带的MSBuild构建
直接调用VS内置的MSBuild来执行构建,确保路径指向正确:
dotnet exec "/Applications/Visual Studio.app/Contents/MacOS/MSBuild/bin/MSBuild.dll" /path/to/your/solution.sln
- 手动设置MSBuildExtensionsPath环境变量
在终端临时设置环境变量,让dotnet build能找到Xamarin的targets文件:
export MSBuildExtensionsPath="/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild" dotnet build /path/to/your/solution.sln
二、重装VS后,VS内构建提示rzc.dll不存在
报错信息
Target RazorGenerateComponentDeclaration: /usr/local/share/dotnet/dotnet exec "/Users/redacted/Library/Caches/VisualStudio/17.0/MSBuild/36265_3/Sdks/Microsoft.NET.Sdk.Razor/build/netstandard2.0/../../tools/netcoreapp3.0/rzc.dll" generate ... The application to execute does not exist: '/Users/redacted/Library/Caches/VisualStudio/17.0/MSBuild/36265_3/Sdks/Microsoft.NET.Sdk.Razor/build/netstandard2.0/../../tools/netcoreapp3.0/rzc.dll' /Users/redacted/Library/Caches/VisualStudio/17.0/MSBuild/36265_3/Sdks/Microsoft.NET.Sdk.Razor/build/netstandard2.0/Microsoft.NET.Sdk.Razor.Component.targets(106,5): error : rzc generate exited with code 129.
原因
VS缓存的Razor SDK文件损坏或缺失,或者项目依赖的.NET Core 3.0 runtime未安装(rzc.dll需要该runtime环境)。
解决办法
- 清理VS的MSBuild缓存
删除VS的MSBuild缓存目录,重启VS后会自动重新下载正确的SDK文件:
rm -rf /Users/$(whoami)/Library/Caches/VisualStudio/17.0/MSBuild
重启Visual Studio for Mac后再尝试构建。
安装.NET Core 3.0 Runtime
rzc.dll依赖.NET Core 3.0 runtime,虽然你的dotnet --info未显示该版本,但需要手动安装对应架构(arm64)的.NET Core 3.0 runtime。安装完成后重启VS再构建。更新Razor SDK版本
检查项目中Microsoft.NET.Sdk.Razor的版本,尝试升级到兼容项目.NET版本的包(比如项目用netstandard2.1,可升级到3.1.x及以上版本),避免依赖过旧的netcoreapp3.0版本的rzc工具。
内容的提问来源于stack exchange,提问作者grandouassou
相关产品推荐
相关产品推荐

