I’ve been posting some of my code to GitHub and creating NuGet packages to make it easy to grab my code.
Typically my process is to create a C# project which contains only interfaces. Then I create another C# project which contains an implementation of this interface, and references the first project. By doing this it’s easy for me to swap out the implementation later and keep the same interface.
Creating NuGet packages for these library projects helps me conveniently include libraries in other projects, and manage versioning through Visual Studio.
Usually this is a simple process – I create my nupkg files and host them locally for testing. When I’m happy that the nupkg file does what I want, I’ll publish NuGet packages to https://www.nuget.org/, using the NuGet package explorer with the secret key I got by registering at that site.
However, a few nights ago when I was creating some servo packages, I started to notice some very strange behaviour. When I tried to install a package in VS2015, the behaviour of the library that was installed was definitely not what I had packaged up. I spent a long time seeing strange behaviour and getting increasingly frustrated by it, until I found the reason – NuGet caches files in a selection of locations across my hard drive.
How to find where NuGet caches files
I used the nuget executable to find where the NuGet caches were hidden – I downloaded it from here. At the time of writing, the downloadable executable is version 2.8.60717.93 which is quite old – but fortunately, you can use nuget to upgrade nuget using the command:
nuget update -self
This takes the version to 3.4.3.
There’s a very simple command to list all the places that NuGet caches files:
nuget locals all -list
And I found that it caches files in four separate locations.
C:\NuGet>nuget locals all -list http-cache: C:\Users\Jeremy\AppData\Local\NuGet\v3-cache packages-cache: C:\Users\Jeremy\AppData\Local\NuGet\Cache global-packages: C:\Users\Jeremy\.nuget\packages\ temp: C:\Users\Jeremy\AppData\Local\Temp\NuGetScratch
Given NuGet caches in so many places, it didn’t surprise me that I started to have problems. And fortunately, there’s a simple command to clear these caches using NuGet.
nuget locals all -clear
This clears all the caches.
C:\NuGet>nuget locals all -clear Clearing NuGet HTTP cache: C:\Users\Jeremy\AppData\Local\NuGet\v3-cache Clearing NuGet cache: C:\Users\Jeremy\AppData\Local\NuGet\Cache Clearing NuGet global packages cache: C:\Users\Jeremy\.nuget\packages\ Local resources cleared.
After I cleared this, the NuGet started to behave much more consistently.