Search code examples
regexsonarqubesonarqube-scansonarqube5.1

SonarQube: Ignore issues in blocks not working with regex


I followed the instructions in question Ignore Issues in Blocks but since it didn't work, I suspect it may be something related with the regex rather than the Sonar configurations.

I have some code that is generated by EMF. I changed the code generation templates to also add //end-generated tag at the end of block, which looks like this:

/*
* @generated
*/
public class MyGeneratedClass implements Enumerator {

    /*
    * @generated
    */  
    public void myGeneratedMethod() {
        // some code that SonarQube doesn't like

    } //end-generated

}

The idea is that the regex must only match method blocks. So the START-BLOCK must match text @generated as long it's not followed by 'class', 'enum' or 'interface' word after the comment termination.

I created this regex:

For start block:

@generated\h*\s[\s\*]*\/(?!\s*[^\s]*\s*(class|enum|interface)\s)

For end block:

\/\/end-generated

This works in my tests using a simple code with Java Pattern class, which returns true for class sample above:

public static void main(String[] args) throws IOException {
    String regex = "@generated\\h*\\s[\\s\\*]*\\/(?!\\s*[^\\s]*\\s*(class|enum|interface)\\s)";
    String text = new String(Files.readAllBytes(Paths.get("test.txt")));
    Matcher matcher = Pattern.compile(regex).matcher(text);
    System.out.println(matcher.find());
}

However when I add it to SonarQube configurations, it doesn't work, issues found in the generated method are still reported by SonarQube.
I added the regex in:
SonarQube » Settings » Exclusions » Issues » Ignore Issues in Blocks

I also tried with escaped version of regex, and result is the same:

@generated\\h*\\s[\\s\\*]*\\/(?!\\s*[^\\s]*\\s*(class|enum|interface)\\s)

Also adding these settings directly into Sonar properties didn't work, as reported already in the question I mentioned above:

sonar.issue.ignore.block=rule1
sonar.issue.ignore.block.rule1.beginBlockRegexp=@generated\\h*\\s[\\s\\*]*\\/(?!\\s*[^\\s]*\\s*(class|enum|interface)\\s)
sonar.issue.ignore.block.rule1.endBlockRegexp=\\/\\/end-generated

I'm using SonarQube server 5.1.2 and running the analysis from Sonar-ant-task 2.3.

I'm wondering if it may be something wrong with the regex that makes SonarQube not able to match it, or maybe some limitation in SonarQube regex processing?

EDIT: I changed the regex for something more simple to match just the '@generated' word, and it worked. But if I put '@generated[\n\r]' to force to only match if there's a new line after @generated, then it doesn't work anymore.
It seems SonarQube doesn't support basic things such as new line characters. Can someone confirm this?


Solution

  • I confirm that all patterns used to exclude issues are applied line per line. In the end it is always translated to "exclude issues from line X to line Y". I agree this is not perfect (especially now we have precise issue location) but this is an "historical" feature. We will probably not invest time in it, since our mood is to avoid complex configuration. The ideal solution for your use case would be to have https://jira.sonarsource.com/browse/SONARJAVA-71 implemented.