Stylecopanalyzers: Feature Request: Shared configuration, cross-solution

Created on 13 Mar 2018  路  8Comments  路  Source: DotNetAnalyzers/StyleCopAnalyzers

It would be great if there was a way to define the preferred StyleCop configuration (with stylecop.json and perhaps an accompanying ruleset file) and have it applied to every solution of an organization.

The model I'm thinking of is how ESLint works in the nodeJS world. You can define your code style preferences and package them as an npm package. Anyone can follow those rules by installing that package.

Perhaps something similar can be implemented with NuGet?

Looking into the csproj format, I see two separate moving parts:

  • CodeAnalysisRuleSet, which specifies the custom rule set file
  • stylecop.json, which I understand it is expected to be at the root of the project level.

In the ideal situation, it should be possible to point both these two elements to a NuGet package containing the common organization-wide ruleset and stylecop.json file.

Most helpful comment

All 8 comments

Shared configurations are already possible. Approaches include:

Use Directory.Build.props for applying configurations to projects in subdirectories

https://github.com/tunnelvisionlabs/MouseFastScroll/blob/5644aeb0115dad820c78ea4446dd540ee57fbfe1/Directory.Build.props#L35-L45

Roll-your-own NuGet package which configures analyzers

  • Declares a dependency on analyzer packages
  • Configures the rule set for projects
  • Includes and configures stylecop.json for projects

Example: https://github.com/mdmsua/csharp-styleguide

Thanks for such a fast response! I need to spend some time studying these approaches as I'm not familiar with props files.

It worked great. I'll try to make some time to write a guide. Perhaps I'll make a PR to have it as part of the documentation of this project.

Thank you for the explanation, you have saved me a lot of time @sharwell . But I am facing a problem with including a default .editorconfig in a nuget package:
I tried to add this line in Directory.Build.props the same way the stylecop.json is added

<AdditionalFiles Include="$(MSBuildThisFileDirectory).editorconfig" Link=".editorconfig" />

I noticed that a reference to the file is added to the project as it appears in the solution explorer but when i click on it, it won't open so I tried to navigate to its containing folder and it is not there. But when i copy it to the package directory C:\Users[username].nuget\packages[package_name][verion]\build it works. Is this a bug or i am doing something wrong.

But I am facing a problem with including a default .editorconfig in a nuget package

This is not currently supported. .editorconfig is a filesystem-based specification and must be placed in a specific location relative to source files or the IDE will not be able to locate it.

Does this feature related to nuget packaging or Roslyn?

Also, i am facing another issue when i try to install my nuget package in a .Net framework project "console, desktop, .." it won't work showing an error

Unable to find a version of 'StyleCop.Analyzers' that is compatible with '[MyNugetPackage] constraint: StyleCop.Analyzers (>= 1.1.0-beta006)'
but it works for .net standard or .net core projects ? Do i have to lower the version of StyleCop or what ?

Does this feature related to nuget packaging or Roslyn?

It's a Roslyn issue/limitation.

but it works for .net standard or .net core projects ? Do i have to lower the version of StyleCop or what ?

I'm not sure what the requirements are here. This seems like a question for the NuGet team since it depends on how all the individual packages were constructed, deployed, and consumed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SetTrend picture SetTrend  路  5Comments

iron9light picture iron9light  路  4Comments

DirtyPiece picture DirtyPiece  路  7Comments

kindermannhubert picture kindermannhubert  路  6Comments

snowjim picture snowjim  路  6Comments