SonarLint for apex (SFDC) on VS Code

I planned to to use SonarLint for apex language on VS Code and installed the SonarLint extension, gone through basic rules. SonarLint not reporting unused variables on Apex as well JavaScript .js on Lightning Web Component(SFDC).
I don’t see much effective for SFDC development, can someone please assist how does it helpful for SFDC apex, JavaScript on VS Code. I see SonarLint effectively working for Eclipse Java.

Hello,

Can you provide a reproducer of the case not detected on .js files for Lightning Web Components?
The JS part of Lightning Web Components is not different from vanilla JS code so SonarLint should be able to detect issues on them.

Thanks
Alex

LWC - Javascript Screen

Apex Screen:

Both the files saved successfully without any SonarLint notification for unused variables.

Hi,

Can you look at the language that VSCode “detect” for those files? This should be visible on the bottom right part. In my example this is TypeScript:


If this is not regular JavaScript language, we would need the id of the language to map it correctly in SonarLint. To find the VSCode language id, just click on the language in the status bar, and it should show you the list of languages with their id between parenthesis:
image

Yes, the language name is ‘DX Code Companion’ at the bottom.

I don’t think so. The language in your screenshot is HTML and this should already be supported by SonarLint.

What you could do in addition to clarify languages of the files you want to analyze is to enable verbose logs and look at the SonarLint output.

If i am on .js file the language displaying as ‘JavaScript’

If i am on apex class the language displaying name as ‘Apex’

Still SonarLint not noticing for unused variables.

The languages look good and this is already what we support. We need the verbose logs to investigate then.

Please find the verbose logs.

[Info  - 14:58:52.518] Started security hotspot handler on port 64120

Error on git command ""C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/fBE_CPQ_QuoteHeader/fBE_CPQ_QuoteHeader.html": Error: Command failed: "C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/fBE_CPQ_QuoteHeader/fBE_CPQ_QuoteHeader.html

fatal: not a git repository (or any of the parent directories): .git

[Info  - 14:58:57.882] Analyzing file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/fBE_CPQ_QuoteHeader/fBE_CPQ_QuoteHeader.html'...

[Warn  - 14:58:57.966] Unable to query node version

[Info  - 14:59:03.702] Creating container for module with key=file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021

[Info  - 14:59:04.465] Found 0 issues

Error on git command ""C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/FBE_CPQ_QuoteHeader/FBE_CPQ_QuoteHeader.js": Error: Command failed: "C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/FBE_CPQ_QuoteHeader/FBE_CPQ_QuoteHeader.js

fatal: not a git repository (or any of the parent directories): .git

[Info  - 15:02:03.833] Analyzing file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/FBE_CPQ_QuoteHeader/FBE_CPQ_QuoteHeader.js'...

[Info  - 15:02:03.952] Found 0 issues

Error on git command ""C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/fBE_CPQ_OpptyQuoteRelatedList/fBE_CPQ_OpptyQuoteRelatedList.html": Error: Command failed: "C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/fBE_CPQ_OpptyQuoteRelatedList/fBE_CPQ_OpptyQuoteRelatedList.html

fatal: not a git repository (or any of the parent directories): .git

[Info  - 15:02:04.617] Analyzing file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/fBE_CPQ_OpptyQuoteRelatedList/fBE_CPQ_OpptyQuoteRelatedList.html'...

[Info  - 15:02:04.707] Found 0 issues

Error on git command ""C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/jsconfig.json": Error: Command failed: "C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/jsconfig.json

fatal: not a git repository (or any of the parent directories): .git

[Info  - 15:02:07.243] Analyzing file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/jsconfig.json'...

[Info  - 15:02:07.286] Found 0 issues

Error on git command ""C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/testSonar/testSonar.html": Error: Command failed: "C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/testSonar/testSonar.html

fatal: not a git repository (or any of the parent directories): .git

[Info  - 15:37:55.478] Analyzing file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/testSonar/testSonar.html'...

Error on git command ""C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/testSonar/testSonar.js": Error: Command failed: "C:\Program Files\Git\cmd\git.exe" check-ignore force-app/main/default/lwc/testSonar/testSonar.js

