Go To Definition: "Cannot navigate to the symbol under the caret."

C#Visual StudioVisual Studio-2015

C# Problem Overview


I suddenly started experiencing a problem today in my project where clicking Go To Definition gives me a strange error: "Cannot navigate to the symbol under the caret."

enter image description here

I first noticed it some time after installing Costura.Fody via NuGet. I've since uninstalled it but the problem persists. I've cleaned and rebuilt the solution multiple times. There are no instances of "Fody" or "Costura" anywhere in my git commit history, before or after the point in time when this started happening, so I'm thinking that one of my .gitignore'd files is stuck in a bad state.

The problem only occurs if the definition being targeted is in another file; Go To Definition for something in the currently opened file works fine. Peek Definition works fine too. Other solutions/projects work fine as well -- it's only this one that's causing issues.

I'm using Visual Studio 2015. The issue doesn't seem to occur in the 2013 version.

Why does this error occur and how can I fix it?

C# Solutions


Solution 1 - C#

I got the same problem. I did all those steps mentioned in all above comments. It didn't work. But when I closed Visual Studio, deleted ".vs" folder (in the solution folder) then reopened Visual Studio. It now works like a charm. The problem is gone.

Solution 2 - C#

The last couple of days I've been getting this error, at least twice a day.. really annoying! None of the solutions proposed here has worked for me. What I found, and since it was pretty difficult to find I'm writing it down here, was to:

  • Close Visual
  • Open Console and navigate to Visual installation folder, in my computer is C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE
  • run devenv.exe /resetuserdata
  • Open Visual Studio, it's going to take some time to load.

Disclaimer: I'm using Xamarin

TAKE INTO CONSIDERATION WHAT @OzSolomon and @xCasper HAVE SAID:

@OzSolomon >

Know that this will reset many of your IDE customization, including > installed plugins.

Make sure you're comfortable with that before using > /resetuserdata

@xCasper > If you have your settings synced through Microsoft, however, most of > the preferences seem to restore themselves. I say most because it > seems my keybindings did not restore and are back to being default. > Everything else, such as my selected theme and colorization choices, > the layout of my IDE (where I have tabs for instance), and what not > seem to of come back.

Solution 3 - C#

I tried David's solution (the one about clicking Empty Symbol Cache option) but it didn't work for me.

How did I solve it?

  • Close all files
  • Clean solution
  • Rebuild solution

For me this error does not make sense but I solved doing this!

Solution 4 - C#

Just had the same issue and it seems like there is a problem with VS 2015 and TFS. I got the problem after I merged some files to another branch. Tried several things and it worked:

  • Close all open files and then VS
  • Tools->Options->Debugging->Symbols->Empty Symbol Cache
  • TFS Check in and get latest (For some reason it worked after this)

Not sure why it occurs but this worked for me, it might also be worth to clear TFSTemp folder as mentioned by zvi.

EDIT

This path: %TMP%\TFSTemp can be copy-pasted into windows explorer to access TFS temp

Solution 5 - C#

The following fixed this for me:

  • In Solution Explorer, right click the solution name.
  • Select "Clean Solution"

After this successfully completed, I no longer received the error, and I didn't have to close and reopen anything.

Solution 6 - C#

Just do it:

  • Close Visual Studio
  • Go to project folder and delete .user file (may be hidden)
  • Open Visual Studio

Solution 7 - C#

I have the same problem after update visual studio, so this is how do I solve the problem.

  1. Close visual studio.
  2. Delete all .vs folder from my project.
  3. Open visual studio.
  4. Open my project.

Hope this helpful for anyone who has this problem.

Solution 8 - C#

Clean your cache symbols (Tools > Options > Debugging > Symbols > EmptySymbolCache). Open your solution in administrative mode.

this resolved my issue.

Solution 9 - C#

Close visual studio then remove the hidden file name .vs on that project and open again the visual studio then build the project. Boom! All good!

Solution 10 - C#

The answer above is correct, but the path is slightly off, try this instead:

%AppData%\..\Local\Temp\TFSTemp

Solution 11 - C#

I've done all things above but my problem did not solve (trying to open project by VS 2017),

after that I realized that the problem was my csproj file. My project (mvc)

was created by VS 2015... So I edit my csproj file and replace

<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />

by this :

<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.7\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.7\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />

It was all about the version of DotNetCompilerPlatform.

(the way that I understood this issue was that I created new mvc project by VS 2017 and compared both csproj files - the new one and the one that created by VS 2015 - )


