You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core 8 Web API升级后MSBuild SourceRoot参数多值错误求助

升级到.NET 8后SourceLink构建错误的解决方法

问题描述

将Web API从netcoreapp7.0升级到netcoreapp8.0后,出现构建错误:

"C:\Users\Dewey.nuget\packages;C:\Users\dewey.nuget\packages" 是"Microsoft.SourceLink.GitHub.GetSourceLinkUrl"任务的"SourceRoot"参数的无效值,无法将多个项传入"Microsoft.Build.Framework.ITaskItem"类型的参数。

错误发生在C:\Program Files\dotnet\sdk\8.0.100\Sdks\Microsoft.SourceLink.GitHub\build\Microsoft.SourceLink.GitHub.targets文件中,文件内容如下:

<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the License.txt file in the project root for more information. -->
<Project>
  <PropertyGroup>
    <_SourceLinkGitHubAssemblyFile Condition="'$(MSBuildRuntimeType)' != 'Core'">$(MSBuildThisFileDirectory)..\tools\net472\Microsoft.SourceLink.GitHub.dll</_SourceLinkGitHubAssemblyFile>
    <_SourceLinkGitHubAssemblyFile Condition="'$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)..\tools\core\Microsoft.SourceLink.GitHub.dll</_SourceLinkGitHubAssemblyFile>
  </PropertyGroup>

  <UsingTask TaskName="Microsoft.SourceLink.GitHub.GetSourceLinkUrl" AssemblyFile="$(_SourceLinkGitHubAssemblyFile)"/>
  <UsingTask TaskName="Microsoft.SourceLink.GitHub.TranslateRepositoryUrls" AssemblyFile="$(_SourceLinkGitHubAssemblyFile)"/>

  <PropertyGroup>
    <SourceLinkUrlInitializerTargets>$(SourceLinkUrlInitializerTargets);_InitializeGitHubSourceLinkUrl</SourceLinkUrlInitializerTargets>
    <SourceControlManagerUrlTranslationTargets>$(SourceControlManagerUrlTranslationTargets);TranslateGitHubUrlsInSourceControlInformation</SourceControlManagerUrlTranslationTargets>
  </PropertyGroup>

  <Target Name="_InitializeGitHubSourceLinkUrl" Outputs="%(SourceRoot.Identity)">
    <!--
      The task calculates SourceLink URL for a given SourceRoot.

      If the SourceRoot is associated with a git repository with a recognized domain the <see cref="SourceLinkUrl"/>
      output property is set to the content URL corresponding to the domain, otherwise it is set to string "N/A".

      Recognized domains are specified via Hosts (initialized from SourceLinkGitHubHost item group).
      In addition, if SourceLinkHasSingleProvider is true an implicit host is parsed from RepositoryUrl.

      Example of SourceLinkGitHubHost items:

      <ItemGroup>
        <SourceLinkGitHubHost Include="github.com" ContentUrl="https://raw.githubusercontent.com"/>
        <SourceLinkGitHubHost Include="mygithub1.com" />           ContentUrl defaults to https://mygithub1.com/raw
        <SourceLinkGitHubHost Include="mygithub2.com:8080" />      ContentUrl defaults to https://mygithub2.com:8080/raw
      </ItemGroup>

      ContentUrl is optional. If not specified it defaults to "https://{domain}/raw".
    -->
    <Microsoft.SourceLink.GitHub.GetSourceLinkUrl RepositoryUrl="$(PrivateRepositoryUrl)" SourceRoot="@(SourceRoot)" Hosts="@(SourceLinkGitHubHost)" IsSingleProvider="$(SourceLinkHasSingleProvider)">
      <Output TaskParameter="SourceLinkUrl" PropertyName="_SourceLinkUrlToUpdate"/>
    </Microsoft.SourceLink.GitHub.GetSourceLinkUrl>

    <ItemGroup>
      <!-- Only update the SourceLinkUrl metadata if the SourceRoot belongs to this source control -->
      <SourceRoot Update="%(Identity)" SourceLinkUrl="$(_SourceLinkUrlToUpdate)" Condition="'$(_SourceLinkUrlToUpdate)' != 'N/A'"/>
    </ItemGroup>
  </Target>

  <!-- 
    We need to translate ssh URLs to https.
  -->
  <Target Name="TranslateGitHubUrlsInSourceControlInformation">

    <ItemGroup>
      <_TranslatedSourceRoot Remove="@(_TranslatedSourceRoot)"/>
    </ItemGroup>

    <Microsoft.SourceLink.GitHub.TranslateRepositoryUrls RepositoryUrl="$(ScmRepositoryUrl)" SourceRoots="@(SourceRoot)" Hosts="@(SourceLinkGitHubHost)" IsSingleProvider="$(SourceLinkHasSingleProvider)">
      <Output TaskParameter="TranslatedRepositoryUrl" PropertyName="ScmRepositoryUrl"/>
      <Output TaskParameter="TranslatedSourceRoots" ItemName="_TranslatedSourceRoot"/>
    </Microsoft.SourceLink.GitHub.TranslateRepositoryUrls>

    <ItemGroup>
      <SourceRoot Remove="@(SourceRoot)"/>
      <SourceRoot Include="@(_TranslatedSourceRoot)"/>
    </ItemGroup>
  
</Target>

</Project>

错误原因

错误核心是SourceRoot项组中混入了用分号分隔的多个NuGet包路径(因用户名大小写不一致导致重复源),而.NET 8的SourceLink任务要求SourceRoot参数只能接收单个项,无法处理合并后的字符串值。

解决方法

1. 清理全局NuGet配置中的重复源

  • 打开C:\Users\Dewey\AppData\Roaming\NuGet\NuGet.Config文件
  • 删除路径大小写不一致的重复NuGet源(比如同时存在的C:\Users\Dewey.nuget\packages和C:\Users\dewey.nuget\packages),仅保留一个符合系统用户名大小写的路径。

2. 在项目中过滤NuGet包路径的SourceRoot

在项目的.csproj文件中添加以下配置,排除NuGet包目录作为SourceRoot:

<ItemGroup>
  <SourceRoot Remove="@(SourceRoot)" Condition="%(Identity) Contains '.nuget\packages'" />
</ItemGroup>

3. 更新SourceLink包版本

确保项目引用的Microsoft.SourceLink.GitHub包与.NET 8 SDK兼容,建议升级到对应版本:

<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />

4. 临时禁用SourceLink(若无需该功能)

在项目的.csproj文件中添加配置关闭SourceLink:

<PropertyGroup>
  <EnableSourceLink>false</EnableSourceLink>
</PropertyGroup>

内容的提问来源于stack exchange,提问作者Dewey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 12:30:58