Few bugs are as maddening as code that looks correct but will not run. Often the culprit is a character you cannot see. Here is how to hunt it down.
How an invisible character breaks code
A zero-width space inside a variable name makes two identifiers that look identical actually different. A non-breaking space instead of a regular space breaks indentation in Python or trips a parser. A BOM at the top of a file stops a script from running. In every case, the editor shows nothing wrong, because the character is invisible.
These sneak in when you copy code from a web page, a chat tool, a PDF, or documentation, where invisible characters were used for formatting.
Method 1: turn on editor highlighting
- VS Code highlights non-breaking spaces and some unusual characters by default, and you can enable "Render Whitespace: all" to see spacing. Look for the "Unicode" or "ambiguous characters" warnings in the status bar.
- Other editors have "show whitespace" or "show all characters" options that mark where odd spaces sit.
Method 2: search by code point with regex
If your editor supports regex search, hunt the usual suspects directly:
\u200B zero-width space
\u00A0 non-breaking space
\uFEFF byte-order mark
\u200D zero-width joiner
Search for each and you will land right on the offending character.
Method 3: paste it into a cleaner
For a fast check without editor setup, paste the snippet into a cleaner that reports hidden characters. textscrubr scans for invisible characters and shows you a count of each, so you can confirm whether your "impossible" bug is actually a zero-width space hiding in a string or identifier. It strips truly invisible characters from code while leaving your spacing, indentation, and punctuation alone, so it does not change the code itself.
Method 4: check the bytes
In a quick script, compare what you see to the byte or code-point length. If a line is longer than it looks, something invisible is padding it. You can also print the code points of a suspect string to see exactly what is there.
Prevent it
When copying code from any styled source, run it through a cleaner or paste as plain text first. Make it a habit for snippets from documentation and chat tools, where invisible characters are most common, and you will save yourself the next hour-long hunt for a bug that was never visible.