How to Fix the SonarScanner When a Line in a File Is Out of Range

A rather annoying error caught my attention a few weeks ago. Our SonarQube pipeline in Azure DevOps started to throw this error:

##[error]ERROR: Error during SonarScanner execution
ERROR: Error during SonarScanner execution
##[error]java.lang.IllegalArgumentException: 
  Line 19 is out of range for file ***/**.razor. File has 18 lines.
at org.sonar.api.utils.Preconditions.checkArgument(Preconditions.java:43)

When you google for this problem, you most likely find suggestions to delete the previous coverage reports. In this case that did not have any impact on the scanner. Should you run into this problem, you could try these two actions:

  1. Check the file encoding
  2. Add an empty line to the reported file

In our case the file encoding was UTF-8 BOM, while it should have been UTF-8. Fixing the encoding reduced some warnings, but the error persisted.

Only after I had inserted a line break at the end of the last line of code and thus created the mythical 19th line of the error message was the Sonar scanner happy.

If you encounter this problem and everything else has failed, add this empty line.