Search code examples
msbuildcode-analysisfxcopstylecop

Any idea why MSBuild would suddenly start performing code analysis on other projects in my solution?


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:

  1. Disable all warnings in ca.ruleset
  2. Enable one
  3. Fix all instances
  4. Re-build the solution to verify the fix
  5. GOTO 2

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:

  • The csproj file of the unit test project has no references to either FxCop or StyleCop.
  • Nor does it refer in any way to ruleset.ca
  • dotnet clean doesn't help
  • Checking out another branch (one without any CA turned on) stops analysis for both projects (as expected), but switching back results in them both being analysed again.
  • I'm using VS Code, so can't play around with any of the GUI settings that people seem (according to my Googling) to suggest for CA issues
  • My .sln file doesn't appear to have any reference to CA at all - it's just the usual project and build definitions.

I'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.


Solution

  • 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.