[c#] Go To Definition: "Cannot navigate to the symbol under the caret."

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?

This question is related to c# visual-studio visual-studio-2015

The answer is


Just do it:

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

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!


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


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!

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!


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.:)


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.


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!


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.


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

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

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.


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

this resolved my issue.


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.


Clean solution. Restore nuget packages.


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.

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


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: Visual Studio "Find" results in "No files were found to look in. Find stopped progress."

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.


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


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.


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


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


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.


  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.

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).


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.


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.


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.


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.


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!


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.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to visual-studio

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references

Examples related to visual-studio-2015

How to download Visual Studio Community Edition 2015 (not 2017) The default XML namespace of the project must be the MSBuild XML namespace tsconfig.json: Build:No inputs were found in config file How to install Visual C++ Build tools? Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" How to unapply a migration in ASP.NET Core with EF Core The term "Add-Migration" is not recognized Visual Studio 2015 Update 3 Offline Installer (ISO) Getting "project" nuget configuration is invalid error ASP.NET 5 MVC: unable to connect to web server 'IIS Express'