On Windows 11 using Visual Studio 2022 16.11.34 with a CMake project, SonarLint is not able to scan source files. The same project opened in VSCode can be scanned just fine. We are not using SonarQube.
The latest SonarLint plugin was installed, SonarLint.VSIX-7.7.0.86423-2022.
Investigating, I found there seem to be three issues (one of which is a Visual Studio issue):
First, our CMake configuration specifies the build output to be in a build
folder. Thus, the path to the compile_commands.json
is, for example, build\Windows-Debug\compile_commands.json
. SonarLint seems to expect an out
prefix for this folder, i.e. out\build\Windows-Debug\compile_commands.json
, and there is no apparent way to override this.
Second, in our CMakePresets.json, we use displayName
attributes for the presets, which differ slightly from the name. SonarLint uses the display name for the path (“Windows Debug”) instead of the name (“Windows-Debug”). Specifically, the configuration looks like this:
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 24,
"patch": 0
},
"configurePresets": [
{
"name": "Windows-Base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "Windows-Release",
"displayName": "Windows Release",
"description": "Build for Windows, release",
"inherits": "Windows-Base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "Windows-Base",
"hidden": true,
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "Windows-Release",
"displayName": "Windows Release",
"description": "Build for Windows, release",
"inherits": "Windows-Base",
"configurePreset": "Windows-Release"
}
]
}
The Visual Studio issue, by the way, is that the CMake distributed with it, cmake version 3.28.0-msvc1, erroneously forces C++ 20 module support on (i.e. it always uses .modmap
files to compile), which of course SonarLint doesn’t support. The public CMake 3.28.3 does not have this issue. If the above two issues are fixed, I’d need to figure out how to tell Visual Studio to use the installed CMake and not its own.