Node.JS sonarcube-scanner doesn't hornor SONAR_USER_HOME set in environment

I am using sonarqube-scanner npm module from https://www.npmjs.com/package/sonarqube-scanner. I have the following groovy script which creates ~/.sonar/cache folder, sets the permission and export SONAR_USER_HOME but when node analyse.js is run, it crashes with “java.lang.IllegalStateException: Unable to create user cache: /.sonar/cache”

def call() {
	sh "mkdir -p ~/.sonar/cache"
	sh "chmod 744 ~/.sonar/cache"
	sh "export SONAR_USER_HOME=~/.sonar"
	sh "echo $SONAR_USER_HOME"
	sh "SONAR_USER_HOME=~/.sonar && node analyse.js"
	return this
}

“sonarqube-scanner”: “^2.6.0”

Here is the log:

<snip>
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Coverage)
[Pipeline] sh
+ mkdir -p /home/jenkins/.sonar/cache
[Pipeline] sh
+ chmod 744 /home/jenkins/.sonar/cache
[Pipeline] sh
+ SONAR_USER_HOME=/home/jenkins/.sonar
+ node analyse.js
[03:46:30] Starting SonarQube analysis...
[03:46:30] Getting info from "package.json" file
[03:46:30] Checking if executable exists: /home/jenkins/.sonar/native-sonar-scanner/sonar-scanner-4.3.0.2102-linux/bin/sonar-scanner
[03:46:30] Could not find executable in "/home/jenkins/.sonar/native-sonar-scanner".
[03:46:30] Proceed with download of the platform binaries for SonarQube Scanner...
[03:46:30] Creating /home/jenkins/.sonar/native-sonar-scanner
[03:46:30] Downloading from https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.3.0.2102-linux.zip
[03:46:30] (executable will be saved in cache folder: /home/jenkins/.sonar/native-sonar-scanner)
INFO: Scanner configuration file: /home/jenkins/.sonar/native-sonar-scanner/sonar-scanner-4.3.0.2102-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.3.0.2102
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 4.18.0-147.3.1.el8_1.x86_64 amd64
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 0.295s
INFO: Final Memory: 3M/235M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarScanner execution
java.lang.IllegalStateException: Unable to create user cache: /.sonar/cache
	at org.sonarsource.scanner.api.internal.cache.FileCache.createDir(FileCache.java:147)
	at org.sonarsource.scanner.api.internal.cache.FileCache.<init>(FileCache.java:46)
	at org.sonarsource.scanner.api.internal.cache.FileCache.create(FileCache.java:52)
	at org.sonarsource.scanner.api.internal.cache.FileCacheBuilder.build(FileCacheBuilder.java:48)
	at org.sonarsource.scanner.api.internal.JarDownloaderFactory.create(JarDownloaderFactory.java:42)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.createLauncher(IsolatedLauncherFactory.java:68)
	at org.sonarsource.scanner.api.EmbeddedScanner.doStart(EmbeddedScanner.java:185)
	at org.sonarsource.scanner.api.EmbeddedScanner.start(EmbeddedScanner.java:123)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:73)
	at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: java.nio.file.AccessDeniedException: /.sonar
	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.createDirectory(Unknown Source)
	at java.base/java.nio.file.Files.createDirectory(Unknown Source)
	at java.base/java.nio.file.Files.createAndCheckIsDirectory(Unknown Source)
	at java.base/java.nio.file.Files.createDirectories(Unknown Source)
	at org.sonarsource.scanner.api.internal.cache.FileCache.createDir(FileCache.java:145)
	... 9 more
ERROR: 
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.

Any advice and insight is appreciated.

OK. This is fixed. The env var has to be set the groovy’s way, not BASH’s way.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.