Issues with SonarLint, Visual Studio 2022 16.11.34, C++, CMake

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.

Hey @grmcdorman! Welcome to the community.

Unfortunately, we currently don’t support the CMakePresets.json settings format and only rely on the CMakeSettings.json for the analysis configuration. Because SonarLint can’t locate it, it defaults to using the out\build\... as the location.

From what I could gather about this issue in the documentation on CMake support in VisualStudio, it seems like the CMakePresets.json is now the preferred format. We will discuss the possibility of adding support for it with our PMs.

The current workaround would be to add a dummy CMakeSettings.json to provide a hint to SonarLint for where your compilation database is. CMakePresets.json would still take precedence for other IDE functions.

Thanks Georgii. I will look at implementing this workaround when I have time, currently we’ve been pulled into a high-priority bug fix.