How to set path based on cmake config in VS Code?

Hello

Is there a way in vscode settings to tell sonarlint to look for compile_commands.json inside cmake build directory?

I tried

"sonarlint.pathToCompileCommands": "${config:cmake.buildDirectory}/compile_commands.json"

But it does not work.
Am I doing something wrong? Or does it need to be enabled by sonarlint, similarly to how ${workspaceFolder} needed to be explicitely supported?
See How to set a relative path in vscode?

Hi,

Welcome to the community!

The docs should help.

 
Ann

The doc does not contain anything useful about my question.

Hello, thank you for your question, and sorry for the long delay.

This would need to be explicitly supported by SonarLint: VSCode’s out-of-the-box support for configuration variable references is only available in launch configurations and task definitions.

I’m flagging this as a potentially useful UX improvement (especially for C and C++ analysis).

Hi Benjamin

Thanks for reaching out.
It is currently not possible to define a custom variable in the pathToCompileCommands apart from the ${workspaceFolder} one.
Could you elaborate on why pointing the cmake build directory would help you? And why not use the ${workspaceFoler}?

When using a CMake project, the compile_commands.json is generally located at the root of the build directory.
So basically for all cmake projects "sonarlint.pathToCompileCommands": "${config:cmake.buildDirectory}/compile_commands.json" is always correct.

Also CMake projects are generally not built at the root of the source directory. Generally we have paths like these ones for the root of the build directory:

  • ${workspaceFoler}/build
  • ${workspaceFoler}/../build-${workspaceFolderBasename}
  • ${workspaceFoler}/../build-${workspaceFolderBasename}-${buildType}
  • ${workspaceFoler}/../builds/${workspaceFolderBasename}-${buildType}

So that means that cmake users almost always have to manually edit the value of pathToCompileCommands for each project they want to use sonarlint on.

1 Like

Thanks for your explanation. Your point is completely valid.
I will store your insight in our backlog to keep track of and monitor the traction. Note that this improvement will not be tackled in the short-term since we have other priorities.

Thanks again for sharing.

Best

Alexander

1 Like