skip to Main Content

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)

    Create empty editorconfig file

  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:

Wiggly lines

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

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


  1. 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:
    enter image description here

    Error List
    enter image description here

    Build Failed:
    enter image description here

    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:
    enter image description here

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search