#NOSONAR breaks sonarqube scanner

Hi Sonar Community,

We have noticed something in sonar scanner. If we add the #NOSONAR to a particular line in the file then sonar scanner ignores that line. But if we add a comment within a comment like // #NOSONAR
then sonarqube ignores the entire file.
Is this a bug or something? Please help.

Hi.

What is the programming language? The NOSONAR marker should be added to a comment.

For instance, as Python uses # for comments, the correct way is # NOSONAR, Java and many others use // so it’ll be // NOSONAR and so on.

Hi in python using ‘’"" #NOSONAR “”" breaks the sonar scanner and shows all checks passed for that file.

Just to confirm… Here you said:

The “//” is not a valid Python syntax as far as I remember, this is why the whole file is ignored. You can’t use // # NOSONAR because it isn’t valid Python code (again, as far as I remember).

I know, that was for php.
The python syntax that broke it was mentioned above :slight_smile:

Can someone please confirm and help us?

Python
“”" #NOSONAR “”"
PHP
// #NOSONAR

Both scenarios causes sonarqube to ignore the entire file and all errors associated with it.
Please help.

I’m not sure what exactly happening to you but here are some facts:

  • Comment containing “NOSONAR” will make all issues on this line disappear
  • There is no such comment that will make entire file ignored (probably you just had parsing error). To do that you can rely on sonar.exclusions to completely exclude file from analysis. Also you can mark some file(s) to be ignored by particular rule(s) through SonarQube UI (see docs “Ignore Issues”)

I repeat
Python
“”" #NOSONAR “”"
PHP
// #NOSONAR

Both scenarios causes sonarqube to ignore all errors associated with the file.
Can you please try and check it out yourself.

Hey Juliana,

I am going to suggest that if you want your question answered swiftly, you

  • don’t suggest the person helping you hasn’t done their due diligence to test your scenario with the information available
  • include information like your SonarQube version and SonarPython version
  • upload logs from your analysis attempts where you face this issue
  • upload a zipped up reproducer including the exact files you are facing this issue with and a sonar-project.properties file containing any additional analysis parameters you pass the scanner

Reproducing the issue should be as simple as navigating to the unzipped directory and running ‘sonar-scanner’

This forum is community support — not paid support. The bandwidth for individuals to answer your questions and investigate your issues can be increased by giving back to the community and answering some questions yourself! It’s also a great way to become a SonarQube expert.

Thank you.

Colin

2 Likes

Sure! And here it is: SonarCloud

I created two simple files with issues, then I added the NOSONAR comment to one issue and the other one is still being reported.

BUT I noticed in your examples you mixed “straight double quote” and “curly double quote”:

image

Maybe this is your problem. The “curly double quote” can’t be used for comments, it isn’t recognized by the Python interpreted and your code won’t execute. This causes a parser failure in the SonarPython analyzer and it’ll ignore the whole file as mentioned by @Lena.

I couldn’t reproduce your issue with the PHP code. Are you sure that your PHP code executes correctly after you add the NOSONAR comment?

Also, please check if you have set anything in Administration > Analysis Scope > “Ignore issues on files”. It shouldn’t contain the word NOSONAR.

By the way, as I tried to explain before, I could’ve write the PHP example like:

<?php

true; // NOSONAR
true;

?>

(you don’t need the the # character)

And the Python example like:

for i in range(3):
  pass # NOSONAR
for i in range(3):
  pass

(using the single-line comment syntax in line of the issue instead of a docstring)

I hope it helps.

2 Likes

Absolutely helped.
It is still breaking for Sonarqube 7.7, we just upgraded our system to 7.8.

If it still shows any issues I will reply to your previous message.
and the python quotes is a mere autocorrect issue when I typed it, nothing more :slight_smile:

Greatly appreciate your help. I’m sure it will help others too if they are stuck. :slight_smile: