Fixing CodeLens in Visual Studio 2017
I was recently having issues with CodeLens not showing all the data in Visual Studio 2017. Thanks to Rosen Dash and the TFS team at Microsoft I was able to get my issue resolved. Here’s the problem, and solution, I was having for others that may be experiencing the same issue.
What Is CodeLens
If you don’t know what CodeLens is then you’re missing out. CodeLens is a set of indicators in the editor shown above the members of a type and types themselves. At a basic level you can see the number of references to that identifier and, when clicked, get the list of references. It basically replaces the Find All References option. But if this were it then CodeLens wouldn’t be that useful. Instead it also can provide information about
- Who made recent changes
- The status of tests against the code
- Check ins against the code
And, yes, it works with TFVC, Git and Github. It is awesome. I personally find the references, test status and recent changes to be very useful. If you are not yet using this tool then enable it now in Visual Studio (Tools\Options –> Text Editor\All Languages\CodeLens).
After updating to Visual Studio 2017 I noticed that only the references were showing. I deleted the .vs folder (a standard solution for fixing problems in VS) but that didn’t help. After trying to get things to work for a while I finally submitted a ticket at the VS Developer Community (aka VS Feedback). It took a while, going through several different tests and emailing back and forth. At one point Rosen and some of the team even skyped so they could see the problem and I could share dumps of Visual Studio. Finally they were able to identify the problem.
CodeLens does not behave correctly if you have an older version of TFS data. When I mean older version I don’t mean you’re using an older version of TFS but rather that your TFS instance has been updated over time. One of the things I noticed in the Fiddler trace and activity logs was that VS was using service locator to find some services by guid. My TFS instance was returning that those guids weren’t valid. Was this the problem? I cannot say but the team did trace down the issue that my TFS instance (which had been updated across versions of TFS over the years) was not behaving correctly with CodeLens. It seems that CodeLens has an issue with upgraded TFS instances.
The formal solution is that CodeLens needs to be updated to handle this scenario that they found. But fortunately I don’t have to wait for the fix. If you are running into the issue with CodeLens not showing all the indicators and you’re running against TFS then do the following to try to fix it.
- Remove all TFS servers from Team Explorer’s Manage Connection dialog.
- Close all instances of Visual Studio.
- Delete the TSF cache at the following locations.
- %LocalAppData%\Microsoft\Team Foundation
- Delete the registry entries under HKCU\Software\Microsoft\VisualStudio\15.0\TeamFoundation\Instances.
- Start Visual Studio.
- Add the TFS server(s) back to Team Explorer.
Note a couple of issues with this approach. Firstly, you are going to wipe all the TFS settings so you will have to reconfigure TFS, including any mapped workspaces. I would recommend that you don’t have any changes checked out before doing this.
Secondly, Team Explorer won’t actually let you have an empty list of connections nor will it allow you to delete the active connection. So you might need to point it to GitHub first.
After all this, CodeLens should be working again. Thanks to Rosen and the team for helping figure this out. It was really frustrating given the usefulness of this extension.