UsingLayoutAnalyser 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package UsingLayoutAnalyser --version 0.2.0
                    
NuGet\Install-Package UsingLayoutAnalyser -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UsingLayoutAnalyser" Version="0.2.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UsingLayoutAnalyser" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="UsingLayoutAnalyser">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add UsingLayoutAnalyser --version 0.2.0
                    
#r "nuget: UsingLayoutAnalyser, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package UsingLayoutAnalyser@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=UsingLayoutAnalyser&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=UsingLayoutAnalyser&version=0.2.0
                    
Install as a Cake Tool

UsingAnalyser

Enforces a using layout that neither the built-in options nor StyleCop can express:

using System;
using System.Blagh;

using Gizmo.Widget;
using ThirdParty.Library.Thingy;

using SolutionPrefix.Host;
using SolutionPrefix.Model;

System first, then third party, then the solution's own namespaces. Three blocks, one blank line between them, each block sorted alphabetically.

Every using block in a file is laid out independently: the file's own directives, and any written inside a namespace - the placement StyleCop's SA1200 asks for by default. Nested namespaces each get a block of their own, since usings in different scopes are in force in different places.

Why this needs an analyser

  • dotnet_separate_import_directive_groups does insert blank lines, but a "group" is the first-level namespace. It splits third party into one block per vendor, and has no way to tell a vendor from you.
  • dotnet_sort_system_directives_first gets System to the top and stops there.
  • StyleCop's SA1208 and SA1210 know "System first, then alphabetical" and have no notion of a blank-line block at all.

Neither of the dotnet_* options is a diagnostic. They configure the editor's sort-usings action and dotnet format, so nothing enforces them during a build.

Configuration

Three keys, in .editorconfig. All of them carry the usinglayout. prefix, so nothing here can collide with a built-in option, a StyleCop setting, or another analyser's.

[*.cs]
usinglayout.first_party_prefixes = SolutionPrefix
usinglayout.separate_system = true
usinglayout.separate_first_party = true
usinglayout.separate_roots = false

first_party_prefixes is comma-separated for a solution spanning several roots (Contoso.Platform, Contoso.Internal). Matching is ordinal, because namespaces are case sensitive, and a root only matches at a dot boundary, so System never swallows SystemsManager. Leaving it unset is a legitimate configuration rather than an error: the scheme collapses to System-then-everything-else.

The two separate_* keys default to true and control the blank lines independently:

separate_system separate_first_party Result
true true Three blocks (the default).
false true System runs into third party; first party set apart.
true false System set apart; third party runs into first party.
false false One run, still ordered.

Turning both off does not turn the scheme off - it stops the scheme being visible. UA1000 still sorts System, then third party, then first party; there is just nothing between the blocks.

separate_roots is the fourth, and it works one level down: inside a block, it puts a blank line wherever the first namespace segment changes, so vendors stand apart from each other.

using Evilcorp.Thingamabob;

using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;

using NSubstitute;

It defaults to false, unlike the other three. Turning it on relayouts every file with more than one vendor in a block, and that is not a thing to do to somebody who upgraded a patch version.

It never overrules the block toggles. A boundary between blocks is theirs alone, so switching separate_system off keeps System running into third party even though their roots differ - asking for roots to be split does not quietly reopen a boundary you closed. Aliases are left alone too: an alias sorts under its alias, which rarely contains a dot, so every alias would otherwise become a root of its own and collect a blank line.

Ordering never changes for any of this. The sort is alphabetical segment by segment, so a root's namespaces are already contiguous - separate_roots only decides whether a blank line falls between them.

Two things the toggles do not reach. A file with no third-party usings has one boundary that crosses both toggles at once, and it takes a blank line if either toggle asks for one, so a block you asked to set apart stays set apart regardless of what else the file happens to import. And the trailing using static and alias blocks are always separated: they exist to keep SA1216 and SA1209 satisfied, and running them into the block above would undo that.

An unparseable value falls back to the default rather than reporting. A typo in a layout setting should not be the thing that fails a build.

Rules

Rule Says
UA1000 The directives are in the wrong order.
UA1001 The order is right but the blank lines between blocks are not.

Both are warnings by default, both are fixable, and at most one is reported per file - the fix rewrites the whole block in a single edit, so a report per misplaced line would be one problem described N times.

Installing

As a project reference, which is the quickest way to try it:

<ProjectReference Include="../UsingAnalyser/UsingAnalyser/UsingAnalyser.csproj"
                  OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="../UsingAnalyser/UsingAnalyser.CodeFixes/UsingAnalyser.CodeFixes.csproj"
                  OutputItemType="Analyzer" ReferenceOutputAssembly="false" />

Or as a package, dotnet pack UsingAnalyser.CodeFixes (package id UsingLayoutAnalyser), which puts both assemblies where Roslyn looks for them.

Turning it on in an existing repository

dotnet format applies the fix in bulk:

dotnet format analyzers YourSolution.slnx --diagnostics UA1000 UA1001 --severity warn

Three settings, and the canonical layout then builds clean:

csharp_using_directive_placement = outside_namespace:error
dotnet_separate_import_directive_groups = false
dotnet_diagnostic.SA1210.severity = none

Which other rules this touches

Measured rather than reasoned about, against StyleCop.Analyzers 1.2.0.556 with every Style category rule at warning, on a canonical file including statics and aliases.

Conflicts. These two must be configured, or the layout will not build clean.

Rule What happens Settle it with
SA1210 Sorts the whole list alphabetically, so it wants third party and first party interleaved - precisely the split this scheme creates. Every laid-out file becomes a warning, and under TreatWarningsAsErrors a broken build. dotnet_diagnostic.SA1210.severity = none. UA1000 takes over sorting entirely.
IDE0055 Fires only when dotnet_separate_import_directive_groups = true, because that option wants blank lines by first-level namespace. Under EnforceCodeStyleInBuild it is a build warning, not merely the editor regrouping behind you. dotnet_separate_import_directive_groups = false, which clears it with everything else held constant.

A trap that is not this analyser's doing. SA1200 fires on every using under StyleCop's defaults, because it wants them inside the namespace. Declaring csharp_using_directive_placement = outside_namespace silences it - StyleCop honours that option. Either placement works here, so this only decides which shape you are enforcing, not whether the analyser applies.

Compatible, verified silent on the canonical layout: SA1208 (System first), SA1209 (aliases last), SA1211 (aliases alphabetical), SA1216 and SA1217 (using static placement and order), SA1516 - including with stylecop.layout.allowConsecutiveUsings = false - and dotnet_sort_system_directives_first = true. IDE0005 is orthogonal: it removes usings nothing needs, which is a separate question from where the rest go.

Sorting is case-insensitive, deliberately matching what SA1210 accepts. Ordinal comparison would put CSharp above CodeActions, since S sits below o in character order, and this rule would then contradict the one it asks you to keep switched on everywhere else.

What it deliberately leaves alone

  • Global usings. The compiler already pins them to the front, and moving a using across that boundary is a change in meaning rather than in layout.
  • Any file with a #if around a using. The position of a conditioned using is meaningful, and layout is not worth changing which usings the compiler sees.
  • A file header. A comment above the first using that is followed by a blank line stays at the top. A comment with no blank line under it belongs to the directive below it and travels with it.
  • The file's line endings, taken from the file rather than assumed, so a fix never turns into a whole-file diff on the other platform.

Licence

MIT. See LICENSE.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0 3,278 8/13/2026
0.3.0 1,147 8/4/2026
0.2.1 499 8/2/2026
0.2.0 384 8/2/2026
0.1.0 143 8/1/2026