DEV Community

loading...
Cover image for Space Ghost: My Halloween Debugging Horror Story

Space Ghost: My Halloween Debugging Horror Story

Yaphi Berhanu
I like to make JavaScript simple when I can. I hope it's helpful!
Originally published at simplestepscode.com ・2 min read

Yesterday I faced a nasty bug.

I needed to integrate some third-party code that would dynamically insert a script onto a page.

It was billed as a simple copy and paste, but these things rarely are.

I pasted the code in the console, and it didn't work. Nice. Time to lace up my Troubleshoot Boots™.

So what was the problem?

  • Was it the way the URL was being inserted (.src vs setAttribute)? - No
  • Was it the script url itself? - No
  • Was it a cross-origin issue? - Nope
  • Was it the site I was using this code on? - No
  • Was it the browser? - No

Finally, I asked myself, had basic script insertion stopped working altogether.

I rewrote my test case for the code from scratch just to make sure nothing was mistyped. Then something crazy happened. The new code worked.

The crazy part? It was exactly the same as the non-working code.

Baffled, I put both versions of the code into a diff checker.

Then I saw the problem: invisible spaces.

The integration code came from a PDF, which had automatically inserted unwanted zero-width spaces, meaning that the code would break while the offending characters would stay invisible.

Once I deleted the spooky space ghosts, the problem went away.

Key takeaway:
If you ever need to paste code from a PDF, an HTML page, an Excel sheet or anything that's not a plain text file, run it through some kind of validator to make sure there aren't any unwanted characters.

I hope this helps in case you run into a similar issue.

This post originally appeared on Simple Steps Code

Discussion (4)

Collapse
skuzzle profile image
Simon Taddiken

I feel your pain. The same issue arises when copying non-escaped code from HipChat.
As it was java and syntactically correct it drove me insane to see the IDE marking it erroneous. Took me way longer than it should to realize that there were hidden characters. Thanks Atlassian

Collapse
yaphi1 profile image
Yaphi Berhanu Author

At least we both know what to look for now.

Collapse
alesinicio profile image
Fúfis

Had similar problem when I had to share keys with another dev. The key was used - among other things - as salt to hash some data, but the hashes were coming out different. Hours later (and OBVIOUSLY convinced that MD5 was broken, of course), we find the invisible char. In my case, the giveway was hashing the key itself.

Collapse
yaphi1 profile image
Yaphi Berhanu Author

Invisible characters are such effective trolls.