VS Reference Blues

When code builds but looks invalid, and intellisense is dead

I recently encountered what I at first thought was a bug in Visual Studio, but which turned out to be a poorly described configuration error on my part.

The problem manifests itself like this:

Apparent reference errors
It looks like references are missing, and intellisense is broken, but the code still builds and runs correctly.

Notice the red color – that is code that Visual Studio can not identify and provide intellisense for. This is valid code that builds and runs correctly, but which still appears invalid in the editor. There is no error message, and no obvious reason for this. Furthermore you can navigate to the class either by clicking F12, or by right clicking it and choosing go to declaration,  yet Visual Studio still looks as if unable to identify the class and provide any coding help for it.

Explanation

The explanation is quite simple, even if it was not initially obvious:

The same component is being referenced twice; once as a nuget package, and once as a project reference.

When I opened the .csproj file for my project, I found something like the above: An xml node <PackageReference ... /> and a node <ProjectReference ... /> that included the same project from two different sources.

My main project referenced a secondary project as a nuget package, but that secondary project was also available in the same VS Solution. At some point I had probably included it as a project reference by accident, which apparently resulted in Visual Studio getting confused.

Solution

Easy fix: Delete the project reference. In the example above, I just removed the whole <ItemGroup> section from my project file. Result: Everything immediately works the way it should:

References and intellisense back in action!

Engage and contribute for the common benefit of mankind!

This site uses Akismet to reduce spam. Learn how your comment data is processed.