在 Visual Studio 上为 Mac 调试 nuget 包不工作

Debug nuget packages on Visual Studio for Mac not working

我的 gitlab 项目中托管了一些 nuget 包。我需要调试这个包,但无法在 Visual Studio 上为 Mac 执行此操作。 这是我的 csproj 文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1</TargetFrameworks>
    <!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
    <PublishRepositoryUrl>true</PublishRepositoryUrl>
    <!-- Optional: Embed source files that are not tracked by the source control manager in the PDB -->
    <EmbedUntrackedSources>true</EmbedUntrackedSources>
    <!-- Optional: Build symbol package (.snupkg) to distribute the PDB containing Source Link -->
    <IncludeSymbols>true</IncludeSymbols>
    <!-- Recommended: Embed symbols containing Source Link in the main file (exe/dll) -->
    <DebugType>embedded</DebugType>
    <SymbolPackageFormat>snupkg</SymbolPackageFormat>
    <EmbedAllSources>true</EmbedAllSources>
    <PackageVersion>1.0.18.0</PackageVersion>
    <AssemblyVersion>1.0.18.0</AssemblyVersion>
    <FileVersion>1.0.18.0</FileVersion>
    <InformationalVersion>1.0.18.0</InformationalVersion>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
  </PropertyGroup>

  <ItemGroup Condition="$(TargetFramework.StartsWith('netstandard2.0')) Or $(TargetFramework.StartsWith('netstandard2.1'))">
    <PackageReference Include="nlog" Version="4.6.2" />
    <PackageReference Include="System.Threading" Version="4.3.0" />
    <PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.0.0" PrivateAssets="All"/>
    <Compile Include="ISynchronizationContext.cs" />
    <Compile Include="TaskExtensions.cs" />
    <Compile Include="AsyncAwaiter.cs" />
  </ItemGroup> 
  <ItemGroup Condition="$(TargetFramework.StartsWith('netcoreapp3.1'))">
    <PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.0.0" PrivateAssets="All"/>
    <PackageReference Include="nlog" Version="4.6.2" />
    <PackageReference Include="System.Threading" Version="4.3.0" />
    <Compile Include="*.cs" />
  </ItemGroup>
  <PropertyGroup>
    <AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
  </PropertyGroup>
</Project>

这是我的 .gitlab 部署部分-ci.yml:

deploy:
  stage: deploy
  script:
    - dotnet pack -c Release
    - dotnet nuget add source "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/nuget/index.json" --name $CI_PROJECT_TITLE --username $CI_REGISTRY_USER --password $CI_REGISTRY_PASSWORD --store-password-in-clear-text
    - dotnet nuget push "Source/bin/Release/*.nupkg" --source $CI_PROJECT_TITLE

调试 nuget 包在 Visual Studio 2019 中工作,但在 Visual Studio 中不工作 Mac。 在这个 article 我读到:

In Visual Studio for Mac, support for symbol servers doesn’t exist yet, so Source Link only works with NuGet packages that contain their own debug symbols.

我需要做什么才能在 Visual Studio 中为 Mac 调试我的 nuget 包?

根据 this comment by TysonMN,.csproj 文件中的以下内容会将 PDB 符号和关联的源代码嵌入到 .nuget 中,以便 VS 可以找到它们:

<PropertyGroup>
  <DebugSymbols>True</DebugSymbols>
  <DebugType>Embedded</DebugType>
  <EmbedAllSources>True</EmbedAllSources>
</PropertyGroup>

未在 Mac 上测试 - 请在评论中说明它是否有效。

您可以尝试使用 Debugging NuGet packages with Source Link,根据 xamarin 文档:

Source Link technology enables source code debugging of .NET assemblies from NuGet that ship .PDBs with links to source files. Source Link executes when developers create their NuGet package and embed source control metadata inside assemblies and the package. When Source Link is enabled in Visual Studio for Mac, the IDE will detect if source files are available for installed packages. Visual Studio for Mac will then offer to download them, which will allow you to step through the package code. Source Link also works with Mono Base Class Library code for Xamarin projects, allowing you to step into .NET Framework code as well. Source Link provides source control metadata to create a great debugging experience.

这是你的做法,你可以查看这个文档https://docs.microsoft.com/en-us/visualstudio/mac/source-link?view=vsmac-2019