|
| 1 | +# Using StyleCop Analyzers with .NET Core |
1 | 2 |
|
2 | | -# Using StyleCop Analyzers with dotnet cli |
| 3 | +StyleCop Analyzers can be used with the **dotnet** tooling, including ASP.NET Core. |
3 | 4 |
|
4 | | -StyleCop Analyzers can be used in dotnet cli projects, including asp.net core. |
5 | | -The tooling support is currently not great and the analyzers only run when the project is compiled and there is currently no way to invoke the code fixes. What does work however is running Stylecop Analyzers in ubuntu on coreclr and OSX (probably) works too. |
6 | | -To set this up a few steps are required: |
| 5 | +## .NET SDK Projects (*.csproj) |
| 6 | + |
| 7 | +Edit the project file and add a package reference to **StyleCop.Analyzers**. Make sure to set **PrivateAssets** so the |
| 8 | +reference is not included when transitive dependencies are calculated across project references: |
7 | 9 |
|
8 | | -First the StyleCopAnalyzers nuget package has to be added to the project. Optimally the package should be marked as `build` type so it is not included as a package by other projects consuming it. |
9 | | -For this add the following to the dependencies section of the project.json file: |
10 | | -```json |
11 | | - "StyleCop.Analyzers": { |
12 | | - "version": "1.0.0", |
13 | | - "type": "build" |
14 | | - } |
15 | | -``` |
16 | | -For VS2017 / dotnet core 1.1.1 SDK 1.0.1 projects edit the `.csproj` file and mark the `StyleCop.Analyzers` as `PrivateAssets`: |
17 | 10 | ```xml |
18 | | - <ItemGroup> |
19 | | - <PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta001" PrivateAssets="All" /> |
20 | | - </ItemGroup> |
| 11 | +<ItemGroup> |
| 12 | + <PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004" PrivateAssets="All" /> |
| 13 | +</ItemGroup> |
21 | 14 | ``` |
| 15 | + |
22 | 16 | If the project is restored and built right now this will already run the analyzers. A few extra steps are needed if they should be configured. |
23 | 17 |
|
24 | | -## Rulesets and stylecop.json |
| 18 | +### Rulesets and stylecop.json |
25 | 19 |
|
26 | | -To supply a ruleset file and a stylecop.json configuration file to the compiler they have to be manually added as arguments to the compiler. For this add the following under the `buildOptions` node in the project.json file: |
27 | | -```json |
28 | | - "additionalArguments": [ "/ruleset:path/to/ruleset.ruleset", "/additionalfile:path/to/stylecop.json" ] |
| 20 | +Update the project file as follows to apply settings and custom rules: |
| 21 | + |
| 22 | +```xml |
| 23 | +<PropertyGroup> |
| 24 | + ... |
| 25 | + <CodeAnalysisRuleSet>stylecop.ruleset</CodeAnalysisRuleSet> |
| 26 | +</PropertyGroup> |
| 27 | +<ItemGroup> |
| 28 | + <AdditionalFiles Include="stylecop.json" /> |
| 29 | +</ItemGroup> |
29 | 30 | ``` |
30 | 31 |
|
31 | | -**Note: ** `additionalArguments` is not currently defined in the schema but does exist and is passed during the build |
| 32 | +### Enabling XML documentation processing |
| 33 | + |
| 34 | +Analyzers for XML documentation can only run if the XML documentation comment processing is enabled. See [SA0001](SA0001.md) for more information. |
| 35 | + |
| 36 | +Update the project file to include the following: |
32 | 37 |
|
33 | | -On VS VS2017 / dotnet core 1.1.1 SDK 1.0.1 projects update `.csproj` as follows to apply settings and custom rules: |
34 | 38 | ```xml |
35 | | - <PropertyGroup> |
36 | | - ... |
37 | | - <CodeAnalysisRuleSet>stylecop.ruleset</CodeAnalysisRuleSet> |
38 | | - </PropertyGroup> |
39 | | - <ItemGroup> |
40 | | - <AdditionalFiles Include="stylecop.json" /> |
41 | | - </ItemGroup> |
| 39 | +<PropertyGroup> |
| 40 | + ... |
| 41 | + <GenerateDocumentationFile>true</GenerateDocumentationFile> |
| 42 | +</PropertyGroup> |
42 | 43 | ``` |
43 | 44 |
|
44 | | -## Enabling xml documentation processing |
| 45 | +## Legacy Projects (*.xproj) |
| 46 | + |
| 47 | +Legacy projects use **project.json** to configure analyzers and other build options. Start by adding the following to |
| 48 | +the `dependencies` section of **project.json**: |
45 | 49 |
|
46 | | -All analyzers regarding xml documentation can only run if the xml processing is enabled. To do this add |
47 | 50 | ```json |
48 | | -"xmlDoc": "true" |
| 51 | +"StyleCop.Analyzers": { |
| 52 | + "version": "1.0.2", |
| 53 | + "type": "build" |
| 54 | +} |
49 | 55 | ``` |
50 | 56 |
|
51 | | -to the `buildOptions` node of the project.json file. Note that this might cause additional CS1591 diagnostics to appear by the compiler. |
52 | | -They can be suppressed in the ruleset file if necessary. |
| 57 | +The rule set and configuration file can be configured by adding the following to the `buildOptions` section: |
53 | 58 |
|
54 | | -For VS VS2017 / dotnet core 1.1.1 SDK 1.0.1 projects update `.csproj` |
55 | | -```xml |
56 | | - <PropertyGroup> |
57 | | - ... |
58 | | - <GenerateDocumentationFile>true</GenerateDocumentationFile> |
59 | | - </PropertyGroup> |
| 59 | +```json |
| 60 | +"additionalArguments": [ |
| 61 | + "/ruleset:path/to/ruleset.ruleset", |
| 62 | + "/additionalfile:path/to/stylecop.json" |
| 63 | +], |
| 64 | +"xmlDoc": "true" |
60 | 65 | ``` |
0 commit comments