How to enable code analysis IDE0058 as a build error?

I want to enable IDE0058 as an error during build. Here’s how I attempted to do that.

  1. Start Visual Studio 2022 (Version 17.11.2)

  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”

  3. Next

  4. Project name: CodeAnalysisError

  5. Location: somewhere on my hard disk

  6. [x] Place solution and project in the same directory

  7. Next

  8. Framework: .NET 8.0 (Long Term Support)

  9. Create

  10. Content of Program.cs is taken from the example of IDE0058:

    System.Convert.ToInt32("35");
    
  11. Right click the project in solution explorer

  12. Choose Add / New item …

  13. In the filter, type “editor”

  14. Choose “editorconfig File (empty)

  15. Add

  16. The default content of the file is

    # All files
    [*]
    indent_style = space
    
    # Xml files
    [*.xml]
    indent_size = 2
    
  17. Add the line in section [*]

    dotnet_diagnostic.IDE0058.severity = error
    
  18. 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 analysis

    is 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.

6

If you write the following code to a function(e.g:Test), VS will treat the code analysis IDE0058 as a build error.

System.Convert.ToInt32("35");
void Test()
{
    System.Convert.ToInt32("35");
}

Test result:

Error List

Build Failed:

Update

1.csproj file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
     <AnalysisLevel>preview-recommended</AnalysisLevel>
  </PropertyGroup>

  <ItemGroup>
    <EditorConfigFiles Remove="C:UserssourcereposCodeAnalysisErrorCodeAnalysisError.editorconfig" />
  </ItemGroup>

  <ItemGroup>
    <None Include="C:UserssourcereposCodeAnalysisErrorCodeAnalysisError.editorconfig" />
  </ItemGroup>

</Project>

2.editorconfig

# C# files
[*.cs]

# Indentation and spacing
indent_size = 4
indent_style = space
tab_width = 4

#### Diagnostic configuration ####

dotnet_diagnostic.IDE0058.severity = error

Test result:

3

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật