19

I'm trying to learn to use StyleCop on a personal project. It's not a very big one, and the solution structure is below:

- MySolution (2 of 2 projects)
   - Solution Items
      - .editorconfig
      - .gitignore
      - CODEOWNERS
      - my-pipeline-workflow.yml
      - README.md
      - stylecop.json
   - MyMainProject
      - Dependencies
      - .editorconfig (pointer Solution Items copy)
      - OneOfManyCsFiles.cs
      - stylecop.json (pointer Solution Items copy)
   - MyTestProject
      - Dependencies
      - .editorconfig (pointer Solution Items copy)
      - OneOfManyTestCsFiles.cs
      - stylecop.json (pointer Solution Items copy)

And here is how the folders themselves are structured:

- RepoFolder
   - .github
      - workflows
         - my-pipeline-workflow.yml
   - src
      - MyMainProject.csproj
      - OneOfManyCsFiles.cs
   - test
      - MyTestProject.csproj
      - OneOfManyTestCsFiles.cs
   - .editorconfig
   - .gitignore
   - CODEOWNERS
   - MySolution.sln
   - README.md
   - stylecop.json

Everything worked prior to adding the file for StyleCop. I could build and test just fine in Visual Studio. For whatever reason, adding the file for StyleCop (and maybe the .editorconfig file) seems to have caused this error on build:

CSC: error CS8700: Multiple analyzer config files cannot be in the same directory ('/home/runner/work/MySolution/MySolution').
[/home/runner/work/MySolution/MySolution/src/MyMainProject.csproj]

As far as I understand, the StyleCop file is the only analyzer file and it's referenced in multiple places. Does the .editorconfig somehow count as another analyzer file? If so, how do I get them to play nice?

Suppressing the error in .editorconfig does nothing. I haven't been able to find any helpful documentation when searching the error alone either.

Metomorphose
  • 434
  • 3
  • 14

2 Answers2

16

Just for other googler's reference.

I used NiccoMlt's structure, but encountered CS8700. I solved this by removing the following from the problematic project file.

  <ItemGroup>
    <EditorConfigFiles Include=".editorconfig" />
  </ItemGroup>
  <ItemGroup>
    <None Remove=".editorconfig" />
  </ItemGroup>

I don't know why this is inserted, but it solved my problem.

jeiea
  • 1,965
  • 14
  • 24
9

Turns out the issue is trying to reference the .editorconfig file from the projects. I deleted the references and just left the file as a solution item in the root of the solution. Most of the settings I have worked fine from there, but I had some StyleCop severity settings that weren't getting picked up properly for the analyzer.

To fix that, I changed to using GlobalSuppressions.cs files for each project individually. I could probably have bundled these together and referenced them like I'm doing with the stylecop.json file, but decided that limiting the suppression scopes was probably more appropriate.

Metomorphose
  • 434
  • 3
  • 14
  • 3
    Actually, you should be able to add project-specific `.editorconfig` files nested inside subfolders, simply by _not_ specifying `root = true` option ([reference](https://learn.microsoft.com/it-it/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019#file-hierarchy-and-precedence)) – NiccoMlt Nov 24 '20 at 20:28
  • @NiccoMlt - True, though my issue was that I was using a reference to the same file in two different projects. The goal was to not duplicate files where it wasn't needed. – Metomorphose Nov 27 '20 at 15:15
  • 2
    Yes, I understood the problem, and your solution works correctly. I was pointing out that, to improve consistency, you could stick with a single type of configuration file (EditorConfig) by using a root `.editorconfig` not referenced in the projects (like you did) and a nested `.editorconfig` file inside each project which needed to tune settings, avoiding the use of `GlobalSuppressions.cs`. – NiccoMlt Nov 27 '20 at 18:16
  • @NiccoMlt I encountered the same error with your structure. But it may be visual studio bug. I ensured I didn't use the reference of the same file. – jeiea Feb 25 '22 at 20:41
  • 1
    @jeiea yes, it seems a weird behavior from Visual Studio; I'm not using Visual Studio anymore, but IIRC `.editorconfig` files should _be there_, but _not_ declared in the project file. So I think that the fix you proposed in your answer below actually is what I expect Visual Studio to set the project file to – NiccoMlt Feb 26 '22 at 21:12