sonarqubesonarscanner

sonar-scanner error java.lang.IllegalStateException: Failed to index files, caused by java.nio.file.AccessDeniedException


(self-answering for future me as I didn't find this particular crash on SO and it took me couple of minutes to figure it out)

When launching sonarqube scanner locally from CLI, it does exit very early during "INFO Indexing files..." step, crashing with IllegalStateException:

12:50:32.226 ERROR Error during SonarScanner CLI execution
java.lang.IllegalStateException: Failed to index files
        at org.sonar.scanner.scan.filesystem.ProjectFileIndexer.indexFiles(ProjectFileIndexer.java:214)
        at org.sonar.scanner.scan.filesystem.ProjectFileIndexer.index(ProjectFileIndexer.java:169)
        at org.sonar.scanner.scan.filesystem.ProjectFileIndexer.indexModulesRecursively(ProjectFileIndexer.java:148)
        at org.sonar.scanner.scan.filesystem.ProjectFileIndexer.index(ProjectFileIndexer.java:115)
        at org.sonar.scanner.scan.SpringProjectScanContainer.doAfterStart(SpringProjectScanContainer.java:363)
        at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
        at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
        at org.sonar.scanner.bootstrap.SpringGlobalContainer.doAfterStart(SpringGlobalContainer.java:137)
        at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
        at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
        at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72)
        at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66)
        at org.sonarsource.scanner.lib.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:41)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at org.sonarsource.scanner.lib.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:62)
        at jdk.proxy3/jdk.proxy3.$Proxy2.execute(Unknown Source)
        at org.sonarsource.scanner.lib.InProcessScannerEngineFacade.doAnalyze(InProcessScannerEngineFacade.java:39)
        at org.sonarsource.scanner.lib.ScannerEngineFacade.analyze(ScannerEngineFacade.java:61)
        at org.sonarsource.scanner.cli.Main.analyze(Main.java:77)
        at org.sonarsource.scanner.cli.Main.main(Main.java:63)
Caused by: java.nio.file.AccessDeniedException: /home/user/project/bin_LinuxUbuntu_x64_Debug/api
        at java.base/sun.nio.fs.UnixException.translateToIOException(Unknown Source)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(Unknown Source)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(Unknown Source)
        at java.base/sun.nio.fs.UnixFileSystemProvider.newDirectoryStream(Unknown Source)
        at java.base/java.nio.file.Files.newDirectoryStream(Unknown Source)
        at java.base/java.nio.file.FileTreeWalker.visit(Unknown Source)
        at java.base/java.nio.file.FileTreeWalker.next(Unknown Source)
        at java.base/java.nio.file.Files.walkFileTree(Unknown Source)
        at org.sonar.scanner.scan.filesystem.ProjectFileIndexer.indexDirectory(ProjectFileIndexer.java:221)
        at org.sonar.scanner.scan.filesystem.ProjectFileIndexer.indexFiles(ProjectFileIndexer.java:207)
        ... 22 common frames omitted

SonarQube server is "Data Center EditionVersion 9.9.4 (build 87374)", sonnar-scanner CLI is "SonarScanner CLI 6.1.0.4477", running with JRE "Java 17.0.11 Eclipse Adoptium (64-bit)" on Linux (Ubuntu 20.04 LTS).

First step using build-wrapper version "build-wrapper, version 6.41.1 (linux-x86)" did pass successfully, creating the build-wrapper-dump.json file with valid content, but second step with sonar-scanner fails early.


Solution

  • The scanner is looking through all sub-directories of the project, including ones which are NOT involved in the build process (neither as source or build work directory), like in this particular case scanner is trying to index files in .../project/bin_LinuxUbuntu_x64_Debug dir, which has remnants of previous sudo make install operation, including sub-directories with root:root owner and 700 permission, which are not accessible to current user running the scanner.

    Because it can't access the directory content, it fails the initial file indexing before starting the scan itself.

    After removing all inaccessible files (deleting them with rm -rf bin_LinuxUbuntu_x64_Debug/ in this case as all files were generated, you may want to move/archive files which you don't want to lose or change their permissions to make them accessible during indexing) the scanner does run whole scan successfully.