120

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Yatiac
  • 1,820
  • 3
  • 15
  • 25
  • related: [How to debug into my nuget package deployed from TeamCity?](https://stackoverflow.com/q/21857780/33499) – wimh Nov 09 '17 at 13:27

8 Answers8

76

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.

enter image description here

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.

NtFreX
  • 10,379
  • 2
  • 43
  • 63
  • 10
    From 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-check `Enable 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 Nov 23 '17 at 15:47
  • @ppumkin Thanks for the information about `Enable just my code`. I've updated my answer to include this information. – NtFreX Nov 28 '17 at 08:06
  • I 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) – Piotr Kula Dec 06 '17 at 09:45
  • 1
    If 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. – Craig Jun 04 '18 at 01:42
  • Copied without attribution from https://learn.microsoft.com/sr-latn-rs/visualstudio/debugger/just-my-code?view=vs-2015 – Martijn Pieters Oct 09 '20 at 09:55
  • 1
    is this supposed to work with Net Core 3.1 ? – mattsmith5 Jun 08 '21 at 19:32
27

For a nuget package source code hosted on GitHub or BitBucket:

  1. 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.

  2. In tools - options - debugging, disable Enable Just My Code, enable Suppress JIT optimization on module load (Managed Only), and enable Prevent using precompiled images on module load (Managed only, resets on restart). When a module 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. When precompiled images are used, it sometimes does not show variable values.

  3. Only for Visual Studio 2017 and potentially older versions (this step is not needed since Visual Studio 2019, thx @alex-klaus ): 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.

After this, you should be able to step into methods of a nuget package dll.

xhafan
  • 2,140
  • 1
  • 26
  • 26
  • 2
    This is a great answer! SourceLink has been taken over by microsoft now so heres the new repo https://github.com/dotnet/sourcelink/. The only bummer is this requires packagereference and .NET Core. – Chris Rice Dec 19 '18 at 21:17
  • 3
    What does `Suppress JIT optimization on module load (Managed Only)` do? – Ehtesh Choudhury Dec 12 '19 at 23:39
  • 1
    When 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. – xhafan Dec 13 '19 at 09:23
  • 1
    Step 1 is [not required since Visual Studio 2019](https://stackoverflow.com/a/54980908/968003). – Alex Klaus Sep 21 '20 at 00:39
  • 1
    Great answer!! It works for my debugging with Castle.Core. +1 – Marlonchosky Jan 12 '21 at 21:11
19

There is a much simpler solution:

enter image description here

Simply embed the debug symbols in the dll. Update your nupkg, et voila!

Frank
  • 903
  • 7
  • 14
  • 4
    The above is VS 2022 , in case the UI looks a little unfamiliar. – Frank Nov 22 '21 at 09:56
  • This will set the DebugType property to embedded ```embedded``` But there is something missing. you must also disable `Enable Just My Code`. After that things work great! – Bryan Jul 13 '23 at 15:33
18

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

ds4940
  • 3,382
  • 1
  • 13
  • 20
10

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):

  1. Create "*.symbols.nupkg".
  2. Deploy symbol package to SymbolSource server.
  3. Configure IDE, Package consumers can add https://nuget.smbsrc.net/ to your symbol sources in Visual Studio.
  4. Add required Library to project using NuGet (from our SymbolSource server).
  5. 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)

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 2
    This 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" – Piotr Kula Nov 23 '17 at 15:48
2

As it might help someone else, here is an additional explanation of the problem in-hand.

What do I need to debug a pkg created by me?

  1. As others here said. the .pdb file.
  2. Also, the source code as expressed here.

Well, how can I include the source code of my nuget package?

  • I got to include the symbol packages. This answer here showed me how.
M. Abouzeid
  • 167
  • 7
2

I got this working by packing the package in debug mode and installing it from my local NuGet source. Then when you debug you can step into your library. You can add your NuGet local source that points to your local folder in Tools -> Options -> Nuget Package Manager -> Package Sources

alex
  • 41
  • 2
0

Just try to use this on a function called from nuget

Picture

It Works for me

Famouse
  • 13
  • 3
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 27 '23 at 10:06