fatal: not a git repository (or any of the parent directories): .git

[Info  - 15:37:56.751] Analyzing file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/testSonar/testSonar.js'...

[Info  - 15:37:56.803] Found 0 issues

[Debug - 15:39:13.961] Queuing analysis of file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/testSonar/testSonar.js'

[Info  - 15:39:13.963] Analyzing file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/testSonar/testSonar.js'...

[Debug - 15:39:13.963] Analysis triggered on 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/testSonar/testSonar.js' with configuration:

[

  baseDir: c:\Users\sireesha_myla\Desktop\Feb Dev 2021

  extraProperties: {}

  moduleKey: file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021

  excludedRules: [Web:TableWithoutCaptionCheck]

  includedRules: []

  ruleParameters: {}

  inputFiles: [

    file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/testSonar/testSonar.js (UTF-8) [js]

  ]

]

[Debug - 15:39:13.981] Start analysis

[Info  - 15:39:13.987] Index files

[Debug - 15:39:13.988] Language of file 'file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/lwc/testSonar/testSonar.js' is set to 'JavaScript'

[Info  - 15:39:13.989] 1 file indexed

[Debug - 15:39:14.031] 'JavaSensor' skipped because there is no related file in current project

[Debug - 15:39:14.031] 'Python Sensor' skipped because there is no related file in current project

[Debug - 15:39:14.033] Execute Sensor: JavaXmlSensor

[Debug - 15:39:14.033] Execute Sensor: HTML

[Debug - 15:39:14.034] 'PHP sensor' skipped because there is no related file in current project

[Debug - 15:39:14.044] 'Analyzer for "php.ini" files' skipped because there is no related file in current project

[Debug - 15:39:14.053] Execute Sensor: Sonar Secrets Detection Sensor

[Info  - 15:39:14.461] Found 0 issues

Hello,

There is an interesting trace:

[Warn - 14:58:57.966] Unable to query node version

I think you activated debug traces after this trace occurred because some other traces should be displayed prior to this one. Could you try to restart the IDE, check again and send us the logs here ?

SonarLint is trying to get the Node.js version installed on your box and it seems there is a problem at this point. Could it be possible that there is a node executable in your path that is not Node.js ? Could you execute the following command in your terminal and send us the result:

node -v

Thanks

I have executed node -v and the below is the screen.

I have done some configurations changes by updating Sonarqube token in settings.json of VS code and now SonarLint supporting as expected in VS Code for JavaScript but not apex.

Logs for apex :

Executing C:\Program Files\Java\jdk-17.0.1\bin\java -jar c:\Users\sireesha_myla.vscode\extensions\sonarsource.sonarlint-vscode-3.0.0\server\sonarlint-ls.jar 50272 -analyzers file:///c:/Users/sireesha_myla/.vscode/extensions/sonarsource.sonarlint-vscode-3.0.0/analyzers/sonarjava.jar file:///c:/Users/sireesha_myla/.vscode/extensions/sonarsource.sonarlint-vscode-3.0.0/analyzers/sonarjs.jar file:///c:/Users/sireesha_myla/.vscode/extensions/sonarsource.sonarlint-vscode-3.0.0/analyzers/sonarphp.jar file:///c:/Users/sireesha_myla/.vscode/extensions/sonarsource.sonarlint-vscode-3.0.0/analyzers/sonarpython.jar file:///c:/Users/sireesha_myla/.vscode/extensions/sonarsource.sonarlint-vscode-3.0.0/analyzers/sonarhtml.jar -extraAnalyzers file:///c:/Users/sireesha_myla/.vscode/extensions/sonarsource.sonarlint-vscode-3.0.0/analyzers/sonarsecrets.jar

[stdout] Binding to 50272

Child process connected on port 50272

[stderr] SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.

[stderr] SLF4J: Defaulting to no-operation (NOP) logger implementation

[stderr] SLF4J: See SLF4J Error Codes for further details.

[Info - 14:48:06.278] Started security hotspot handler on port 64120

[Error - 14:48:07.697] No SonarQube/SonarCloud connections defined for your binding. Please update your settings.

[Error - 14:48:08.302] No SonarQube/SonarCloud connections defined for your binding. Please update your settings.

