skip to Main Content

I’m trying to configure my .NET 6 / 8 projects to run code analysis also on Debug => Start Debugging (F5) or Debug => Start Without Debugging (ctrl+F5) , but I can’t do that.

Writing something wrong in my code and than running the command Project => Build/Rebuild the build fails due to the errors shown in the Error List Panel on VS (or on command line) as I expect.
The same wrong lines doesn’t stop the Debug command.

In the .csproj I set

<AnalysisLevel>latest-recommended</AnalysisLevel>
<AnalysisMode>all</AnalysisMode>

Can anyone help me figure out the problem? Am I missing any configuration?

I’ve tried to find out a configuration or a specific command on learn.microsoft.com but without any suggestions.

edit:
Here some information in order to reproduce the problem

  • Visual Studio 2022 v. 17.9.6
  • New Project Console application .NET 8
  • Install nuget package StyleCop.Analyzers 1.2.0-beta.556
  • new local file .editorconfig with the following configuration
root = true
[*.{cs}]
dotnet_diagnostic.SA1507.severity = error # Code should not contain multiple blank lines in a row
  • in program.cs add two lines above Console.WriteLine("Hello, World!");

2

Answers


  1. Check

    1. Options
    2. Projects and Solutions Build And Run
    3. "On Run, when build or deployment errors occur"

    If this is set to "Launch old version" you might get the problems you describe. If it is set to the default "Prompt to launch" I would have expected build errors, followed by a dialog asking if you want to run the last successful build.

    Login or Signup to reply.
  2. It seems that it’s necessary to enable "Treat warnings as error" for the project, either by setting it in the project settings or adding it via <TreatWarningsAsErrors>True</TreatWarningsAsErrors> to the csproj.

    Otherwise the build fails, but the debugger can still be started. I assume the debugger only cares about actual compiler errors, and not errors emitted by analyzers otherwise. This will fail the build on all warnings however, even those only set to warning in .editorconfig.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search