I've been busy turning on code analysis for one of our solutions. On Friday everything was going well.
I had added the following to one of our csproj files:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<CodeAnalysisRuleSet>ca.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
</ItemGroup>
And spend some time on Friday working through the warnings, like this:
This had been working really well. I came in this morning and picked up where I left off, except that when I got to step 4 (rebuilding the solution), I suddenly had 3k+ warnings from our unit test project.
I'm not sure why code analysis is now being applied to this other project and I can't seem to disable it.
Some observations:
dotnet clean
doesn't helpI'm beginning to think this must be an issue with MSBuild.
Has anyone ever seen this before? What was the fix?
Update: Have filed a bug with the MSBuild team.
As per the bug report, this seems to be a general MSBuild thing and unrelated to any particular environment. For now I'm just going to add a blanket ignore
ruleset, but it's hardly ideal.
Found a workaround via my bug report.
Nick Guerra suggested adding PrivateAssets="All"
to the PackageReference
statement in the csproj file of my main project.
In other words, for me, this:
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
Became this:
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.2" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="All" />
A quick dotnet clean && dotnet build
later, and all those warnings vanish!
It seems as though this attribute enforces what should already be the default behaviour: that analysers should be treated as being private to the project to which they are directly added. Relevant bug.