Reputation: 10517
I have a problem with Visual Studio on a C# solution. It displays totally random errors, but the projects build. Right now, I have 33 files with errors, and I can see red squiggly lines in all of them.
I tried cleaning / rebuilding the solution, closing Visual Studio and even restarting my computer. I can modify .cs files and I see the changes in the solution.
Does anyone have an idea about why it does that?
Upvotes: 361
Views: 162015
Reputation: 41
Haven't seen this answer yet - the only thing that worked for me is updating some of my Nuget packages that were out of date.
Upvotes: 0
Reputation: 987
In my case, I solved the problem by removing a superfluous element from the .csproj file as follows:
<ItemGroup>
<Compile Update="UserControls\MyUserControl.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
It turned out that this code file is compiled anyway, as it is part of the source directory tree. After the above lines have been removed, no errors are displayed any more.
Upvotes: 0
Reputation: 1097
The best way to solve strange project configuration errors like this is to open the project file (right click on project select 'edit project file') then look at the item groups and see if the order and files in these list are what you expect them to be.
eg.
<ItemGroup>
<Compile Remove="out\of\date\path\to\foo\foo.cs" />
etc.
Sometimes (especially if you move files around) the project file can get out of date and still have old invalid entries in it, and you'll never know it by looking in the solution pane settings.
Upvotes: 0
Reputation: 2264
So many things that could cause it, as evidenced by the long list of answers here. Here's what fixed it for me, having tried pretty much everything else first.
Build your solution in DEBUG mode. Then build it in RELEASE mode (it shouldn't build when it has red wavy lines, but in my case it was just warnings that should have had green wavy lines but it was getting in a muddle and giving them red wavy lines, and it built anyway even in release mode). Then build in in DEBUG mode. Spitting on your hands and turning around three times optional.
Worked for me, when nothing else did.
Upvotes: 10
Reputation: 29149
tldr; Unload and reload the problem project.
When this happens to me I (used to) try closing VS and reopen it. That probably worked about half of the time. When it didn't work I would close the solution, delete the .suo file (or the entire .vs folder) and re-open the solution. So far this has always worked for me (more than 10 times in the last 6 months), but it is slightly tedious because some things get reset such as your build mode, startup project, etc.
Since it's usually just one project that's having the problem, I just tried unloading that project and reloading it, and this worked. My sample size is only 1 but it's much faster than the other two options so perhaps worth the attempt. (Update: some of my co-workers have now tried this too, and so far it's worked every time.) I suspect this works because it writes to the .suo file, and perhaps fixes the corrupted part of it that was causing the issue to begin with.
Note: this appears to work for VS 2022, 2019, 2017, and 2015.
Upvotes: 175
Reputation: 206
In my case helped a combination of things:
bin
folder contents.vs
folderGeneric.xaml
e.g. <ResourceDicitonary Source="example/path/somefile.xaml">
when I played around with the path trying to change it to a wrong one re-building then correcting it and rebuilding again, then this finally cleared all errors. It was specifically WPF project if that's relevant.Upvotes: 2
Reputation: 539
I experienced IntelliSense displaying distracting errors that did not exist, but still had the ability to build and debug the project in Visual Studio 2019. This issue did not occur in Visual Studio 2017. On top of this issue we were unable to navigate to various references in Visual Studio.
After trying all of the options posted and finding this post about navigating symbols: https://stackoverflow.com/a/49100341/999011
The solution for our situation was related to the Microsoft.Net.Compilers
&
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
references in the project file, which were mentioned in the above post.
However, I never updated them I just found that there were multiple references to different versions in the project file. After cleaning those up and only having one reference for each package Microsoft.Net.Compilers
&
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
, distracting red squiggles disappeared and navigating symbols started working.
I can only theorise that the extra references were added during upgrades as I think this project was originally created in Visual Studio 2015.
Upvotes: 0
Reputation: 869
In my case when I tried to work with a project written with C# 9.0 for the first time after updating sdk to latest, it would show red lines no matter what I did (it would build fine). I have tried everything here, but nothing worked. Finally I realised that the problem was with my Resharper's syntax highlighter from an older version. Once I updated Resharper all the red went away.
Upvotes: 1
Reputation: 189
I've been struggling with this issue for over a year and none of these solutions helped me:
I finally fixed this issue - I opened the vbproj/csproj file in notepad and noticed that in the ItemGroup section, there was a reference back to my main project dll. I deleted this reference, reopened my solution and the problem was fixed.
Upvotes: 2
Reputation: 71
There are a lot of answers to delete the SUO / hidden solution files.
In my case it was because I needed to run Visual Studio as an Admin in order publish. Which overrode those files with admin permissions. Now when running as a standard user I can not get rid of any errors.
If I re-run in admin mode I am able to resolve all the errors.
Upvotes: 0
Reputation: 9324
My symptoms in VS2019 were that I would build with some errors. Then I'd fix the errors, and the build would work, as shown in the Output window. But the Errors windows still showed the old errors. I could run it just fine. Closing VS2019 and reopneing fixed the issue, but only for a little while. This started happening on version 16.4.3
This solution seems to work for me:
Uncheck Tools->Option->Projects and Solutions->General->Allow parallel project initialization
I found this fix buried way down in the comments here: https://developercommunity.visualstudio.com/content/problem/483450/vs-2019-intellisense-reports-compile-errors-when-r.html
Upvotes: 4
Reputation: 10517
Here's a collection of popular answers. Upvote the OP of the answer if it helped you:
As @Mike Fuchs mentioned, try the following operations:
In menu, Build > Clean Solution
And
In menu, Build > Build Solution
and select the project in question, and click on the refresh button:
As @Pixel mentioned, try the following sequence of operations:
- Clean the solution
- Close Visual Studio
- Open Visual Studio
- Build solution
If you have ReSharper, try emptying the ReSharper cache:
In menu, ReSharper > Options > Environment > General > Clear Caches
and disabling and re-enabling ReSharper:
In menu, Tools > Options > ReSharper > General > Suspend / Restore
As @Neolisk mentioned, deleting the .suo file might solve your problem. For Visual Studio 2015, the file is located in:
[Path of Solution]/.vs/[Solution Name]/v14/.suo
And for Visual Studio 2017:
[Path of Solution]/.vs/[Solution Name]/v15/.suo
Note that the .vs directory is hidden.
As @TTT mentioned, try unloading the project that causes problems:
In Solution Explorer, right-click on project, Unload Project.
And re-loading it
In Solution Explorer, right-click on project, Reload Project.
As @Guilherme mentioned, try removing and adding the reference to "Microsoft.CSharp" from the projects that have problems.
In Solution Explorer, expand the project, expand "References", right-click on "Microsoft.CSharp" and Remove.
Then, right-click on References > Add Reference, select "Microsoft.CSharp" from the list and click OK
Upvotes: 24
Reputation: 1098
Deleting .vs
folder solved my problem.
But it also reset my solution's current settings in VS. Like, my unloaded projects in the solution were re-loaded and all the pinned and opened documents were also closed when I restarted the VS.
Upvotes: 5
Reputation: 924
Delete the hidden file path = your solution\ .vs\ your solution Name \v15\ .suo
Upvotes: 17
Reputation: 5415
I've noticed that sometimes when switching git branches, Visual Studio (2017) will not recognize types from some files that had been added in the second branch. Deleting the .vs folder solves it, but it also trashes all your workspace settings. This trick seems to work well for me:
This causes Intellisense to parse the file that it missed when switching branches.
Upvotes: 5
Reputation: 483
I found that this can happen if the referenced project is targeting a higher version of the framework than the project that is trying to use it. You can tell if this is the problem by going to the output window and looking for something similar to this:
The primary reference "my_reference" could not be resolved because it was built against the ".NETFramework,Version=v4.7.2" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.7".
The solution is to change the target framework of one or other of the projects.
Upvotes: 1
Reputation: 1670
Had this issue at work (running VS2017). Tried all of the answers here. No joy.
The project would build just fine, but was complaining that namespaces / types couldn't be found. Red squiggles all over the place. Lots of errors in the Error List window.
My solution contained 3 projects.
Discovered that 3 of the NuGet library references for one of the projects was out of line. Consolidated the referenced library versions, and Bingo.
Hope this helps someone.
Brett.
Upvotes: 1
Reputation: 6821
A colleague of mine experienced this issue today. We tried many of the recommendations here and none worked except the solution described below.
Project builds fine but Intellisense fails to recognize certain types and marks particular using
statements as invalid.
Change the 'Solutions Platform' (in VS 2017 this is the dropdown next to the Solution Configuration dropdown and has values such as x86, x64, AnyCPU, Mixed Platforms, etc.) to AnyCPU.
The platform for your project may vary, but it seems as though some references may not be valid for all platforms.
Upvotes: 2
Reputation: 10517
If you have ReSharper, try emptying the ReSharper cache:
In menu, ReSharper > Options > Environment > General > Clear Caches
and disabling and re-enabling ReSharper:
In menu, Tools > Options > ReSharper > General > Suspend / Restore
Upvotes: 348
Reputation: 14422
I found that happens frequently when using Git in Visual Studio 2017, switching branches where there is dependent code changes. Even though the project will build successfully, there will remain errors in the error list.
These errors are often namespace issues and missing references, even when the library reference exists.
To resolve:
Upvotes: 54
Reputation: 5951
TL;DR: Perform a clean re-install of Visual Studio
After wasting several hours, I still couldn't fix it for Visual Studio 2017. I then installed Visual Studio 2019 PREVIEW and all of a sudden, IntelliSense displays me the members of STL-classes again (which it doesn't with Visual Studio 2017).
So, my guess is that there might be also something wrong with Visual Studio itself (maybe something in the cache directory or in general something on your PC which is not directly related to a specific solution) which can be solved by a clean and complete re-installation of Visual Studio. I know, this is a stupid "solution", but in my case, only a fresh Visual Studio (2019) installation had an effect.
As already mentioned, in my case, only STL classes were affected. IntelliSense wouldn't display their members which is odd. I thought, it could maybe have something to do with precompiled headers. Somewhere I read that the STL and the project should be on the same drive and putting them onto the same should solve the problem. But none of these routes led to success.
Upvotes: 0
Reputation: 51
Following solution worked for me
1 - Close VS
2 - Delete .vs folder
3 - Open VS
4 - Build solution
Upvotes: 5
Reputation: 118
I hope solve your problem
Upvotes: 1
Reputation: 291
I have tried all the 6 options, nothing worked for me. Below solution resolved my issue.
Close VS. Delete the hidden ".vs" folder next to your solution file. Restart VS and load the solution.
Upvotes: 29
Reputation: 1499
After trying all of the options listed I discovered yet another reason why this can happen. If somebody sent you the source code as a zip, or you downloaded a zip, Windows may have blocked all files. 2 ways to solve this:
Method 1:
Right click on the original Zip file -> Check 'Unblock' -> Click apply
Method 2:
If that's not an option, rather than opening properties on every file in the solution folder simply open power shell and unblock recursively using the following:
Get-ChildItem -Path 'C:\<ROOT FOLDER OF SOLUTION>\' -Recurse | Unblock-File
Upvotes: 1
Reputation: 1
in my case vs was never retaining the imported namespaces in the project properties > references
when I tried to add/check them again I couldn't and vs threw an error and when saved project vs crashed. When I reopened all the standard imported namespaces (system.data etc...) were all ticked again and it then was recognising everything without error
Upvotes: 0
Reputation: 10003
0 - Right click on the Solution and clean solution
1 - Close VS
2 - Delete project's .suo file
3 - Open VS
4 - Build solution
Upvotes: 2
Reputation: 81
Ran into this issue with a single type not being recognized by Visual Studio which showed the red squiggle even though the solution built successfully. I noticed in the Solution Explorer the file did not have the expand arrow on the left which shows classes and properties on expansion.
The fix was to Exclude the file from the project and save/build which produced an expected error and then Include the file in the project and save and build.
After performing these steps Visual Studio started to recognize my type again. Looking at the diff in git it appears the issue was due to line endings not matching on the <Compile Include="..." />
line of my .csproj file.
Upvotes: 0