I want to enable IDE0058 as an error during build. Here’s how I attempted to do that.
-
Start Visual Studio 2022 (Version 17.11.2)
-
Create new project. Project type: C#, Windows, Console -> Console App "A project for creating a command-line application that can run on .NET on WIndows, Linux and macOS"
-
Next
-
Project name: CodeAnalysisError
-
Location: somewhere on my hard disk
- [x] Place solution and project in the same directory
-
Next
-
Framework: .NET 8.0 (Long Term Support)
-
Create
-
Content of Program.cs is taken from the example of IDE0058:
System.Convert.ToInt32("35");
-
Right click the project in solution explorer
-
Choose Add / New item …
-
In the filter, type "editor"
-
Choose "editorconfig File (empty)
-
Add
-
The default content of the file is
# All files [*] indent_style = space # Xml files [*.xml] indent_size = 2
-
Add the line in section
[*]
dotnet_diagnostic.IDE0058.severity = error
-
Build the project. It succeeds
Build started at 09:33... 1>------ Build started: Project: CodeAnalysisError, Configuration: Debug Any CPU ------ 1>CodeAnalysisError -> C: [redacted]CodeAnalysisErrorbinDebugnet8.0CodeAnalysisError.dll ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== ========== Build completed at 09:33 and took 10,612 seconds ==========
For reference, the .csproj
file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<EditorConfigFiles Remove="C:[redacted]CodeAnalysisError.editorconfig" />
</ItemGroup>
<ItemGroup>
<None Include="C:[redacted]CodeAnalysisError.editorconfig" />
</ItemGroup>
</Project>
The IDE displays the wiggly lines right from the start:
I have tried:
-
adding
<EnableNETAnalyzers>true</EnableNETAnalyzers>
, but that should not be necessary, since enable code analysisis enabled by default for projects that target .NET 5 and later versions.
-
moving the rule to
[*.cs]
(which again, I think should not matter):[*.cs] dotnet_diagnostic.IDE0058.severity = error
-
Adding
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
, as mentioned here -
Adding
<AnalysisMode>All</AnalysisMode>
as mentioned here just because I wanted to see if I could let the build fail in any way. This starts finding the warning CA1305, but not IDE0058. -
Adding
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
lets the build fail, due to CA1305. I removed that for a moment. -
Setting
<AnalysisLevel>preview-recommended</AnalysisLevel>
as mentioned here found another warning:1>CSC : warning EnableGenerateDocumentationFile: Set MSBuild property 'GenerateDocumentationFile' to 'true' in project file to enable IDE0005 (Remove unnecessary usings/imports) on build (https://github.com/dotnet/roslyn/issues/41640)
-
I read about
.globalconfig
here but figured that I should not need this. -
I read about
.editorconfig
here to make sure I didn’t miss something. -
I made sure that "[x] Follow project coding conventions" is turned as as mentioned here
-
I tried using a
.ruleset
file, but VS tells me that they are deprecated in favor of.editorconfig
. -
Upgrading Visual Studio from 17.10.x to 17.11.2
In summary, I have done everything which is also mentioned in other questions
- my files look like this accepted solution
- considering this answer, removing the two `s did not help.
So, how do I get a build error for IDE0058? If you answer, please describe the steps in detail, so I can follow. I spent more than 2.5 hours already. My code, my solution and everything is minimal.
2
Answers
If you write the following code to a function(e.g:Test), VS will treat the code analysis IDE0058 as a build error.
Test result:
Error List
Build Failed:
Update
1.csproj file
2.editorconfig
Test result:
You can not. The reason for this is that the Roslyn Code Analyzer responsible for this is not available during the build, but only within the environment of your IDE.
It might turn out that SA1409 will give similar results, and that one can configured to run without your IDE being present.