Additional related info by https://stackoverflow.com/users/15667/xan (since answers are locked)

For me (VS2019 opening an older solution) it was only affecting one project in the solution. The Imports looked like this:

<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

The working projects only had the

<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

line, so I removed the first 2 and intelisense / go to definition etc. started working again.

HOWEVER the web project now wouldn't run due to missing Roslyn csc compiler.

In the end the solution that worked overall was to update the Microsoft.CodeDom.Providers.DotNetCompilerPlatform and Microsoft.Net.Compilers Nuget packages for the project.

Solution 12 - C#

Ran into this problem when using F12 to try and go to a method definition.

All of the mentioned items (except the /resetuserdata - which I didn't try because it would be a pain to recover from) didn't work.

What did work for me:

  • Exit Visual Studio

  • From a Command Prompt, go to the folder for your solution and run the following code (this deletes ALL bin and obj folders in your solution):

     FOR /F "tokens=*" %%G IN ('DIR /B /AD /S bin') DO RMDIR /S /Q "%%G"
     FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G"
    
  • Restart Visual Studio. Opening the solution should take a bit longer as it now rebuilds the obj folders.

After doing this F12 worked!

As a side note, I normally place this in a batch file in my solution's folder, along side the .sln file. This makes it easy to run later!

Solution 13 - C#

Recently upgraded to VS 2017 15.5.0 and came across this problem. I tried:

  1. Deleting the symbol cache
  2. Deleting my .vs folder.
  3. Rebuilding solution.
  4. Running devenv /resetuserdata

Sadly, none of these worked. I noticed that this was only happening on some projects and not others. On the project where it was failing, I ended up switching all of the framework versions to 4.7.1, did a clean/rebuild and my "Go to Definition" started working again.

Solution 14 - C#

I'd say this is due to the new Roslyn C# compiler, in its github issue tracker you can find many of the actual developers encountering this and trying to fix it, but at present it is still open: https://github.com/dotnet/roslyn/issues/9602

I've spent some time around this also, and I lost the battle. But the following workaround I've found works well for me.

WORKAROUND

  1. Change the keyboard shortcut F12 -> Edit.FindAllReferences (for TextEditor). This will find the definition and all references and focus the root node (definition) in the Find Symbol Results.
  2. Now pressing F12, ENTER will do the same as F12 before.

For me it seems to work even better than just GoToDefinition, because:

  • After hitting F12 you can choose a reference instead of the definition by pressing UP or DOWN
  • You can simply cycle between the references and definition just with these few keys (F12, ENTER, UP, DOWN), easy to remember.

Solution 15 - C#

I'm using VS 2017 15.7.5 and this stopped working for certain test files. I noticed that they were all the new ones I had recently added and that in Solution Explorer there was no arrow available to expand and see the properties / methods.

I excluded and then re-included them into my project and the go to definition command worked again.

Solution 16 - C#

I also came across this once. There is something wrong with TFS in VS 2015.

I followed these steps and it worked

  1. Cleared TFS Cache This might be here:

C:\Users\(UserName)\AppData\Local\Microsoft\Team Foundation\(Version)\Cache

Note: path may vary based on operating system so don't blame me.

  1. Emptied Symbol Cache

    Tools > Options > Debugging > Symbols > EmptySymbolCache

  2. Restarted Visual Studio (It Might ask for connecting to TFS again)

This Worked for me.:)

Solution 17 - C#

Most of the paths submitted by others either don't work or are a little backwards.

Empty this folder:

%localappdata%\Temp\TFSTemp

This worked for me.

Solution 18 - C#

It appears you need to have built the project at least once for caret browsing to become available. If you're experiencing this issue on a project you haven't built yet, try building it. That fixed it for me so I thought I'd throw it out here.

Ctrl+Shift+B

Solution 19 - C#

  1. Check the namespaces of the two classes.
  2. If you are adding classes in the Class library - Make sure you are using the right Project type. There are two class library types. Class Library(Package) and Class Library. (Select the later one as it creates .dll file) which can then be added if needed in other solutions.

Solution 20 - C#

If you recently installed an add-in, consider uninstalling it. That's what worked for me.

Solution 21 - C#

I also faced with the same problem and "Find all references" for selected class has solved this issue.

Solution 22 - C#

Clean solution. Restore nuget packages.

Solution 23 - C#

try cleaning the solution or clean your project, this worked for me

Solution 24 - C#

