Saturday, July 23, 2016

MVC CSHTML partial view loses all syntax highlighting/color during rename operation; then copy, cut or paste operations cause Visual Studio errors to be thrown

Obscure problem

This was a weird one and as weird problems go with Visual Studio - they always happen out of the blue and when you really really need to get work done. I was working on an MVC 5 partial view (*.cshtml) and out of nowhere all of a sudden all color was drained from the markup. I lost my syntax highlighting. The MVC editor was already very fickle, so I ignored it and I closed the file and re-opened it because that usually fixes the problem. Well... that didn't work this time. The file still didn't have its color. I needed to get work done and I can work without syntax highlighting or intellisense just fine so I attempted to cut a highlighted section of code and I was met with the following error:

Inconsistent length calculation in projection snapshot.
I have no flipping idea what that is supposed to mean which is why this error is going into my "Application errors hall of shame". Therefore as usual, you click OK, you know - because you are presented with such a huge list of alternatives and you try again. Well trying again gave me this error instead:

Attempted TextBuffer edit operation while another edit is in progress.
I understand this error a little clearer, but honestly it doesn't matter because it is a symptom of the overall problem. Long story short, "Something broke". This message in particular is just saying that you already tried to copy, cut or paste something already and that operation didn't complete so you cannot start a new one. Hence the TextBuffer is in use, you cannot edit it while in use.

Obscure solution

At this point I thought Visual Studio was broken. I should have known better because other CSHTML files were just fine. I am going to break this down in what to do and what not to do:

Don't do these things because they are unnecessary in this situation and won't help

  1. Don't reinstall Visual Studio
  2. Don't repair Visual Studio
  3. Don't run "devenv.exe /resetsettings"
  4. Don't delete your solutions ".vs" hidden folder
  5. Don't delete the contents of your ComponentModelCache folder: "%appdatalocal%/Microsoft/VisualStudio/14.0/"  -  This is assuming VS2015
  6. You can and should install Visual Studio updates, but it won't help with you with this problem

Do this instead

Hopefully this solves your problem, as all weird problems I cannot predict that we are having the same problem. Anyhow - all you have to do is inspect your problem *.cshtml file for something that looks like the following snippet below. This is just an example of actual code I was working with. Please understand this is a subsection and this code isn't supposed to make sense.

Please click the image to view it clearly, it is a little large.
What happened? My file was corrupted during a keyboard operation. I am not certain, but I believe the corruption occurred when I attempted to do a rename operation on a variable on the page using F2. This is a bug in Visual Studio for sure because the MVC view editor is very buggy. It is constantly crashing Visual Studio for me. Please draw your attention to the lines that start with a hashtag "#" - I didn't put those there. VS put them there when the rename operation failed horribly and corrupted the file. To make matters worse, I lost the bottom half of my file. Thank goodness for source control, I was able to recover everything just fine.

#line default
#line hidden

#line 72 "C:\Dev\this_is_the_path_to_this_view\Expanded.cshtml"
 __o = val;


#line default
 rline hidden
r
#line 73 "C:\Dev\this_is_the_path_to_this_view\Expanded.cshtml"
r

Therefore in order to fix this, you have to get rid of all of the corrupted syntax and lines that I am pointing out above. Just be aware that you might have lost the bottom half of your file too. You might want to get forced latest from source control or proceed at your own risk after fixing your file. If the syntax highlighting comes back and you can copy, cut and paste freely again - then the file is fixed.

No comments:

Post a Comment