suitendaal
suitendaal

Reputation: 193

Why does StyleCopAnalyzers think that global usings should be declared within a namespace, when such syntax is not possible?

I am creating a NUnit test project and in the creation of that, MS Visual Studio has created a file called Usings.cs with the line

global using NUnit.Framework;

which tells the project to include the NUnit framework in every file.

I have been running StyleCopAnalyzers over this test project, and it keeps reporting

SA1200: Using directive should appear within a namespace declaration.

However, when I put the global using within a namespace declaration

namespace TestProject
{
    global using NUnit.Framework;
}

I get the error

CS8914: A global using directive cannot be used in a namespace declaration.

What is the correct approach? Should I use the Usings.cs file with global usings?

Upvotes: 1

Views: 968

Answers (2)

Atif
Atif

Reputation: 1547

Looks like the default may have changed. The fix involves updating your editor config file (.editorconfig) that should be in the same directory as your solution.

Look for the following

  • csharp_using_directive_placement
  • dotnet_diagnostic.SA1200.severity

You want them to look like

csharp_using_directive_placement = outside_namespace
dotnet_diagnostic.SA1200.severity = None

or just delete csharp_using_directive_placement to get the default behavior

dotnet_diagnostic.SA1200.severity = None

In my project, csharp_using_directive_placement was set to inside_namespace, and this is impossible in a global using file.

Upvotes: 0

Tanzim Siddiqee
Tanzim Siddiqee

Reputation: 582

In your .csproj add the following lines

 <ItemGroup>
    <Using Include="NUnit.Framework" />
  </ItemGroup>

Upvotes: 1

Related Questions