The following worked out for me like a charm:

  1. I checked the warnings that showed up when I built the Project
  2. Some of them mentioned something about assembly version conflict. Visual studio suggested that I clicked on the warning and hit Enter. A popup window offered to automatically fix the issue and so I did.
  3. Problem solved!

Solution 25 - C#

This weird bug happened to me while importing a Xamarin project into VS15. None of the solutions listed here worked for me. After a day of frustration I also ran into a problem similar to this: https://stackoverflow.com/questions/259398/visual-studio-find-results-in-no-files-were-found-to-look-in-find-stopped-pr

When clicking 'find all' in the search dialog, a popup informed me that other files could not be searched through. so i figured the reason 'go to definition' didn't work was due to the same bug, as looking for definitions in the same file did work.

the weird solution in the above link (pressing Ctrl+Scroll Lock or Ctrl+Break a few times) solved both problems for me.

Coloring of the code (which wasn't working when the 'Go to definition' functionality was broken) also immediately returned.

Solution 26 - C#

After doing a "devenv.exe /resetuserdata", my VAssistX fails, so, actually, this reminds me that VAssistX can resolve the problem by Goto Implementation (Alt+G).

Solution 27 - C#

My problem was that I (semi-accidentally) changed the property Build action of the problematic .cs file to Content. Changing it back to Compile did the trick, which makes sense.

Solution 28 - C#

After I read that this particular issue was fixed in the roslyn compiler (see this issue at GitHub), I updated Visual Studio from version 15.0.x to 15.3.1. It works like a charm now.

Solution 29 - C#

For me the navigate works just NO XAMARIN SOLUTIONS. That suggestions here DIDN´T WORKS. :( Devenv.exe /resetuserdata not works for me.

My solution was: Re-create the solutions, project, folders and works. No import. Detail: my project was on the VS 2015, the error was on the VS 2017.

Solution 30 - C#

I had this error for quite sometime now until I couldn't take it anymore so I have tried all possbile solutions above, but none worked for me. I noticed that the error only pops up on a certain project only (like what a user mentioned above) but not on other projects. Because of that, since nothing is working, I cloned my project again on a different folder and it started working again.

Solution 31 - C#

For some reason my call_name.xaml.vb class had been excluded from the project (it seemed to happen after I got latest version from TFS). I just had to include it again and it works now!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeffView Question on Stackoverflow
Solution 1 - C#AnthonyView Answer on Stackoverflow
Solution 2 - C#fabricioView Answer on Stackoverflow
Solution 3 - C#joalcegoView Answer on Stackoverflow
Solution 4 - C#DavidView Answer on Stackoverflow
Solution 5 - C#Chris SzalajView Answer on Stackoverflow
Solution 6 - C#HelperView Answer on Stackoverflow
Solution 7 - C#wut-excelView Answer on Stackoverflow
Solution 8 - C#user3724031View Answer on Stackoverflow
Solution 9 - C#KhailXproView Answer on Stackoverflow
Solution 10 - C#user2386254View Answer on Stackoverflow
Solution 11 - C#Mohammad Galouz LeeView Answer on Stackoverflow
Solution 12 - C#TheHUTManView Answer on Stackoverflow
Solution 13 - C#billbView Answer on Stackoverflow
Solution 14 - C#LubosTomsaView Answer on Stackoverflow
Solution 15 - C#LiamView Answer on Stackoverflow
Solution 16 - C#Maulik ModiView Answer on Stackoverflow
Solution 17 - C#MohannadView Answer on Stackoverflow
Solution 18 - C#trademarkView Answer on Stackoverflow
Solution 19 - C#DevView Answer on Stackoverflow
Solution 20 - C#tstutsView Answer on Stackoverflow
Solution 21 - C#Artem UstimovView Answer on Stackoverflow
Solution 22 - C#ziaprogView Answer on Stackoverflow
Solution 23 - C#Saahithyan VigneswaranView Answer on Stackoverflow
Solution 24 - C#CharalabosView Answer on Stackoverflow
Solution 25 - C#Jake WaldenView Answer on Stackoverflow
Solution 26 - C#macrodView Answer on Stackoverflow
Solution 27 - C#okkkoView Answer on Stackoverflow
Solution 28 - C#Franz WimmerView Answer on Stackoverflow
Solution 29 - C#VoumirView Answer on Stackoverflow
Solution 30 - C#user742102View Answer on Stackoverflow
Solution 31 - C#brandonstrongView Answer on Stackoverflow