Issue is reported twice when scanning multi module project

Root/
   --sonar-project.properties
   --ModuleA/
          --src/abc.java
   --ModuleB/
          --src/123.js

sonar.modules=ModuleA,ModuleB

I make breakpoint in my sensor. It is triggered twice

fs = context.fileSystem()
fs.inputFiles(fs.predicates().hasExtension("java"))

“abc.java” can be gotten when handling ModuleB. I check context.fileSystem(), its baseDir is Root/ModuleB. Why files in ModuleA can be gotten in ModuleB?

How can I only get the files in the current module?

Hi,

Would be interesting to share a few more details:

  • the version of SonarQube you are testing your plugin with
  • the exact interface your Sensor is implementing (ProjectSensor, Sensor, …).
  • the Sensor descriptor
  • possibly the entire code of your Sensor
  • analysis logs in verbose mode

FYI Global/project sensors are called once per project, but “normal” sensors are called once per module. In theory the module sensors should only “see” files of the module.

sonarqube version: Enterprise 7.9.1.27448 and Community 6.7.3
sonar-scanner: 3.2.0.1227 and 4.0.0.1744
demo project: https://github.com/gosp/sonar-example-prj
analysis log: https://raw.githubusercontent.com/gosp/sonar-example-prj/master/log.txt

public class G11nSensor implements Sensor {
    @Override
    public void describe(SensorDescriptor descriptor) {
        descriptor
                .onlyWhenConfiguration(x->x.getBoolean(Constants.StringIssueEnableKey).orElse(false) || x.getBoolean(Constants.ResourceIssueEnableKey).orElse(true))
                .name("G11n Sensor")
                .onlyOnFileType(InputFile.Type.MAIN);
    }
    @Override
    public void execute(SensorContext context) {
        FileSystem fs = context.fileSystem();
        FilePredicate predicate = fs.predicates().hasExtension("java");
        Iterable<InputFile> inputFiles = fs.inputFiles(predicate);
        for (InputFile inputFile : inputFiles) {
            ... ...
        }
    }
}

the issue occurs just using “fs.predicates().hasExtension”.
“fs.predicates().matchesPathPattern” works correctly.

Thanks for all the details. Unfortunately the predicates hasFilename or hasExtension are broken for multi-module projects (they will return files from all modules, not limited to the current module the Sensor is running for). I created a ticket:
https://jira.sonarsource.com/browse/SONAR-12421

If your goal is to query all Java files, I suggest you query directly by language:
fs.predicates().hasLanguage("java");
This is more correct because it will handle custom user extensions (even if I admit this is not common in Java to have file extension different than .java).

fs.predicates().hasLanguage("java"); depends on the original java plugin which declares java language.
So if customers don’t install java plugin, my plugin will not work. It isn’t acceptable.
So far, I feel the workaround solution is to compare path prefix to filter files before your jira issues would be done.

Another option is to use a ProjectSensor, if you don’t need to deal with modules. ProjectSensor were introduced in SQ 7.6, so if you want to support older versions, you can use normal Sensors, but with the attribute “global”, to make them behave almost like ProjectSensors: https://github.com/SonarSource/sonarqube/blob/c0c259634b868550a1df35d74b44af2fca84a09f/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java#L95

Project/global Sensors are executed only once for the entire project, and FS queries are executed on the entire list of files.