If projects in your solution have a hierarchical structure, meaning they reference each other and have a common, most abstract base that is root in the structure similar to this:
Common
├── Worker
└── Persistence
└── API
...then you can reference StyleCop.Analyzers
package in your root project and set the value of <PrivateAssets>
tag to none
:
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>none</PrivateAssets>
</PackageReference>
</ItemGroup>
all
is set by default when you add the StyleCop.Analyzers
package reference to your project. What does it mean?
You might be using a dependency purely as a development harness and might not want to expose that to projects that will consume your package. In this scenario, you can use the PrivateAssets metadata to control this behavior. ~ Docs
So, by default StyleCop.Analyzers
will only be enabled in the project in which you explicitly reference the package. But in almost all code bases I've been part of, enforcing the StyleCode rules in all projects in solution was desired (almost all, except for projects with auto-generated code e.g. EF migrations). Changing all
to none
in the package reference metadata will result in passing down the styling rules to all projects that depend on it.
Solution
Summarizing, the root project Common will have to reference StyleCop.Analyzers
package while setting <PrivateAssets>
to none
:
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>none</PrivateAssets>
</PackageReference>
</ItemGroup>
and Worker or Persistence or any other dependent project will only have to reference the previous layer, which is common practice in any layered architecture anyway:
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>
treatwarningsaserrors
or even turn on strict. Yes I inherited legacy code...and want to search for dead code with this ruleset #30974933 – Rectrix