14

Visual Studio allows the Unicode character Zero Width Space (U+200B) to be pasted into the code editor. This character is (as the name implies) invisible.

This can lead to nasty bugs such as the one I just spent time troubleshooting where HttpWebRequest.CreateHttp(string url) threw a UriFormatException when passed the innocuous-looking string literal "​http://www.umich.edu".

The exception occurred because the first character in that string literal is not h, as it appears, but is the Zero Width Space character. It was put onto the clipboard when I copied the URL from the body of a web page, and was dutifully pasted into my code by Visual Studio when I hit Ctrl+V in the code editor window.

Turning on the "View White Space" option (Edit > Advanced > View White Space) does not cause Visual Studio to reveal that there's a Zero Width Space character present.

I would like Visual Studio to:

  • Give some kind of indication or warning when there's a Zero Width Space character (or other invisible character) in a string literal in my code, and/or
  • Prevent such control characters from being pasted into the code editor in the first place.

Is there a way to make Visual Studio do this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
  • 3
    So, we've run into the same issues in our editor too. Only thing we've found to do is to report it to Microsoft from the feedback area in Visual Studio. In the meantime you can scrub it using a find replace with find options set to use regular expressions and this as your find term `[\u200B-\u200D\uFEFF]` with the replace text being blank. This doesn't do anything to actively alert or prevent it sadly :( – Brian King Mar 03 '16 at 19:50
  • What could be more informative than an UriFormatException to know that there is something wrong with the Uri? It is not a bug, you learned something new :) Silently hacking urls, that's a bug generator. – Hans Passant May 01 '16 at 14:48
  • @HansPassant, that particular exception is fine; what I'd like is for Visual Studio to somehow indicate that there's an invisible character in my (non-verbatim) string literal. Put a yellow squiggle under the string or something. Make it an optional setting (even off-by-default) if people might find it annoying. – Jon Schneider May 01 '16 at 14:50
  • Post feature requests [where they belong](https://visualstudio.uservoice.com/forums/121579-visual-studio-2015). – Hans Passant May 01 '16 at 14:52
  • 1
    Done, thanks! https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/13729794-indicate-when-an-invisible-character-such-as-zer In the meantime, I'm still interested in any solutions available to us in the Visual Studio 2015 that we have available to us today. – Jon Schneider May 04 '16 at 13:24
  • Possible duplicate of [How to highlight invisible unicode character 65279; in Visual Studio Code?](https://stackoverflow.com/questions/47077628/how-to-highlight-invisible-unicode-character-65279-in-visual-studio-code) – Machavity Jul 24 '19 at 02:16
  • 1
    I got into the same trap, spent like 3 hours of looking for what causes it. Endpoint wasn't working for gremlin reason. This zero width space is an evil. Who invented this character must have been really pissed of with devs. :) – David Apr 21 '21 at 07:02
  • 1
    Note: VSCode (again, not Visual Studio) now renders Unicode "control" characters: https://code.visualstudio.com/updates/v1_62#_unicode-directional-formatting-characters. But: this invisible character ("Zero Width Space") is *not* a control one anyway, and would trigger the same issue on VSCode as the one you had with Visual Studio. – VonC Nov 10 '21 at 13:57

4 Answers4

15

The Gremlins tracker for Visual Studio Code worked for me.
https://github.com/nhoizey/vscode-gremlins

P.s.: IntelliJ recognizes those hidden troublesome chars OOTB

enter image description here

4F2E4A2E
  • 1,964
  • 26
  • 28
3

As of December 2021, Visual Studio Code introduced highlighting of invisible characters.

From this

Zero width unicode character error in code

to this

Zero width unicode character highlight

Preferences -> Settings (ctrl + ,) -> search for "invisible characters" -> tick "Editor › Unicode Highlight: Invisible Characters"

VSC Unicode Highlight: Invisible Characters

This option should be on by default.

1

Not sure how recently this was introduced, but in Visual Studio 2022, under Tools -> Options -> Text Editor -> General, there's a "View zero-width characters" option in the Display group: enter image description here

Tom Lint
  • 483
  • 5
  • 13
-2

If you are using "VSCODE" then it's simple, just install gremlins in vscode. After Installing where there are gremlins remove those spaces(with red line) and you are good to go. It works for me.