[Debug - 14:48:08.559] Default settings updated: WorkspaceFolderSettings[analyzerProperties={},connectionId=,projectKey=SonarVscode,testFilePattern=]

Error on git command ““C:\Program Files\Git\cmd\git.exe” check-ignore force-app/main/default/classes/sonarLintTest.cls”: Error: Command failed: “C:\Program Files\Git\cmd\git.exe” check-ignore force-app/main/default/classes/sonarLintTest.cls

fatal: not a git repository (or any of the parent directories): .git

[Debug - 14:48:08.880] Cached SCM ignore status for file ‘file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls’

[Info - 14:48:08.885] Analyzing file ‘file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls’…

[Debug - 14:48:08.899] Analysis triggered on ‘file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls’ with configuration:

[

baseDir: c:\Users\sireesha_myla\Desktop\Feb Dev 2021

extraProperties: {}

moduleKey: file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021

excludedRules: [Web:TableWithoutCaptionCheck]

includedRules:

ruleParameters: {}

inputFiles: [

file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls (UTF-8) [apex]

]

]

[Debug - 14:48:08.899] Starting standalone SonarLint engine…

[Debug - 14:48:08.901] Using 5 analyzers

[Debug - 14:48:08.912] Node.js path provided by configuration: C:\Program Files\NodeJS\12-lts\bin\node.exe

[Debug - 14:48:08.912] Checking node version…

[Debug - 14:48:08.928] Execute command ‘C:\Program Files\NodeJS\12-lts\bin\node.exe -v’…

[Debug - 14:48:08.947] Unable to execute the command

[Debug - 14:48:08.953] org.sonar.api.utils.command.CommandException: java.io.IOException: Cannot run program “C:\Program Files\NodeJS\12-lts\bin\node.exe”: CreateProcess error=2, The system cannot find the file specified

at org.sonar.api.utils.command.CommandExecutor.execute(CommandExecutor.java:102)

at org.sonarsource.sonarlint.core.NodeJsHelper.runSimpleCommand(NodeJsHelper.java:144)

at org.sonarsource.sonarlint.core.NodeJsHelper.detect(NodeJsHelper.java:66)

at org.sonarsource.sonarlint.ls.NodeJsRuntime.init(NodeJsRuntime.java:53)

at org.sonarsource.sonarlint.ls.NodeJsRuntime.getNodeJsPath(NodeJsRuntime.java:71)

at org.sonarsource.sonarlint.ls.EnginesFactory.createStandaloneEngine(EnginesFactory.java:87)

at org.sonarsource.sonarlint.ls.standalone.StandaloneEngineManager.getOrCreateStandaloneEngine(StandaloneEngineManager.java:37)

at org.sonarsource.sonarlint.ls.AnalysisManager.analyzeStandalone(AnalysisManager.java:486)

at org.sonarsource.sonarlint.ls.AnalysisManager.analyze(AnalysisManager.java:355)

at org.sonarsource.sonarlint.ls.AnalysisManager.lambda$analyzeAsync$4(AnalysisManager.java:299)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)

at java.base/java.lang.Thread.run(Thread.java:833)

Caused by: java.io.IOException: Cannot run program “C:\Program Files\NodeJS\12-lts\bin\node.exe”: CreateProcess error=2, The system cannot find the file specified

at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)

at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)

at org.sonar.api.utils.command.CommandExecutor.execute(CommandExecutor.java:74)

... 12 more

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified

at java.base/java.lang.ProcessImpl.create(Native Method)

at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:494)

at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:159)

at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)

... 14 more

[Warn - 14:48:08.957] Unable to query node version

[Debug - 14:48:09.223] Create : C:\Users\sireesha_myla.sonarlint\plugins

[Debug - 14:48:09.226] Plugin cache: C:\Users\sireesha_myla.sonarlint\plugins

[Debug - 14:48:09.227] Create : C:\Users\sireesha_myla.sonarlint\plugins_tmp

[Debug - 14:48:09.718] Load plugins

[Debug - 14:48:09.790] Plugin ‘JavaScript/TypeScript Code Quality and Security’ requires Node.js 10.12.0. Skip loading it.

