Search code examples
c#.netvisual-studio-2022static-analysiseditorconfig

In VS2022 can you setup a solution wide editorconfig, and and project specific editorconfig that override?


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?


Edit: As an added fyi, I am familiar with the MS documentation on .editorconfig folder hierarchy.

The method in the documentation did not work on my actual project or a purposely created demo project. It was tested on VS2022 17.4.3 and Preview 17.5. With experience, you will learn documentation and features do not always align.;)


Solution

  • 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
    

    In VS2022 17.4.3+, when dealing with multiple .editorconfigs you may need to use a standard text editor instead of the default .editorconfig gui tool.