.NET project file analyzers SDK
.NET project file analyzers ships with its own SDK.
Why the .NET project file analyzers SDK?
The .NET project file analyzers work by linking files to a project (most
commonly a *.csproj file), and hook on to Roslyn when that project is built.
For files that can not be linked to a single project to build, this approach
does not work, and that is where the .net.csproj project comes in handy.
How does it work?
The .net.csproj acts as proxy for all files not linked to a single project.
It obtains its powers because it is a little different from normal projects:
- The
.net.csprojfile is typically placed in a parent directory common to all other projects. This could be the root of your repo, the same directory as your solution(s), or somewhere in between. Placement really depends on which (sub) directories should be scanned, experiment a little to see what works for you. The needs of a large monorepo with many solutions and projects differ from those of a small repository with a single solution and just a few projects. - The
.net.csprojproject has a PackageReference to. It automatically includes files it can analyze. Those, and files included as
<AdditionalFiles>are analyzed by the appropriate .NET project file analyzers.
Although the .net.csproj is not supposed to contain <Compile> items, the
compiler still generates output. Since that output is useless, it is hidden.
.net.csproj includes top level files and as such provides a solid alternative
to a Solution items folder.
.net.csproj
A .net.csproj project file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetProjectFile.Analyzers.Sdk" Version="*" PrivateAssets="all" />
</ItemGroup>
</Project>
Download this example .net.csproj
Enable analyzers for .net.csproj
The .NET project file analyzers can be included to the .net.csproj by adding
<ItemGroup>
<PackageReference Include="DotNetProjectFile.Analyzers" Version="*" PrivateAssets="all" />
</ItemGroup>
However, it is advised to add the reference in the Directory.Build.props file, or
Directory.Packages.props. In the latter case using a <GlobalPackageReference>:
<ItemGroup Label="Analyzers">
<GlobalPackageReference Include="DotNetProjectFile.Analyzers" Version="1.8.3" />
</ItemGroup>