Most of the tutorials on how to set up SourceLink seem to omit two key things.
Firstly:
- As well as unchecking "Enable Just My Code" (non-default) and making sure "Enable Source Link support" is checked (default) under Options/Debugging/General,
- If you want to work with SourceLink for public packages (say Newtonsoft.Json) you also have to check the listed symbol servers in VS Options/Debugging/Symbols.
Secondly:
- If you want to publish symbols for your own package onto a public feed (e.g. nuget.org) then AFAIK the
.snupkg
format works fine
- On a private NuGet feed (whether using a local file system NuGet feed, or the IIS
NuGet.Server
package) there is no symbol server, .snupkg
is not supported, and this (which is widely listed as what to do instead) does not work (it includes the .pdb file in the NuGet package, but from my tests the consuming VS never uses them):
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
- Using this instead, in the same place, does work (the pdb is embedded inside the dll itself, and the consuming VS sees it just fine):
<PropertyGroup>
<DebugType>Embedded</DebugType>
</PropertyGroup>
You should definitely check out the VS Modules window found under Debug/Windows/Modules (shortcut CTRL+ALT+U) (only available while code is running), which shows which modules have loaded symbols and helps you figure out which haven't, and why not, if they haven't.
EDIT
It turns out that:
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
is working. It produces a NuGet file which is fine, or should be... but there is currently a bug in .NET SDK which causes it not to load .pdb files stored in NuGet files in this way, although it should.
There is another fix. In the consuming project, include:
<PackageReference Include="SourceLink.Copy.PdbFiles" Version="2.8.3" PrivateAssets="All" />
after adding that, everything should work again as per the demos.
Alternatively, the fix which that package applies is:
<Project>
<Target Name="_ResolveCopyLocalNuGetPackagePdbs" AfterTargets="ResolveReferences"
Condition="$(CopyLocalLockFileAssemblies) == true">
<ItemGroup>
<ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->'%(RootDir)%(Directory)%(Filename).pdb')"
Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)' != '' and Exists('%(RootDir)%(Directory)%(Filename).pdb')" />
</ItemGroup>
</Target>
</Project>
and (from here):
A good place to put it is in Directory.Build.targets so that your project files are not cluttered and so that you can apply it to multiple projects in a solution.
https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build