How to debug code in NuGet package created by me
Solution 1
To debug any dll you need the symbol file of it (.pdb). If you build your project in the debug
configuration you will see that those files are generated and put in the build output folder.
Visual studio loads those symbol files from different places as described here. The easiest way to debug your nuget packages is to put the .pdb files of the packages in the build output folder of the project you want to debug.
If the code you are trying to debug is classified as non-user code you need to uncheck Just My Code
in the debugging options.
The following quote from the Microsoft - Visual Studio Docs shows what counts as user and what as non-user code.
User and non-user code
To distinguish user code from non-user code, Just My Code looks at symbol (.pdb) files and program optimizations. The debugger considers code to be non-user code when the binary is optimized or when the .pdb file is not available.
Three attributes also affect what the debugger considers to be My Code:
- DebuggerNonUserCodeAttribute tells the debugger that the code it is applied to is not My Code.
- DebuggerHiddenAttribute hides the code from the debugger, even if Just My Code is turned off.
- DebuggerStepThroughAttribute tells the debugger to step through the code it is applied to, rather than step into the code.
All other code is considered to be user code.
A more detailed answer can be found on my blog.
Solution 2
For Visual Studio 2017 and your nuget package source code hosted on GitHub or BitBucket:
-
Enable full debug information in *.csproj file:
<PropertyGroup Condition="'$(Configuration)'=='Debug'"> <DebugType>full</DebugType> <DebugSymbols>true</DebugSymbols> </PropertyGroup>
or right-click project properties, build, advanced, output debugging information - set to full.
-
To enable automatic source download and stepping for your nuget package dll, add nuget package
SourceLink.Create.CommandLine
to your project, or add it manually into *.csproj file:<ItemGroup> <PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.2" PrivateAssets="All" /> </ItemGroup>
More info here.
-
In tools - options - debugging, disable "Enable Just My Code", and enable "Suppress JIT optimization on module load (Managed Only)".
After this, you should be able to step inside methods from your nuget package dll.
Solution 3
I got this working by building the project the nuget package originated from in debug mode, then just copying the pdb and dll from the debug directory to the location of the nuget dll within the project I wanted to debug it in.
e.g copy from
ExternalNugetPackage\bin\Debug\
to
ProjectDirectory\Packages\ExternalNugetPackage.1.0.0\lib\net4.5
Solution 4
How to debug code in a nuget package created by me
Just as NtFreX answered, "To debug any dll you need the symbol file of it (.pdb). ". So you can create symbol packages which allow consumers to step into your package code in the Visual Studio debugger.
The way we do it (and works):
- Create "*.symbols.nupkg".
- Deploy symbol package to SymbolSource server.
- Configure IDE, Package consumers can add https://nuget.smbsrc.net/ to your symbol sources in Visual Studio.
- Add required Library to project using NuGet (from our SymbolSource server).
- Debug.
For the detail info, you can refer to Creating symbol packages.
If these packages are not suitable for publishing on NuGet Gallery/SymbolSource, you can put the *.nupkg and *.symbols.nupkg files on a local disk.
Note: Add the source code to the Debug Source Files for the solution that references the package(Right click on Solution, select Properties...Common Properties...Debug Source Files, and add the root source directory for the relevant binary reference)
Solution 5
There is a much simpler solution:
Simply embed the debug symbols in the dll. Update your nupkg, et voila!
Yatiac
Updated on November 22, 2021Comments
-
Yatiac over 2 years
I have a NuGet package I created and installed in another solution but now I need to debug the code of the package when called from my new solution.
I tried referencing the solution of the package but it's not working.
I am using Visual Studio 2013.
-
Piotr Kula over 6 yearsFrom every other crazy solution I have tried and wasted my time with this is the simplest. I just extract my
nuget.symbols.nupkg
and dump the pdb in the bin directory next to the downloaded dll , un-checkEnable just my code
and I can step into my NuGet package. I did not even need to add the source files into the solution "Debug Source Files" panel. I don't understand why everybody suggested over the top, extremely complicated solutions? +1 for you mate! -
Piotr Kula over 6 yearsThis just does not work- I have no idea why.. but I have spent too much time trying to figure it out when it should be simple for us. Putting the pdb file in the bin directory of the built project.. worked straight away for me (just had to un-check Enable My Code"
-
NtFreX over 6 years@ppumkin Thanks for the information about
Enable just my code
. I've updated my answer to include this information. -
Piotr Kula over 6 yearsI have tried this again on another project and it says the source code is missing.. so last time I included the source in the properties and it worked but this new project I didnt so I cannot step into the code - But you will also get "source code missing" when trying to step in.. you can then click "navigate to source" and select the folder with all the source files (in my case unpacked from the nuget package) boom - stepped into code. Another way is to just open the file in Visual Studio. File -> Open (the exact source file)
-
Craig almost 6 yearsIf I switch Enable Just My Code off, I end up having to step through a bunch of .NET framework stuff before I get anywhere near my code. It's not ideal, but disabling Optimize code when building the NuGet package is the only reasonable option for me. Just have to remember to switch it back for deployment.
-
Chris Rice over 5 yearsThis is a great answer! SourceLink has been taken over by microsoft now so heres the new repo github.com/dotnet/sourcelink. The only bummer is this requires packagereference and .NET Core.
-
Ehtesh Choudhury over 4 yearsWhat does
Suppress JIT optimization on module load (Managed Only)
do? -
xhafan over 4 yearsWhen the package is JIT optimized, the code being executed is slightly different (optimized) than the source code (e.g. some small methods might be inlined, etc), and stepping in the debugger would not reflect lines of code exactly.
-
Alex Klaus over 3 yearsStep 1 is not required since Visual Studio 2019.
-
Martijn Pieters over 3 yearsCopied without attribution from docs.microsoft.com/sr-latn-rs/visualstudio/debugger/…
-
Marlonchosky over 3 yearsGreat answer!! It works for my debugging with Castle.Core. +1
-
mattsmith5 almost 3 yearsis this supposed to work with Net Core 3.1 ?
-
Ankush Jain almost 3 yearsAwesome...Saved my day.
-
Frank over 2 yearsThe above is VS 2022 , in case the UI looks a little unfamiliar.