realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.58k stars 2.22k forks source link

Error while importing swiftlint issues tro sonar #5209

Open Absh-Day opened 1 year ago

Absh-Day commented 1 year ago

New Issue Checklist

Describe the bug

A clear and concise description of what the bug is.

Complete output when running SwiftLint, including the stack trace and command used

When I run swiftlint --reporter json and then try to upload that json file to sonarqube(developer edition), it shows warnings like this while uploading report and in end does not upload because absolute path won't exist on Sonar:

15:39:10 INFO: Sensor Swift Code Quality and Security [swift] (done) | time=417ms 15:39:10 INFO: Sensor Import of SwiftLint issues [swift] 15:39:10 INFO: Importing /Users/ab/workspace/PR-8/lint_report.json 15:39:10 WARN: No input file found for '/Users/ab/workspace/PR-8/Tests/GeneratorTests/ResultGeneratorTests.swift'. No SwiftLint issues will be imported on this file.

The report can also not be uploaded when I run swiftlint --reporter sonarqube and this is whats thrown as exception:

15:49:33 ERROR: No issues information will be saved as the report file can't be read. 15:49:33 com.A.A.B: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 15:49:33 at com.A.A.K.A(Unknown Source) 15:49:33 at com.A.A.K.A(Unknown Source) 15:49:33 at com.sonar.swift.plugin.external.SwiftLintSensor.importReport(Unknown Source) 15:49:33 at com.sonar.swift.plugin.external.SwiftLintSensor.lambda$execute$1(Unknown Source) 15:49:33 at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) 15:49:33 at com.sonar.swift.plugin.external.SwiftLintSensor.execute(Unknown Source) 15:49:33 at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48) 15:49:33 at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85) 15:49:33 at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:59) 15:49:33 at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:77) 15:49:33 at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:59) 15:49:33 at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82) 15:49:33 at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137) 15:49:33 at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123) 15:49:33 at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:392) 15:49:33 at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:388) 15:49:33 at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:357) 15:49:33 at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137) 15:49:33 at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123) 15:49:33 at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:150) 15:49:33 at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137) 15:49:33 at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123) 15:49:33 at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72) 15:49:33 at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66) 15:49:33 at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46) 15:49:33 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 15:49:33 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 15:49:33 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 15:49:33 at java.base/java.lang.reflect.Method.invoke(Method.java:566) 15:49:33 at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60) 15:49:33 at com.sun.proxy.$Proxy0.execute(Unknown Source) 15:49:33 at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189) 15:49:33 at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138) 15:49:33 at org.sonarsource.scanner.cli.Main.execute(Main.java:126) 15:49:33 at org.sonarsource.scanner.cli.Main.execute(Main.java:81) 15:49:33 at org.sonarsource.scanner.cli.Main.main(Main.java:62) 15:49:33 Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 15:49:33 at com.A.A.D.D.G(Unknown Source) 15:49:33 at com.A.A.B.A.I.A(Unknown Source) 15:49:33 ... 36 common frames omitted 15:49:33

So it seems sonarqube reporter needs to be reviewed completely and json reporter should be flexible enough to account sonar.Sources value. Can someone fix that?

For now, I am running json reporter, then using script to strip the workspace path, and remove backslashes in file paths(from json) and then the uploads works just fine.

Environment

mildm8nnered commented 1 year ago

Can you share any of your json or sonarqube reporter output files, suitably obfuscated?

AGandhiCraniUS commented 9 months ago

I also have the same issue. Did you find a fix for this?

reybriel commented 4 months ago

I'm having the same issue using the sonarqube reporter!

The problem seems to be related with the way that the JSON report is generated when using the sonarqube reporter. The complain of the stack trace is that it found an object container instead of an array. Checking the report generated by SwiftLint, the formatting is:

{
  "issues" : [
    {
      "engineId" : "SwiftLint",
      "primaryLocation" : {
        "filePath" : "MyProject\/Models\/JSON.swift",
        "message" : "Use `Self` to refer to the surrounding type name",
        "textRange" : {
          "startLine" : 29
        }
      },
      "ruleId" : "prefer_self_in_static_references",
      "severity" : "MINOR",
      "type" : "CODE_SMELL"
    }
  ]
}

I believe that by removing the object with the issues field and leaving only the array should solve the issue. But I haven't tested it yet. I'm probably going to write a script to do it and try to upload the SwiftLint report again. I can come back with the results I get later.

Since the reporters json and sonarqube produce very different json reports though, I expect this to not work at first. Maybe using the json with a script to strip the workspace path is the way to go for now.