[SOLVED] How to force dotnet format or MSBuild to use .editorconfig rules?


This Content is from Stack Overflow. Question asked by baitendbidz

When working with JavaScript or TypeScript projects I’m using Eslint to enforce code style and Prettier to enforce code format.

I want to achieve the same behaviour when working with C# projects, I created a new solution with a Web Api project. Inside the .csproj I added the property EnforceCodeStyleInBuild. I added a .editorconfig file to the solution

root = true

dotnet_analyzer_diagnostic.category-Design.severity = error
dotnet_analyzer_diagnostic.category-Documentation.severity = error
dotnet_analyzer_diagnostic.category-Globalization.severity = error
dotnet_analyzer_diagnostic.category-Interoperability.severity = error
dotnet_analyzer_diagnostic.category-Maintainability.severity = error
dotnet_analyzer_diagnostic.category-Naming.severity = error
dotnet_analyzer_diagnostic.category-Performance.severity = error
dotnet_analyzer_diagnostic.category-SingleFile.severity = error
dotnet_analyzer_diagnostic.category-Reliability.severity = error
dotnet_analyzer_diagnostic.category-Security.severity = error
dotnet_analyzer_diagnostic.category-Style.severity = error
dotnet_analyzer_diagnostic.category-Usage.severity = error
dotnet_analyzer_diagnostic.category-CodeQuality.severity = error

# prefer 'var'
dotnet_diagnostic.IDE0007.severity = error
dotnet_diagnostic.IDE0008.severity = none

# Use expression body for methods
dotnet_diagnostic.IDE0022.severity = suggestion

I also installed dotnet format to inspect the format via dotnet format --verify-no-changes after building the solution.

This setup works pretty solid but there are some things I’m struggling with, for testing purposes I changed the sample class WeatherForecastController to

using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
    public sealed class WeatherForecastController : ControllerBase
        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<int> Get()
            // use var instead
            string foo = "not using var";

            // remove some empty lines

            // add some linebreaks and remove redundant select statements
            return Enumerable.Range(1, 5).Select(x => x).Select(x => x).Select(x => x).Select(x => x).Select(x => x).Select(x => x).ToArray();

I added some comments to show the expected behaviour. Does someone know if I’m missing something or how to achieve it?

As a sidenote: I’m using Rider instead of Visual Studio but I think this shouldn’t matter ( some people also use Visual Studio Code ).


Try to configure this rule with the following options in your .editorconfig:

csharp_style_var_for_built_in_types = true 
csharp_style_var_when_type_is_apparent = true
csharp_style_var_elsewhere = true

See the reference:

This Question was asked in StackOverflow by baitendbidz and Answered by cezn It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?