roslyn: IDE information diagnostics displayed in the VS Code problems pane do not surface via dotnet build

Follow on from discussion here: https://github.com/dotnet/roslyn/issues/6195#issuecomment-1090684677

6.0 (going by the SDK):

Steps to Reproduce:

  1. have the .NET 6 SDK installed
  2. dotnet new webapi -o AnalyseThis
  3. Update AnalyseThis.csproj to:
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>

    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> 
    <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
    <AnalysisModeStyle>All</AnalysisModeStyle>
    <!-- <AnalysisMode>All</AnalysisMode> -->
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

</Project>

Run dotnet build

Expected Behavior:

Surface IDE messages like this in the build output:

image

Private member ‘WeatherForecastController._logger’ can be removed as the value assigned to it is never read [AnalyseThis] IDE0052

Actual Behavior:

IDE messages not surfaced in build output:

Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  AnalyseThis -> /workspaces/dapr-devcontainer-debug-and-deploy/AnalyseThis/bin/Debug/net6.0/AnalyseThis.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.76

I understand that it was previously by design that this was not surfaced, but if I read the docs correctly, things have changed. And as of .NET 6, the rules should be surfaced by dotnet build. However, I haven’t managed to achieve that - it’s possible I’m doing it wrong

I took that it was desired behaviour from here: https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#analysislevel

Starting in .NET 6, if you set EnforceCodeStyleInBuild to true, this property affects code-style (IDEXXXX) rules too.

Maybe I’m doing something wrong? or there’s a bug?

cc @JohnMarkHowell

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

@johnnyreilly These analyzers are not one-size fits all and it really should take some thought to turn the severity up and make it a requirement for your team.

Completely agree @JoeRobich - this is more about learning what’s possible. So much more than I realised it turns out!

With that said, dotnet-format exists and is truly intended to be the formatter/linter for C# code. dotnet-format style -v detailed --severity info --verify-no-changes may be a better fit for your scenario.

Thanks for this. I was surprised that dotnet-format is considered to be a linting tool. The name of dotnet-format and in fact the description in the repo suggests that it’s only for formatting:

dotnet-format is a code formatter for dotnet that applies style preferences to a project or solution.

Upon experimenting the command is:

dotnet format style -v detailed --severity info --verify-no-changes

Essentially losing the hyphen. I’ve updated the blog post to include this.

Are you proposing a dotnet_analyzer_diagnostic.prefix-IDE.severity = error configuration (something similar was requested in https://github.com/dotnet/roslyn/issues/52051#issuecomment-804743784)? The reason IDE0052 is unaffected by setting the Style category’s severity is because it belongs to the Quality category of analyzers (see IDE0052 for more details).

Ah this is great. Essentially I didn’t know about the categories of analyzers. They’re documented here and I’ve updated the blog post to reflect this

Given that IDE0052 is part of CodeQuality I can dial the category up with:

# Default severity for analyzer diagnostics with category 'CodeQuality' (escalated to build errors)
dotnet_analyzer_diagnostic.category-CodeQuality.severity = error

I probably don’t want to do this in general - but it’s good to know what’s possible.

I believe that dotnet new editorconfig will generate an .editorconfig file containing all the default code style options with default severity

Totally does - this is gold dust! Again I’ve updated the blog post to reflect. I’ve also credited folk in here who’ve provided some very useful assistance; thank you! ❤️ 🌻

I could dial up each individual IDE**** to a warning or an error, but I wouldn’t get the VS Code problems pane experience of just seeing that information by default. I have to opt in to each one.

@johnnyreilly These analyzers are not one-size fits all and it really should take some thought to turn the severity up and make it a requirement for your team. With that said, dotnet-format exists and is truly intended to be the formatter/linter for C# code. dotnet-format style -v detailed --severity info --verify-no-changes may be a better fit for your scenario. If you’ve used dotnet-format and found it lacking, please consider leaving feedback because we want to see it improve and be more successful for our users.

upgrade all those information diagnostics to warnings - or errors - so I can use them to fail a build.

Are you proposing a dotnet_analyzer_diagnostic.prefix-IDE.severity = error configuration (something similar was requested in #52051)? The reason IDE0052 is unaffected by setting the Style category’s severity is because it belongs to the Quality category of analyzers (see IDE0052 for more details).

it would be great if there was a reference somewhere of what IDE information diagnostics that the Roslyn Analysers are looking for and surfacing by default.

I believe that dotnet new editorconfig will generate an .editorconfig file containing all the default code style options with default severity (@jmarolf to correct me if wrong). You can also see a version of this in the docs under ‘Example EditorConfig file’. Also in the docs, you can also find a listing of CodeStyle IDExxxx diagnostic ids under the ‘Code Style rules > Overview’ section.

I’m definitely seeing them as well with these settings. My output:

D:\source\repos\AnalyseThis\Controllers\WeatherForecastController.cs(3,1): warning IDE0160: Convert to block scoped nam
espace [D:\source\repos\AnalyseThis\AnalyseThis.csproj]
D:\source\repos\AnalyseThis\WeatherForecast.cs(1,1): warning IDE0160: Convert to block scoped namespace [D:\source\repo
s\AnalyseThis\AnalyseThis.csproj]
D:\source\repos\AnalyseThis\Program.cs(1,1): warning IDE0008: Use explicit type instead of 'var' [D:\source\repos\Analy
seThis\AnalyseThis.csproj]
D:\source\repos\AnalyseThis\Program.cs(10,1): warning IDE0008: Use explicit type instead of 'var' [D:\source\repos\Anal
yseThis\AnalyseThis.csproj]
D:\source\repos\AnalyseThis\Controllers\WeatherForecastController.cs(14,57): warning IDE0052: Private member 'WeatherFo
recastController._logger' can be removed as the value assigned to it is never read [D:\source\repos\AnalyseThis\Analyse
This.csproj]
D:\source\repos\AnalyseThis\Program.cs(15,5): warning IDE0058: Expression value is never used [D:\source\repos\AnalyseT
his\AnalyseThis.csproj]
D:\source\repos\AnalyseThis\Program.cs(16,5): warning IDE0058: Expression value is never used [D:\source\repos\AnalyseT
his\AnalyseThis.csproj]
    7 Warning(s)