DotNetAnalyzers / StyleCopAnalyzers

An implementation of StyleCop rules using the .NET Compiler Platform
MIT License
2.66k stars 507 forks source link

SA1200 How to suppress it for a few files (namespaces)? #3151

Open nguyenlamlll opened 4 years ago

nguyenlamlll commented 4 years ago

Hi everyone, I meet this scenario when I try to disable rules for auto-generated files of EF Core. I can suppress a few other messages for the scope namespaceanddescendants, targeting migration files' namespaces.

However, I cannot suppress SA1200 for some reasons (bug, or by design?).

Auto-generated code:

using System;
using Microsoft.EntityFrameworkCore.Migrations; // These 2 using statements throw warning

namespace My.Data.Migrations.Parent.Namespace
{
    ...
}

In my GlobalSupressions.cs file, I have:

[assembly: SuppressMessage(
    "StyleCop.CSharp.OrderingRules",
    "SA1200:UsingDirectivesMustBePlacedWithinNamespace",
    Justification = "Auto-generated by EF Core.",
    Scope = "namespaceanddescendants",
    Target = "My.Data.Migrations.Parent.Namespace")]

This does not work. I have also tried "member", "resource", "module", "type", "method", "namespace". Nothing works.

Moreover, the way that the document suggests is not really suitable for me because I don't want to suppress the message for the whole assembly.

So, my question is that can we suppress SA1200 for files/namespaces? (and how?) Thank you all.

sharwell commented 4 years ago

StyleCop Analyzers does not enable rules in generated code files. Can you provide some additional information about the files where you want to disable it?

nguyenlamlll commented 4 years ago

I'm talking about migration files generated by EF Core (or EF In general) in code-first approach.

using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Migrations
{
    public partial class Name: Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        { ... }
        protected override void Down(MigrationBuilder migrationBuilder)
        { ... }
    }
}

Alternatively.

They are generated with using statements outside of namespace scopes.

sharwell commented 4 years ago

Are these files generated once (e.g. the way a new file gets created from a template), or repeatedly (e.g. the way .xaml files generate a code file in the obj directory during each build)?

nguyenlamlll commented 4 years ago

More of "repeatedly". When the devs make changes to the database models, they run the CLI. A few new files (one is the example I gave above) will be generated. So, our current workflow after each CLI run like that is to go into the files and put #pragma warning disable to every file that is generated.

Using GlobalSupressions, I was able to suppress other warnings. The only left is SA1200.

sharwell commented 4 years ago

...A few new files...

Are these new files with new names, or do they overwrite files? Put another way, after the pragma lines are added, will the code generator ever overwrite the file and remove them?

If each file is only generated one time, my suggestion would be:

  1. Fix SA1200 in the generated file rather than suppress it
  2. File a bug for EF requesting that the code template honor the .editorconfig setting that instructs the IDE to place using directives inside namespaces
nguyenlamlll commented 4 years ago

They are completely new each time. Saying, 2 files per time. So, after we run CLI things 3 times, we have 6 files.

I see that your suggestions might be alright for us. Thank you.

Besides that, I don't know if I can suppress SA1200 for a whole file or namespace? Just curious now.

Trolldemorted commented 4 years ago

File a bug for EF requesting that the code template honor the .editorconfig setting that instructs the IDE to place using directives inside namespaces

I assume it would also suffice if they'd prepend // <auto-generated /> to the migration file? They also don't place trailing commas, which is another source of warnings.

Dreamescaper commented 3 years ago

I don't know if it's possible to disable rules per namespace, but you can disable them per path in editorconfig file:

[{Folder1/*.cs,**/Folder2/*.cs}]
dotnet_diagnostic.SA1200.severity = none