Debugging Open Source dependencies included as NuGet packages

You may encounter the need to debug into a dependency that is NuGet package. If this NuGet package is proprietary, you need to contact the vendor. However, if the NuGet package is open source, perhaps on GitHub, then you have all the tools you need to debug into it. Debugging into an open source NuGet package is what this article is about.

We are going to use Rhyous.StringLibrary for this example. It is a simple open source project that provides some common extensions to strings. These are extensions that are often found duplicated in many different projects and sometimes multiple times in the same project.

Step 1 – Check out the Source

Check out the repo from GitHub. You need a Git client. If you don’t have one, you can use GitHub Desktop or the one that is included in the Windows install of Git.

  1. Check out the repository: 
    git fetch https://github.com/rhyous/StringLibrary.git 

Step 2 – Compare Assembly Versions

Some NuGet packages have different assembly versions than the code. I know, they shouldn’t be it happens. Make sure that the assembly version of the dll reference via the nuget package is the same as the assembly version in the downloaded source.

  1. In your project that references the NuGet package, expand the references, highlight the dll that came from the NuGet package, and note the assembly version.

  2. In the download NuGet package source project, check the Assembly version. This is different in .NET Framework and .Net Standard, but it should be easy to figure out in both.

Step 3 – Build the Solution

  1. Open the StringLibrary.sln in Visual Studio.
  2. Click Build.
  3. Go to the output directory and copy the dll and pdb files.

Step 4 – Copy the dll and pdb to your solution

If you go to your project that references the dll, find and highlight the reference and go to properties, you can see the full path to the referenced dll.

  1. Go to the solution folder of the project you are working on.
  2. Go to your project that references the dll.
  3. Under References, locate the dll.
  4. Go to Properties of the dll reference by pressing F4.
  5. Note the path to the dll.
  6. Go into the Packages directory.
  7. Find the folder for Rhyous.StringLibrary.
  8. Locate the dll folder. 
  9. Rename the existing rhyous.stringlibrary.dll to rhyous.stringlibrary.dll.orgininal.
  10. Copy the compiled dll and pdb from Step 2 to this folder.
  11. Clean and build your solution.

Step 5 – Add a breakpoint

You should now be able to step into the Rhyous.StringLibrary source from your project.

Note: If you have two instances of Visual Studio open, one for your project and one for Rhyous.StringLibrary project, you may think you put the break point in on the one with the SimplePluginLoader project. You don’t.  You don’t even need the Rhyous.StringLibrary project open, unless you need to make a change and recompile and recopy the dll and pdb to the packages directory. You simply need to step into the code in order to set a break point.

Note: One trick is to go to Tools | Options | Debugging | General and turn off Step over Property operators (Managed Only).

  1. Debug your poject.
  2. Put a break point on the call to Rhyous.StringLibrary you would like to step into.
  3. Step into the call to Rhyous.StringLibrary.
    Once you have stepped into the call, you should see it’s source.
    Continue stepping into or over or whatever you would like.
    Once you are in the source, you can add breakpoints.
    Note: If you know how to add a break point without first stepping into the project, let me know.

You should now be easily debugging your NuGet package.

Leave a Reply

How to post code in comments?