Get source files while debugging Nugets

Debugging your own private Nugets can become a nightmare if you don't configure everything right. No source code, no lines on errors... Let's do something about it !

The goal

If you use your own private Nuget feeds to re-use your code between multiple projects, you maybe have hard times debugging it. When you publish your Nuget, it becomes a black box and it can get messy to debug it. If a project that use it encounters a bad behaviour, it’s impossible by default to have a proper debugging session, because you’re projects are not in the same solution. Visual Studio doesn’t open your source file and all the comments that comes with it that could help to identify what’s going wrong.

What we want and will achieve here is to get a beautiful stack trace with the original source code shown as if it was in your local machine.

Divide By Zero Exception Screenshot

Our environment

First, let’s talk about the environment we have at La Moutarde. You’ll maybe have to adapt this tutorial depending yours.

  • Our Git repositories are stored in VSTS
  • Our private Nuget feeds are stored in VSTS
  • We have continuous integration pluged on our master branch (so VSTS builds what is on the master branch)

VSTS Configuration

First of all, we will configure the symbols publishing on the build process.

If you use the standard build preset for .NET application, you already have a task that index the sources, but without publishing it. Go in (or add) the task of type “Index Sources & Publish Symbols” and check “Index sources”, “Publish symbols” and choose a server to publish. You can use Azure Artifacts or a file share, but using artifacts makes everything easier (you won’t need anything else to configure and Visual Studio will find it automatically).

Symbols Publishing VSTS Screenshot

Visual Studio configuration

Now, it’s time to configure Visual Studio. Open Tools > Options > Debugging > Symbols and add a server. If you use VSTS like use, you just have to click on the “New Azure DevOps Symbol Server Location” button. It will open a prompt so you can choose your VSTS server.

Add Azure DevOps Server Screenshot

Now, Visual Studio will be able to fetch your symbols files and download it when you’ll start a debug session.

If you launch a debug session now, you’ll have line errors on debugging, but not your source code. It’s good but we can do better. Let’s introduce Source Link (GitHub).

Source Link is a package to add to your project that will add everything needed to your Nuget at build time. Everything is explained in the GitHub, but in our case, we need to add these lines in the .csproj file of our Nuget :

<PropertyGroup>
    <!-- ... -->
    <PublishRepositoryUrl>true</PublishRepositoryUrl>
    <EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>

<ItemGroup>
    <!-- ... -->
    <PackageReference 
        Include="Microsoft.SourceLink.GitHub" 
        Version="1.0.0-beta2-18618-05" 
        PrivateAssets="All"
        />
</ItemGroup>

Back to Visual Studio

And… that’s all. Now, Visual Studio will ask if you want to download the source on an error, everytime it’s possible. Enjoy your new debugging sessions !

author picture
Written by Francois Bertrand
co-founder / developer at La Moutarde
C# and Visual Studio lover. Grew up with Sonic and Streets Of Rage. Trying to adulting right now.