Stylecopanalyzers: stylecop.json seems to be ignored

Created on 2 Nov 2015  路  13Comments  路  Source: DotNetAnalyzers/StyleCopAnalyzers

I've created a stylecop.json via the recommended "Add StyleCop settings file to the project" and configured it to be:

{
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "documentationRules": {
      "companyName": "ANYTHING",
      "copyrightText": "Copyright (c) {companyName}. All rights reserved."
    }
  }
}

Unfortunately the following header violates SA1636:

// <copyright file="BundleConfig.cs" company="ANYTHING">
// Copyright (c) ANYTHING. All rights reserved.
// </copyright>

What am I doing wrong?

question resolved

Most helpful comment

Not sure what is happening. Could you try and edit the .csproj file so that it adds the stylecop.json file as AdditionalFiles? See the sample below what that should look like. Also make sure that the stylecop.json entry is not combined with other entries in the same ItemGroup.

<ItemGroup>
  <AdditionalFiles Include="stylecop.json" />
</ItemGroup>

All 13 comments

Not sure what is happening. Could you try and edit the .csproj file so that it adds the stylecop.json file as AdditionalFiles? See the sample below what that should look like. Also make sure that the stylecop.json entry is not combined with other entries in the same ItemGroup.

<ItemGroup>
  <AdditionalFiles Include="stylecop.json" />
</ItemGroup>

I had the same issue when I first added the file. Restart of VS fixed it for me.

Scrap that, it wasn't the restart. It was the fact that the file was set as Content.

This worked for me, for some reason. I only had to add it to a couple projects in the solution for it to fix it for all.

After some digging, @vweijsters was right.

For a Web Application project, the stylecop.json file is created with a "Build Action" configuration of "Content". Changing this to "Additional Files" fixed my issue.

For a Class Library project, the stylecop.json file is created with a "Build Action" configuration of "None". That seems to work fine.

Thanks for your help.

@spdufeu One option is to implement the same trick for Content that we used for cases where the Build Action is set to None. However, I'm concerned that leaving stylecop.json with a Build Action of Content may have negative side effects regarding publication of a web application project. We don't want to deploy stylecop.json; if it has a Build Action of Content does it get deployed automatically during a Publish operation?

Unfortunately setting the Build Action to Content does deploy the file.

Where as a setting of AdditionalFiles does not.

Sounds like all we can really do is update the documentation to mention that the Build Action needs to be either None or AdditionalFiles (None is the easiest to set of course). We should mention this in Configuration.md, and consider adding a comment to the default stylecop.json file which is created by the code fix that mentions this.

I just ran into the same problem. Putting the stylecop.settings in its own ItemGroup solved the problem. Build Action is None in my case.

What confoused me was that the settings file was _not_ ignored on one machine. After investigation, I found out that after clicking Add stylecop setting file and removing the file stylcop1.settings it worked, too - despite the fact that the stylecop.settings file was not in its own ItemGroup

@ursenzler After running the "Add StyleCop settings file" code fix, the settings will not take effect until the project is reloaded in Visual Studio. The most common way to do this is restarting Visual Studio, but another way is unloading and reloading the project in Solution Explorer. If all you did was move the item to its own ItemGroup (and it already had the type None), then I suspect the action which actually resolved this for you was the unload/reload.

:memo: You will also have to reload the project after restoring NuGet packages in order for stylecop.json to be located by the analyzers. If you don't have packages in source control (this is the most common setup), then you will see this each time a user clones the project. You can bypass this limitation by changing the item type from None to AdditionalFiles.

Thanks for the explanation.

Fixed as part of #1788. See the Breaking Changes section of the 1.0.0-beta016 release notes for information about upgrading.

Hi,

I've this same probem. json file is (now) ignored. I'm using VS2017, I've a C# UnitTest project. The StyleCop NuGet version is 1.1.118. The json file is in the same folder than the project and it is included as "Additional File"
Everything was working until I've added a new C# test class to the project. Now it looks like the json file is ignored (I've many Tabs related errors).
I could not get it working again even undoing the change. I've also tried to clear StyleCop.cache... to un-install/re-install the analyzer, to unload/reload the project, to change between "None" and "Additional File" to reboot VS...

Any help would be welcome.

Jojo

Was this page helpful?
0 / 5 - 0 ratings