Sonarlint Configure Project Type in VS2022

Hey guys,
I have been using SonarLint in VS for quite some time now, and I really like the suggestions to improve code quality.

It all works well if you open a .sln file for a C/C++ project.

However in VS2022 Microsoft started supporting CMakePresets.
To use CMakePresets in VS you have to open the project as a folder. However when opening the project as a folder Sonar Lint does not seam to detect the language automatically

I’m getting

[CMake] Could not locate compilation database at 'C:\...\out\build\Windows Ninja Debug\compile_commands.json'. Make sure that your project is configured correctly. 
    See https://github.com/SonarSource/sonarlint-visualstudio/wiki for more information.

Unable to retrieve the configuration for file 'C:\...\app\Testclient.cpp'.
    Check the file is part of a supported project type in the current solution.

How can I set the project type manually?

  • Operating system: Windows 10
  • IDE name and flavor/env: VS 2022
  • SonarLint plugin version: 5.5.0.43817 (newest at the time of writing)
  • Is connected mode used:
    • Connected to SonarCloud or SonarQube (and which version):

Hi Julian,

Unfortunately we do not support CMakePresets at the moment. It is a known limitation and we have a ticket at our backlog about it.
For setting the project up manually you can refer to our documentation.
Hope these are enough, please do not hesitate to follow up if you need further assistance.

1 Like

Hey Ugras,
thank you for your quick feedback.
I added set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
and the compile_commands.json is properly generated within the build folder.
However in our porject we changed the build folder
Our build dir is located under

{
      "name": "_builddir",
      "hidden": true,
      "binaryDir": "${sourceParentDir}/build/${presetName}",
      "installDir": "${sourceParentDir}/build/install/${presetName}"
},

Can I change the location where Sonarlint searches for the compile_commands.json?

Hi Julian,

You’ll need to provide a buildroot property that either points to an environment variable, or uses macros which are supported. $(sourceParentDir) and ${presetName} are not supported.

You could provide environment variable like this.

"buildRoot" : "$(env.compilationDbLocation)"

Macros we currently support are these

  • workspaceRoot
  • workspaceHash
  • projectHash
  • projectFile
  • projectDir
  • projectDirName
  • thisFile
  • thisFileDir
  • name
  • generator

Hey Ugras,
thank you for your answer

I added the buildRoot in the AppData/Romaing/…/settings.json

{
  "sonarlint.rules": {},
  "buildRoot": "C:\\git\\xxx\\build\\windows_ninja_debug"
}

but this did not work.
Is there a way to set this property per project and not globally like in the AppData/Romaing folder?

Hi Julian,

You should’ve added this to CMakeSettings.json . But because you’re using CMakePresets you do not have this file.

A workaround would be adding the “CMakeSettings.json” for just SonarLint and maintaining it alongside with the “CMakePresets.json”.

According to Microsoft Docs if both CMakePresets.json will take precedence for configuring the build. The dummy CMakeSettings.json is just there to allow SonarLint to find the compilation database. (see table below)

Hey Ugras,
thank you for your quick support. Adding a CMakeSettings.json just for sonarlint worked nicely. Although It would make things easier when Sonarlint supports presets :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.