Sonarqube for IDE [Visual Studio] issues/questions

  • Operating system: Windows 11 Enterprise 23H2
  • Visual Studio version: 2022 Patch 17.14.21
  • SonarQube for Visual Studio plugin version: 9.0.0.15771
  • Programming language you’re coding in: C#
  • Is connected mode used: yes
    • SonarQube Cloud, SonarQube Server, or SonarQube Community Build? (if one of the latter two, which version?): SonarQube Server v2025.5

And a thorough description of the problem / question:

We have observed

  • that now SonarQube findings in Visual Studio are now only marked as warnings and not anymore as errors, if you have TreatWarningsAsErrors enabled.
  • that SonarQube issues are not propagated to the compiler anymore, thus a pragma warning disable in the code will be removed by Visual Studio Code Cleanup extension to remove unnecessary code

So our question is now, how can we enable the TreatWarningsAsErrors feature again for SonarQube issues to break the build also in Visual Studio (shift left approach)?

And how can we use pragma warning disable again in the code itself, to make it clear, which suppressions are done in a file for later removal/fixes. I saw that the new version now is working with a settings.json, but we would like to have the suppression in the code itself.

Greetings

Markus Kramer

2 Likes

Hi @Lutti1988

  1. At the moment we lost TreatWarningsAsErrors functionality with our recent 9.0 changes. Would you be more interested in having this back or having a separate customization for mapping from Sonar severities to VS’ Info/Warn/Error?
  2. It’s possible to manage issue statuses in Connected Mode by changing SonarQube Server resolution status (via browser or via in-IDE context menu by right clicking on the issue). Does this functionality solve the problem for you?

I’ve checked and actually the pragmas still work for me. Can you provide a more specific example of how it’s not working?

I used #pragma warning disable S3776 with the rule csharpsquid:S3776

Sorry I just realized I misread the second question. Can you link the extension you’re referring to?

Hello,

  1. Both would be good I guess, but for our current code base and amount of projects, it would at least be nice to have the TreatWarningsAsErrors functionality back

  2. We are using the built-in CodeCleanup on Save extension: https://visualstudiomagazine.com/articles/2022/01/11/code-cleanup-on-save.aspx
    there I did the following configuration:

Greetings

Markus Kramer

@georgii.borovinskikh Same for me, having this feature back is kind of important for a “shift left” approach. Otherwise the feedback will be returned during build time in CI/CD which is kind of late already!

I will investigate the problem further. In the meantime, you can use the SonarQube Report view with Focus on New Code filter in order to see newly introduced issues: New code | Visual Studio | Sonar Documentation

Unfortunately, there’s no easy way to integrate this with VS build system in a way that preserves the old behavior 1-to-1. We will have an internal discussion in order to come up with a solution.

2 Likes

Hi @georgii.borovinskikh ,

did you had time so far to have an internal discussion about a solution?

Greetings

Hi @Lutti1988

Sorry for not updating you sooner.

We’ve had a discussion and we agree with the concerns you raised.

We did an investigation into possible solutions, but none of them seemed straightforward enough for us to immediately jump into implementation. Unfortunately, we’ve not scheduled time to create a prototype yet.

A bit more details:

  1. We can detect when the project uses TreatWarningsAsErrors and we can mark the issues as errors in the Error List/Sonar Report View, however there’s no way to contribute those errors to the MSBuild and fail the build natively. We could raise our own ‘notification’ to the developer if we detect that the build has started and there were errors detected during the last analysis. Would that be useful to you?

  2. We can disable the ‘false-positive’ cleanup of #pragma warning disable via dotnet_remove_unnecessary_suppression_exclusions, but it will mean that those pragmas won’t be collected by the built-in cleanup anymore. We have not explored the possibility to have our own clean up mechanism, but it looks possible to do with Roslyn APIs. Would that be useful to you?

1 Like

Hi @georgii.borovinskikh ,

the combined solution of #1 and #2 could work. That would at least be useful to us, to see things in the code itself.

Greetings

Hi. Can you please try this build and provide feedback? Thanks

@Lutti1988 @DominikTouring Can you please try this build and provide feedback? Thanks

1 Like

Hi,

this (SLVS-2854 Fix: pragma-based warning suppressions get false-positively cleaned up by VS · SonarSource/sonarlint-visualstudio@0b92ed1 · GitHub) looks good. the pragmas don’t get cleaned up anymore, if they are needed and unnecessary ones get cleaned up.

Ty and greetings

while this ( SLVS-2854 Fix: pragma-based warning suppressions get false-positively cleaned up by VS · SonarSource/sonarlint-visualstudio@08bf28e · GitHub ) has the issue again

Hi. Are you sure that it’s the case?

The first link I’ve sent you had a bug: we didn’t link the configuration file to the project, so it shouldn’t have worked at all. The second link is the one that should fix that and disable the cleanup for our rules.

The cleanup of no longer necessary pragmas was not implemented (yet).

We’ve released version 9.8.0.16314 to VS marketplace yesterday, can you run the update of the extension via VS and try again?

Hi,

sorry I messed something up. I saw my visual studio auto updated the first version I installed to the latest one 9.8.0.16314 and while trying the second link and installing, something seems to went wrong and it looks like it went back to an older version.

however the new version https://github.com/SonarSource/sonarlint-visualstudio/tree/9.8.0.16314 of yesterday works with the disable of cleanup rules, that they don’t get removed.

Thank you

Does the TreatWarningsAsErrors functionality work for you? It was also added in the same release

I see sonar issues in the report view, but not as an error. Same for the “Error List” View. I see the issues there as “Message” for example for the S2094. At least as error or warning would be great, because we have the policy to have no warnings and errors, but mostly not look at the “Message” list (new language features are always in that list)

The mapping depends on your quality profile - sonarlint-visualstudio/src/IssueViz/Helpers/IAnalysisSeverityToVsSeverityConverter.cs at f9145509ead7abff8dd59cfafd09c64aa71f140d · SonarSource/sonarlint-visualstudio · GitHub.

Without the TreatWarningsAsErrors property you should see messages and warnings, with it you should see messages and errors. sonarlint-visualstudio/src/IssueViz/Helpers/IAnalysisSeverityToVsSeverityConverter.cs at f9145509ead7abff8dd59cfafd09c64aa71f140d · SonarSource/sonarlint-visualstudio · GitHub

Hi,

I have

image

and this is the result in Visual Studio 2022

The issue is not as a warning or error in the list for example.

Greetings