Debugging libraries from NuGet
As I was troubleshooting my issues with Elastic logger configuration, I couldn't get the debugging in Visual Studio 2022 to work for the Elastic.Extensions.Logging
NuGet package, although it appeared to have SourceLink correctly set up, and I could navigate its source code without any issues. When I tried it in JetBrains Rider, it just worked. I decided to get to the bottom of this.
I started by checking that I have SourceLink debugging configured correctly in Visual Studio 2022:
- In Debugging > General options, I had:
- Enable Source Link support checked, and
- Enable Just My Code unchecked.
- In Debugging > Symbols options, I had NuGet.org Symbol Server checked under the Symbol file (.pdb) search locations.
But still, it didn't work. It took me a while to find the reason: the .pdb
files for the assembly I was trying to debug were included in the same NuGet package. And this requires explicit opt-in or the symbols won't get loaded. Since .NET 7, the CopyDebugSymbolFilesFromPackages
property must be enabled in the project file:
<PropertyGroup>
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
</PropertyGroup>
As soon as I did that, I could successfully debug the NuGet package.
I tried it once again in JetBrains Rider, and I could confirm that it ignores this property: the debugging worked if the property was enabled, disabled or not present in the project file at all. I only had to have Use sources from symbol file when available checked in the Tools > External Symbols settings.
Visual Studio Code behaves like Visual Studio 2022. For SourceLink-based debugging, it only requires the Csharp > Debug: Just My Code setting disabled in UI:
Or directly in the
settings.json
file:
{
"csharp.debug.justMyCode": false
}
And for the .pdb
files included in the main NuGet file, it also requires the CopyDebugSymbolFilesFromPackages
property to be set in the project file:
<PropertyGroup>
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
</PropertyGroup>
While at it, I also tested debugging of NuGet packages without SourceLink support, i.e., distributed without .pdb
files linking to source code:
- In Visual Studio 2022, I had to additionally check Automatically decompile to source when needed (Managed only) in Debugging > General options.
- In JetBrains Rider, I had to additionally check Decompile methods in Tools > External Symbols settings.
- In Visual Studio Code, I couldn't get it to work with C# or C# Dev Kit extension.
Although the CopyDebugSymbolFilesFromPackages
property technically is documented, I feel like it should be mentioned more often in the documentation related to SourceLink. And although it's only needed for debugging assemblies, which have the .pdb
files in the same NuGet package, I think it's a good idea to add it to your project file as soon as you start debugging assemblies from NuGet packages. Otherwise, you might suddenly start wondering why debugging doesn't work for one assembly, but it does for all the others.