在使用 dotnet 构建的时候提示 error : SourceRoot items must include at least one top-level (not nested) item when DeterministicSourcePaths is true 构建失败

我在库里使用了 SourceLink 这个库,用来关联代码,详细请看 dotnet 使用 SourceLink 将 NuGet 链接源代码到 GitHub 等仓库

此时在构建时候有如下提示

C:\Users\runneradmin\AppData\Local\Microsoft\dotnet\sdk\3.1.300\Roslyn\Microsoft.Managed.Core.targets(104,5): error : SourceRoot items must include at least one top-level (not nested) item when DeterministicSourcePaths is true [D:\a\dotnetCampus.Ipc\dotnetCampus.Ipc\src\dotnetCampus.Ipc.Abstractions\dotnetCampus.Ipc.Abstractions.csproj]

解决方法就是在 csproj 等定义 SourceRoot 这个属性的值,这个值需要表示当前的源代码的最顶层路径

<ItemGroup>
  <SourceRoot Include="../"/>
</ItemGroup>

下面是整个 csproj 的定义代码,方便大家了解上面代码写在哪

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <SourceRoot Include="../"/>
    </ItemGroup>

上面代码使用的是相对的路径,而一个比较好的方法是写在 Directory.Build.props 文件里,关于 Directory.Build.props 文件,请看 Roslyn 使用 Directory.Build.props 文件定义编译

在 Directory.Build.props 文件添加如下代码就可以

<ItemGroup>
  <SourceRoot Include="$(MSBuildThisFileDirectory)/"/>
</ItemGroup>

上面代码的 $(MSBuildThisFileDirectory) 表示的是当前文件所在的文件夹,这是构建时的常量,更多常量请看 项目文件中的已知属性(知道了这些,就不会随便在 csproj 中写死常量啦) - walterlv

本文的解决方法是在 DeterministicSourcePaths can break building if source control information not available · Issue #37379 · dotnet/roslyn 找到的


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-%E6%9E%84%E5%BB%BA-SourceRoot-items-must-include-at-least-one-top-level-item-when-DeterministicSourcePaths-is-true-%E5%A4%B1%E8%B4%A5.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利