Codementor Events

SourceLink

Published Feb 19, 2020
SourceLink

This is final post about additions to your Directory.Build.props file for now. Check out part 1 here for more information about this magic file.

In this part, I'm going to discuss integrating SourceLink into your project. SourceLink solves the problem of debugging into NuGet packages by adding 3 key bits of metadata to a package.

  • The type of your source-code repository (Git, TFS, etc.)
  • The URL to the repository
  • The id of the commit when the package was built.

Your IDE can then automatically download the code for the assemblies in the NuGet package, and step-into it when debugging - very cool!

For example, a typical value for a nuget package called Contoso.Utilities would be

<repository 
    type="git" 
    url="https://github.com/contoso/Contoso.Utilities.git" 
    commit="39c5274e7c36c5ecc37a0ca1389a589441262c20" />

The great part is that you can automatically turn on SourceLink for every project in your solution by simply adding a few properties into your Directory.Build.props file.

<PropertyGroup Label="Enable SourceLink Support">
  <PublishRepositoryUrl>true</PublishRepositoryUrl>
  <DebugType>portable</DebugType>  
  <AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<!-- Choose the right package for your source-control provider. I'm using Github -->
<ItemGroup Label="Enable SourceLink GitHub Support">
  <!-- Enable github sourcelink integration. 
       See https://github.com/dotnet/sourcelink.
  -->
    <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-*" PrivateAssets="All"/>
</ItemGroup>

One thing to note is that I am including the PDB files inside the nuget package. This makes the package a little larger, but is the lowest-friction way to make this work.

Another approach is to create a .snupkg - which is a NuGet package that contains symbols. This can be served up automatically by most major nuget package providers (including NuGet.org). See here for more details.

Discover and read more posts from Rowland Banks
get started