IDE name and flavor/env: VSCode + cmake-tools plugin
SonarLint plugin version: 3.4.1
Is connected mode used: Irrelevant
People use CMake to build C++ projects for different architectures/oses/modes. This results in different paths for the compile_command.json file.
SonarLint should take this into account and switch the referenced file based on the used CMake Kit or Preset used by cmake-tools.
I am not a user of cmake-tools so I want to understand your need first.
I gave a quick try and it appears to me that if you change the kit or variant, the compile_commands.json file is modified in place. In your use case do you have multiple compile_commands.json files in different directories ? Could you explain how you achieve that ?
You can (and you should to avoid rebuilds!) specify different outputs directories. I can provide you a basic example if you want. Do you prefer Linux or Windows?
Hint: See binaryDir
PS: I think you should always prefer presets over Kits/Variants, because presets is supported by the CMake developers and the other thing was a solution by the plugin, before presets were implemented.
As we handled this feature request on Commercial Support, I was due to update this post to let the Community knows about the outcome, but I forgot back in May, so here I do now!
We discussed the use case internally and tracked it in our insights tool. It is a corner case for now, but interesting nonetheless. Indeed the most realistic solution appears to be able to evaluate the ${command:cmake.buildDirectory} in the path provided into sonarlint.pathToCompileCommands. Whatever CMake extension is used will populate this variable and SonarLint will read it. This is only limited to extensions that take care of ${command:cmake.buildDirectory} of course but it’s already an improvement.
This is not a priority for now but we have it recorded to track further similar requests.