How to work on a third-party NuGet package and verify its work in your application

Rus
Publication date: 2020-08-23

Every so often, I find myself in a situation when I need to change something in one of the open-source .NET libraries I use. Sometimes, when contributing to a third-party library, I want to verify the changes in my own application immediately. There could be various reasons for doing this: either I want to validate the resulting design of my changes, and whether they'll be useful in a real environment, or I'm developing a complicated protocol that should better be checked in my application before being contributed to an upstream library.

Either way, I need to install a changed version of a library into my local application without too much fuss, and it's not an easy task. People sometimes start to replace stuff in their local NuGet storage (~/.nuget) and develop some devious scripts devoted to this purpose, or recommend adding <ProjectReference Include="..\..\library-source\library.fsproj"> into the sources of your own application.

After a couple of tries, I have inferred my own—somewhat successful—practice of working on such packages, which I've decided to describe in this post for further reference.

First of all, you'll need to figure out how is the library versioned (e.g. what do you need to change to publish a new version of the library), and how do you produce *.nupkg packages of the library. In modern world, it is often simple, because almost everyone uses dotnet pack, but for some more complex projects it could become a very hard or unobvious task.

After that, you may follow this protocol:

  1. Make the desired changes in the library code.

  2. Update the library version (e.g. to 1.0.0-pre)—usually by changing its project file—and then prepare its *.nupkg (e.g. by calling dotnet pack on its sources).

  3. Create a directory for temporary NuGet storage on your disk (for example, I have it in my TEMP directory, it is called T:\Temp\nuget-source and is shared between all the projects I use).

  4. Copy the *.nupkg file over to that storage directory. It is now a NuGet feed.

  5. Create a nuget.config file in your application project (get the template by running dotnet new nugetconfig in your terminal). If you want to share the config between multiple projects, you may place it in a parent directory. For example, if I want all my projects in W:\Projects to get this new feed, then I could create a file named W:\Projects\nuget.config.

  6. Add the following item into your nuget.config:

    <configuration>
      <packageSources>
        <add key="local" value="T:\Temp\nuget-source" />
      </packageSources>
    </configuration>
    
  7. You're all set. Now, add or update the library version dependency in your application (usually by updating the <PackageReference Version="…" /> attribute), and test it out.

After you perform this procedure a couple of times, you'll get used to it, and it will become very easy and straightforward to contribute to any third-party libraries and check the changes in your local applications.