运行 sonarqube 分析我的代码时获取 NullPointerException

Get NullPointerException while running sonarqube analysis of my code

我有一项 Jenkins 工作,用于将单元测试覆盖率(lcov 格式)发布到 SonarQube 服务器,如 及其答案中所述。有时,我在 Jenkins 控制台日志的 node_modules 目录中看到 JavaScript 文件的以下异常。

16:57:15.037 ERROR - Problem while calculating coverage for /var/lib/jenkins/jobs/jenkins-project-name/workspace/project-name/node_modules/js-tokens/test/fixtures/regex.js
java.lang.NullPointerException: null
at org.sonar.plugins.javascript.lcov.UTCoverageSensor.saveZeroValueForResource(UTCoverageSensor.java:146) [sonar-javascript-plugin-2.9.jar:na]
at org.sonar.plugins.javascript.lcov.UTCoverageSensor.saveMeasureFromLCOVFile(UTCoverageSensor.java:128) [sonar-javascript-plugin-2.9.jar:na]
at org.sonar.plugins.javascript.lcov.UTCoverageSensor.analyse(UTCoverageSensor.java:85) [sonar-javascript-plugin-2.9.jar:na]
at org.sonar.batch.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:58) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.phases.SensorsExecutor.execute(SensorsExecutor.java:50) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.phases.PhaseExecutor.execute(PhaseExecutor.java:98) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:192) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:100) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:85) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.scan.ProjectScanContainer.scan(ProjectScanContainer.java:258) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:253) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:243) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:100) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:85) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.bootstrap.GlobalContainer.executeAnalysis(GlobalContainer.java:153) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:110) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:76) [sonar-batch-shaded-5.2.jar:na]
at org.sonar.runner.batch.IsolatedLauncher.execute(IsolatedLauncher.java:48) [sonar-runner-batch8900787800742033772.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79]
at org.sonar.runner.impl.BatchLauncher.delegateExecution(BatchLauncher.java:87) [sonar-runner-dist-2.4.jar:na]
at org.sonar.runner.impl.BatchLauncher.run(BatchLauncher.java:75) [sonar-runner-dist-2.4.jar:na]
at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_79]
at org.sonar.runner.impl.BatchLauncher.doExecute(BatchLauncher.java:69) [sonar-runner-dist-2.4.jar:na]
at org.sonar.runner.impl.BatchLauncher.execute(BatchLauncher.java:50) [sonar-runner-dist-2.4.jar:na]
at org.sonar.runner.api.EmbeddedRunner.doExecute(EmbeddedRunner.java:102) [sonar-runner-dist-2.4.jar:na]
at org.sonar.runner.api.Runner.execute(Runner.java:100) [sonar-runner-dist-2.4.jar:na]
at org.sonar.runner.Main.executeTask(Main.java:70) [sonar-runner-dist-2.4.jar:na]
at org.sonar.runner.Main.execute(Main.java:59) [sonar-runner-dist-2.4.jar:na]
at org.sonar.runner.Main.main(Main.java:53) [sonar-runner-dist-2.4.jar:na]

我将 node_modules 目录添加到排除列表中:

sonar.exclusions=./project-name/node_modules/**/*
sonar.test.exclusions=./project-name/node_modules/**/*

但我有时还是会报错。知道出了什么问题吗?

您的情况似乎与 known issue 类似,将在 JavaScript 插件的 2.10 版中修复。这个版本应该会在未来几天发布。

作为解决方法,像您一样设置排除项应该可行。如果你仍然得到同样的错误,我想你提供的路径没有被 SonarQube 正确解析:你可以尝试删除开头的 ./