[Debug - 14:48:09.859] Load plugins (done) | time=141ms

[Debug - 14:48:10.066] Plugins:

[Debug - 14:48:10.067] * Python Code Quality and Security 3.6.0.8488 (python)

[Debug - 14:48:10.071] * Java Code Quality and Security 7.3.0.27589 (java)

[Debug - 14:48:10.074] * HTML Code Quality and Security 3.4.0.2754 (web)

[Debug - 14:48:10.077] * PHP Code Quality and Security 3.20.0.8080 (php)

[Debug - 14:48:10.077] * Sonar Secrets Plugin for SonarQube 1.1.0.36766 (secrets)

[Info - 14:48:13.956] Creating container for module with key=file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021

[Debug - 14:48:13.967] Standalone SonarLint engine started

[Debug - 14:48:14.058] Start analysis

[Info - 14:48:14.089] Index files

[Debug - 14:48:14.095] Language of file ‘file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls’ is set to ‘Apex’

[Info - 14:48:14.107] 1 file indexed

[Debug - 14:48:14.717] ‘JavaSensor’ skipped because there is no related file in current project

[Debug - 14:48:14.720] ‘Python Sensor’ skipped because there is no related file in current project

[Debug - 14:48:14.722] Execute Sensor: JavaXmlSensor

[Debug - 14:48:14.727] Execute Sensor: HTML

[Debug - 14:48:14.732] ‘PHP sensor’ skipped because there is no related file in current project

[Debug - 14:48:14.733] ‘Analyzer for “php.ini” files’ skipped because there is no related file in current project

[Debug - 14:48:14.735] Execute Sensor: Sonar Secrets Detection Sensor

[Info - 14:48:14.807] Found 0 issues

[Debug - 14:48:15.799] Checking binding updates

[Debug - 14:48:29.877] Queuing analysis of file ‘file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls’

[Info - 14:48:29.879] Analyzing file ‘file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls’…

[Debug - 14:48:29.882] Analysis triggered on ‘file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls’ with configuration:

[

baseDir: c:\Users\sireesha_myla\Desktop\Feb Dev 2021

extraProperties: {}

moduleKey: file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021

excludedRules: [Web:TableWithoutCaptionCheck]

includedRules:

ruleParameters: {}

inputFiles: [

file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls (UTF-8) [apex]

]

]

[Debug - 14:48:29.951] Start analysis

[Info - 14:48:29.974] Index files

[Debug - 14:48:29.975] Language of file ‘file:///c:/Users/sireesha_myla/Desktop/Feb%20Dev%202021/force-app/main/default/classes/sonarLintTest.cls’ is set to ‘Apex’

[Info - 14:48:29.978] 1 file indexed

[Debug - 14:48:30.077] ‘JavaSensor’ skipped because there is no related file in current project

[Debug - 14:48:30.082] ‘Python Sensor’ skipped because there is no related file in current project

[Debug - 14:48:30.090] Execute Sensor: JavaXmlSensor

[Debug - 14:48:30.093] Execute Sensor: HTML

[Debug - 14:48:30.104] ‘PHP sensor’ skipped because there is no related file in current project

[Debug - 14:48:30.124] ‘Analyzer for “php.ini” files’ skipped because there is no related file in current project

[Debug - 14:48:30.129] Execute Sensor: Sonar Secrets Detection Sensor

[Info - 14:48:30.270] Found 0 issues

Thanks for this detailed report. There are several traces that look strange:

Node.js path provided by configuration: C:\Program Files\NodeJS\12-lts\bin\node.exe
Cannot run program “C:\Program Files\NodeJS\12-lts\bin\node.exe”: CreateProcess error=2, The system cannot find the file specified

So it seems that you configured the path to node in your workspace settings but are you sure this file exists on disk ?

No SonarQube/SonarCloud connections defined for your binding. Please update your settings.

Your configuration seems to be wrong. I encourage you to read this page to understand how to configure a connection. Also just to be explicit (from the same link):

The support for Apex analysis is only available together with SonarQube Enterprise Edition or SonarCloud.

If it’s still not working, I invite you to share your configuration files, e.g. your user config file and your workspace config file.