Home > Software engineering >  In VS2022 can you setup a solution wide editorconfig, and and project specific editorconfig that ove
In VS2022 can you setup a solution wide editorconfig, and and project specific editorconfig that ove

Time:12-27

In Visual studio 2022, can you setup a solution wide .editorconfig file and and then project specific .editorconfig files that override or add to the solution wide one?

In my case, I am looking to have different settings for unit tests projects, and/or disable legacy method warnings on legacy projects.

Does Visual Studio 2022 support this?

CodePudding user response:

Yes, the setup of .editorconfig files is hierarchical, where settings in child .editorconfig files add to or override those in parent .editorconfig files.

It is even possible to stop the inheritance starting from a certain level in a child folder by including root = true in that child .editorconfig file.

From the documentation

When you add an .editorconfig file to a folder in your file hierarchy, its settings apply to all applicable files at that level and below.

To override some or all of the EditorConfig settings, add an .editorconfig file at the level of the file hierarchy you want those overridden settings to apply. The new EditorConfig file settings apply to files at the same level and any subdirectories.

If you want to override some, but not all of the settings, specify just those settings in the .editorconfig file. Only those properties that you explicitly list in the lower-level file are overridden. Other settings from higher-level .editorconfig files continue to apply.

Such a folder and files hierarchy might look like below.

\ YourSolution
  - YourSolution.sln
  - .editorconfig
    ...      
  \ Src      
    \ Project1
      ...    
    \ Project2    
      ...
    \ Project3
      - .editorconfig    
      ...            
  \ Tests      
    - .editorconfig        
    \ Tests1
      ...          
    \ Tests2
      - .editorconfig

The root .editorconfig file can also be in a common parent folder for all your Visual Studio solutions.

\ YourSolutions
  - .editorconfig
  \ YourSolution1
    - YourSolution1.sln  
    ...
  \ YourSolution2
    - YourSolution2.sln
    ...
    

Example files.

Root .editorconfig file

[*.cs]
dotnet_sort_system_directives_first = true
dotnet_style_require_accessibility_modifiers = 
for_non_interface_members:error

Child folder .editorconfig file

[*.cs]
dotnet_sort_system_directives_first = false
dotnet_style_require_accessibility_modifiers = 
for_non_interface_members:silent

CodePudding user response:

The answer by pfx is an excellent overview using .editorconfig files - as described, they are hierarchical based on the directory, so precedence is always implied by how close the property is defined to the file (there can't be two .editorconfig files with equal precedence).

As a supplemental answer because you tagged C#, I'll also mention .globalconfig files. Where .editorconfig files apply to the contents of a directory, .globalconfig files are for applying analyzer rules based on project (or as part of a NuGet package). For example, if you have files linked into a project from outside the project folder, .editorconfig settings for that project will not be applied to those files, but .globalconfig settings will.

Globalconfig files can be inherited implicitly or explicitly included on a per-project basis (if named something other than ".globalconfig"). See https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files#naming. This can help if your project structure looks something like this:

\ YourSolution
  - YourSolution.sln
  - .editorconfig
  - Directory.Build.props
    ...      
  \ Src      
    \ Feature1
      \src
        - Feature1.csproj
        ...
      \test
        - Feature1.Test.csproj
        ...
    \ Feature2
      \src
        - Feature2.csproj
        ...
      \test
        - Feature2.Test.csproj
        ...

In this case, you can define something in your Directory.Build.props file to apply only to test projects, such as

<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('.Test'))">
  <GlobalAnalyzerConfigFiles Include="AnalyzerSettingsForTestProjects.globalconfig" />
</PropertyGroup>

Globalconfig files can conflict with each other or with .editorconfig files that apply to the same files. Globalconfig files can specify a global_level property to specify their relative precedence. .editorconfig will win over .globalconfig. The rules for conflict resolution are laid out here: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files#precedence

  • Related