question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sharwellcommented, May 13, 2020

…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
1reaction
nguyenlamlllcommented, May 13, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SA1200 is firing for global usings · Issue #3404
It's clear that Microsoft has been working hard to reduce boilerplate with file-scoped namespaces, implied Main method, record types and ...
Read more >
StyleCopAnalyzers/documentation/SA1200.md at master
When multiple namespaces are defined within a single file, placing using directives within the namespace elements scopes references and aliases.
Read more >
c# - Stop Visual Studio from putting using directives outside ...
You can set this in Re-sharper. Re-sharper > Options > C# > Namespace Imports > Add using directive to the deepest scope.
Read more >
Using statements inside or outside of namespace? : r/csharp
Rule SA1200 - A C# using directive is placed outside of a namespace ... I've seen some "enterprisey" code have it inside the...
Read more >
CA1020: Avoid namespaces with few types - Visual Studio ...
It is safe to suppress a warning from this rule when the namespace does not contain types that are used with the types...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found