The Path is Null and Full of Squiggles
I've been using Visual Studio 2017 since the earliest bits were available and I can confidently say, I'm a fan and highly recommend you give it a shot if you get a chance.
With that being said, sometimes new features can cause things to blow up and this post is going to be discussing one of those such features, namely Lightweight Solution Load.
The Terrifying Null Path Error
Recently, I opened up a decently sized solution (~9 projects) and everything seemed in order. All of my dependencies were there, with no warning signs to be seen:
Then I actually opened up a code file and was bombarded with red squiggles:
Bizarre. Visual Studio clearly recognizes all of the dependencies and that they exist within the Project, but the actual code doesn't acknowledge them at all.
Let's try a few things:
- Try a
dotnet restore- Hoping that this was just some bizarre UI issue and that the packages weren't in fact loaded.
- Restart Visual Studio - Sometimes, you just need a good ole' restart to get things back on track.
- Restart Local Machine - I don't nearly restart as often as I should, so perhaps something got out of whack (i.e. user settings, VS settings, etc.)
Nothing, none of these worked - at all. So, I simply tried building the project and as you might expect, yet another plethora of 200+ error messages like these:
The type or namespace name 'Microsoft' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
And we certainly can't go having basic primitive types either:
Predefined type 'System.String' is not defined or imported
After digging around in the Build Output window, I noticed the following message:
Build started ... Build Failure. Error: 'path' cannot be an empty string ("") or start with the null character. Parameter name: path ========== Build: 0 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Now we were getting to the bottom of things, now let's see if we can use this to actually resolve the issue.
The Cause: Lightweight Solution Load
After a bit of research into this issue, I came across a Visual Studio Community Issue that detailed the same basic behavior and error message and it revealed the culprit: Lightweight Solution Load.
If you aren't familiar with Lightweight Solution Load, it's a great feature within Visual Studio 2017 and can make Visual Studio feel much snappier and responsive, especially when working on large projects. It essentially loads all of the projects asynchronously, however attempting to access any particular one will prioritize it's loading so you can get to the code you need quickly. Well - that's great and all, but if this occurs what's the point.
Anyways, back onto actually resolving the problem. So to fix this issue simply disable Lightweight Solution Load for the solution and re-open it.
You'll quickly see after doing this that your dependencies are all loaded (and recognized) as expected:
A Few Potential Alternatives
The earlier discussion within the Visual Studio issues area had quite a few potential other fixes that may/may not work in all scenarios, but are worth giving a try if you really enjoy using the Lightweight Solution Load feature:
- Deleting the .vs and temp directories
- Clearing the NuGet Cache - via Tools > Options > Nuget
Your mileage may vary, but hopefully this helped guide you far, far away from all of those red squiggles.