Sam Leach
Sam Leach

Reputation: 12956

How to fail .NET 4.5 build with Microsoft.CodeAnalysis.NetAnalyzers?

I am attempting to use Microsoft.CodeAnalysis.NetAnalyzers in a legacy .NET 4.5 app that cannot be upgraded for reasons.

According to the documentation you can install the Microsoft.CodeAnalysis.NetAnalyzers NuGet package and use the Analysers in 'older' .net versions. How old, I am not sure.

If you don't want to move to the .NET 5+ SDK, have a non-SDK-style .NET Framework project, or prefer a NuGet package-based model, the analyzers are also available in the Microsoft.CodeAnalysis.NetAnalyzers NuGet package.

The Errors do show in the Visual Studio Error List under "Build + IntelliSense" but do not show under the regular "Build" so subsequently the build does not fail even if there are errors configured in the .editorconfig

I imagine that the Visual Studio IntelliSense is using the .NET 5 SDK to show the errors and then the actual build is using .NET 4.5 so is unaware of the analysis rules? But then what is the NuGet package supposed to be doing?

Is this just not supported in such an old .NET Framework version? That quote from the documentation suggests that some older frameworks should be able to use it.

I have a blank, file new project .NET 4.5 Console app with Microsoft.CodeAnalysis.NetAnalyzers installed and a simple .editorconfig with a sample rule

dotnet_diagnostic.IDE0010.severity = error

If so, what are alternatives? StyleCop "Classic"?

Upvotes: 1

Views: 910

Answers (1)

Lukasz Szczygielek
Lukasz Szczygielek

Reputation: 3110

As I know old code analysis uses ruleset file, not .editorconfig. I found also documentation about it. I use code analysis in .NET Framework projects like this:

  • right-click on the project and go to Properties,
  • go to Code Analysis tab,
  • in active rules there is by default Microsoft Managed Recommended Rules - click on Configure button,
  • search for IDE0010 and select error and save ruleset,
  • ensure that now in Code Analysis tab you have your file selected in active rules option.

Upvotes: 1

Related Questions