You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

macOS下dotnet restore生成Windows风格路径导致MSBuild无法定位NuGet DLL的问题求助

macOS下dotnet restore生成Windows风格路径导致MSBuild无法定位NuGet DLL的问题求助

大家好,我在macOS上用.NET 8.0 CLI和Rider开发一个.NET 8.0解决方案,项目通过<PackageReference>引入了LiteDB、TiktokenSharp这类依赖,最近遇到了一个非常诡异的路径问题,折腾了好几天都没搞懂根因,来这里求助!

问题现象

执行dotnet restore后,NuGet包明明已经成功下载,但执行编译时却报一堆找不到类型/程序集的错误:

Error CS0246: The type or namespace name 'LiteDB' could not be found (are you missing a using directive or an assembly reference?)
Error CS0246: The type or namespace name 'TiktokenSharp' could not be found (are you missing a using directive or an assembly reference?)
Warning MSB3106: Assembly strong name "...LiteDB.dll" is either a path which could not be found or it is a full assembly name which is badly formed.

我排查后发现,restore把DLL存在了带Windows反斜杠\的路径里,比如:
packages/litedb/5.0.16/lib\netstandard2.0\LiteDB.dll
macOS的路径本该用正斜杠/,结果路径里混了反斜杠,导致MSBuild编译时根本找不到这些实际存在的DLL。

我尝试过的所有解决方法

为了修复这个问题,我几乎把能试的清理/还原操作都做了一遍,但都没用:

  1. 校验包引用:确认LiteDB和TiktokenSharp的包引用在项目文件里正确,且能正常解析
  2. 彻底清理缓存与项目文件
    • 执行dotnet clean
    • 执行dotnet nuget locals all --clear
    • 自定义脚本清理了这些内容:
      • NuGet HTTP缓存
      • 本地包文件夹
      • 临时缓存
      • 插件缓存
      • 所有项目的bin/obj/文件夹
  3. 强制还原:用强制参数执行restore成功,但编译还是报同样的错误
  4. 检查包文件夹结构:确实看到DLL的路径里是lib\netstandard2.0\LiteDB.dll这种带反斜杠的结构,在macOS上完全无效,直接破坏了MSBuild的路径解析

进一步排查的细节

后来我按照建议执行了dotnet build -bl,查看binlog后发现:
MSBuild尝试查找的路径是正斜杠的/Users/.../packages/litedb/5.0.16/lib/netstandard1.3/LiteDB.dll,但实际文件所在的路径是带反斜杠的/Users/.../packages/litedb/5.0.16/lib\netstandard1.3\LiteDB.dll,两者完全不匹配,这就是找不到文件的根本原因。

偶然的解决方法与我的疑问

我抱着试试看的心态,把LiteDB从5.0.16升级到5.0.21,TiktokenSharp从1.0.6升级到1.0.8,居然就编译成功了?但我完全搞不懂为什么升级包就能解决这个问题,也不知道根因到底是什么。

现在我最想搞清楚的是:

  1. 为什么在macOS上,dotnet restore会用Windows风格的反斜杠路径来保存NuGet包文件?
  2. 有没有办法从根源上阻止这种情况,或者强制路径标准化,让MSBuild能正确解析DLL路径?

火山引擎 最新活动