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:
- have the .NET 6 SDK installed
dotnet new webapi -o AnalyseThis
- 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:
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?
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (4 by maintainers)
Completely agree @JoeRobich - this is more about learning what’s possible. So much more than I realised it turns out!
Thanks for this. I was surprised that
dotnet-format
is considered to be a linting tool. The name ofdotnet-format
and in fact the description in the repo suggests that it’s only for formatting: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.
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:I probably don’t want to do this in general - but it’s good to know what’s possible.
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! ❤️ 🌻
@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 useddotnet-format
and found it lacking, please consider leaving feedback because we want to see it improve and be more successful for our users.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).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: