"CS8700: Multiple analyzer config files cannot be in the same directory" but only one StyleCop file
Asked Answered
B

2

22

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.

Bunyabunya answered 9/6, 2020 at 4:15 Comment(0)
B
11

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.

Bunyabunya answered 9/6, 2020 at 17:49 Comment(5)
Actually, you should be able to add project-specific .editorconfig files nested inside subfolders, simply by not specifying root = true option (reference)Enroll
@Enroll - 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.Bunyabunya
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.Enroll
@Enroll 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.Apropos
@Apropos 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 toEnroll
A
18

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.

Apropos answered 25/2, 2022 at 21:34 Comment(1)
Note that you might need to restart VS for this to take effect on Intellisense.Sybaris
B
11

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.

Bunyabunya answered 9/6, 2020 at 17:49 Comment(5)
Actually, you should be able to add project-specific .editorconfig files nested inside subfolders, simply by not specifying root = true option (reference)Enroll
@Enroll - 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.Bunyabunya
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.Enroll
@Enroll 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.Apropos
@Apropos 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 toEnroll

© 2022 - 2024 — McMap. All rights reserved.