Thursday, November 23, 2023

This looks like a NuGet bug - but isn't

Error NU1301 The local source '[PATH]' doesn't exist.

I recently reported a bug where I was trying to use an updated version of a NuGet package. When I specified a new version of that package (in Directory.Packages.props), I got build errors complaining about a path to another project in the solution.

I got the error message shown above, but the path was relative to another project in the solution.

Very odd.

Why would the newly updated package version do anything with the referenced path?

It turns out it didn't.

But, it took me two days, and then the answer popped into my head while walking back from the shop, to explain what was happening.

A couple of days earlier I'd been testing the package created by another project in the solution.

To do so, I'd created a local package source that pointed to the output path of that project. [Those of you paying attention will have already guessed that this is also the path from the error message that started all this.] Then in a different solution, I could load my test project and add a reference to the package I'd built for testing.

All was good. I tested that package and carried on with my work.

Here's an example of the package source I created for testing. [Ignore the parts that are obscured. They're not relevant.]

See the path I'm setting in the image: "c:/source/MyProject/Release".

The Package Sources Window - showing a custom configured source
If this path doesn't exist when creating (updating) the source entry you get a helpful error message:
Package Manager Options - The source specified is invalid. Please provide a valid source.

That's a good, clear error message. It clearly makes known what's wrong.

Now, guess what happens if that path exists when the source is created but is later deleted.

It's a reasonable scenario. Create a local package source for testing and then later delete the directory when testing is finished.

Or rather than explicitly delete it, maybe you reset a branch, and that causes the path to be deleted.

That's what I did.

Then, a few days later I try and add a version of a package I've never used before.

And that's when I got the error.

So, what's going on?

Well, what I assume is happening is that before downloading a new version of the package, it checks all the configured sources to see if it exists in any of them. It (the download manager) doesn't know that the package only exists on and quite rightly checks all configured options before downloading something again unnecessarily.

Except, in my situation, it's now trying to look in a directory that no longer exists which causes an exception.

There's an argument that it might be good to add an option to ignore local sources that don't exist but I expect that to be likely to also cause a number of obscure exceptions that aren't obvious to diagnose.

I expect if I had tried to use the UI to update the package version rather than directly editing the props file I'd have got a different (and probably more helpful) error message. - [Actually, I've just checked and  it's the same error message but seeing that I'm using ALL Package sources may have helped me spot what's wrong.]

So, not a bug in the package I was trying to use, and not a bug in the projects that reference that package.

If anything, this is just a limitation of options for testing a package locally and poor error messages.

Or it's me being an imposter for thinking I knew how to use Visual Studio and NuGet. 

Or, it's me being stressed and making silly mistakes with configuration.

Or, it's another reason for someone to complain about my code. There's been a lot of that recently. :(

Or maybe it's a reminder for all of us to only keep local NuGet package sources enabled while they're needed.


Post a Comment

I get a lot of comment spam :( - moderation may take a while.