Zero Width Space Causing Compiler Error

I have a problem where a piece of code, which was copied from Slack (this seems to be the source of the error) into an existing Kotlin file, made the code unable to compile. At first it was just a matter of a missing import, but even after that had been taken care of, it still failed with

(22, 1): Expecting a top level declaration

After removing some blank lines, and some line feeds and replacing them with new blank lines and new line feeds, the code compiled. Now, I am lucky it only took me ten minutes to figure out, because this is something that could have taken hours, especially for someone new to Kotlin. I was so confident that the code was correct that I could quite quickly deduce that it was likely to be this sort of error, but a novice Kotlin programmer might have thought it was their code that was at fault.

So I have some questions

  1. How can I prevent this from happening again?
  • Can I somehow display line feeds in Intellij, and what sort of line feeds it is? (I recall having this option before, but I cannot find it any more)
  • Is it possible to show an error in Intellij when any other sort of whitespace other than the “correct” whitespace (U+0020) is present? The compiler gave an error, which was indicated in the editor, but it was very hard to see since it wasn’t any character to display the error one.
  • Does someone have a git commit hook to prevent this sort of error and preventing a bad character being committed into the code base?
  1. Is it possible to improve the error message for this kind of compilation error?

I can’t include the entire code, but I can include the patch that fixed the problem. While adding the patch to GitHub Gists, GitHub noticed and pointed out that there was a non standard white space in it. It appears that the culprit was a zero width space (U+200B).

Would lint (Analyze >> Inspect Code…) detect the problem?

As I see, this problem doesn’t have a good solution yet (both in Kotlin compiler and Intellij).

Related issues:

1 Like

Well, if you have a compile error, the linter will tell you about it, but it just displays the compile error message, which is as mentioned, not good enough in this case.

Thank you @Alexey.Belkov, at least I know it is something you are aware about. Hopefully this will be handled better in the future then.