0

I'm trying to use custom ruleset file from nuget package. I added given .props to the build folder of the package:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <CodeAnalysisRuleSet>MyProject.CodeAnalysis.ruleset</CodeAnalysisRuleSet>
        <RunCodeAnalysis>true</RunCodeAnalysis>
    </PropertyGroup>
</Project>

After installing nuget package to project I see that rule set file is in the package root folder, the paths are correct:

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\packages\MyProject.CodeAnalysis.3.0.0\build\MyProject.CodeAnalysis.props" Condition="Exists('..\packages\MyProject.CodeAnalysis.3.0.0\build\MyProject.CodeAnalysis.props')" />
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

But Visual Studio is not seeing correct rule set. When I open active rule set from References -> Analyzers, it's pointing to different file: MinimumRecommendedRules.ruleset. So it's using rules from this file not from my one.

My .nuspec file incldes this code:

<files>
   <file src="MyProject.CodeAnalysis.ruleset" target="content/StyleCop" />
   <file src="build\**\*.*" target="build" />
</files>

Structure of nuget package in Nuget Package Explorer looks like this:

  • build
    • MyProject.CodeAnalysis.props
  • content
    • StyleCop
      • MyProject.CodeAnalysis.ruleset

My configuration is:

  • Visual Studio 2019, ver. 16.6.0
  • Project Target: .NET Framework v4.7.2

What I do wrong?

P.S. I see this posts, but they didn't help me:

  1. Code Analysis is not working with ruleset from nuget package (from .props)
  2. Add code analysis ruleset through nuget package

1 Answers1

0

Code analysis rule set is not updated from nuget package

I think the main issue is that you should use MyProject.CodeAnalysis.targets file in the build folder rather than MyProject.CodeAnalysis.props file.

As this document said,

.props is added at the top of the project file; .targets is added at the bottom.

So when you use .props file, it will read the custom rulset file before you imported the file into your project, so you will miss that file.

To solve it, you should use .targets file.

Solution

Update 1

1) change to use MyProject.CodeAnalysis.targets file in the build folder rather than MyProject.CodeAnalysis.props .

enter image description here

2) add your code in this file

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <CodeAnalysisRuleSet>MyProject.CodeAnalysis.ruleset</CodeAnalysisRuleSet>
        <RunCodeAnalysis>true</RunCodeAnalysis>
    </PropertyGroup>
</Project>

3) Instead, use this in your .nuspec file.

<files>
   <file src="MyProject.CodeAnalysis.ruleset" target="content" />
   <file src="build\**\*.*" target="build" />
</files>

4) Then you pack your nuget project and when you install it in other main project, you should build the project first, and then you can find that the new ruleset file is enabled automatically under References --> Analyzers.

This shows correctly in Analyzers:

enter image description here

In addition, when you try to create a new version by suggestions, you should first, clean your global nuget cache first. Usually, delete all files under C:\Users\xxx(current user)\.nuget\packages.

Besides, use a new version called 1.1.1 in .nuspec file.

<version>1.1.1</version>

Then create a new net framework project to install the new version of the package to test again.

Any concern and feedback will be expected. We are looking forward to hearing from you.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • I tried what you suggested, but unfortunately still have the same result: MinimumRecommendedRules.ruleset is active. Do you have any suggestions? – Jeremis May 26 '20 at 06:36
  • Actually, your project already used the new ruleset. And you can Click `References`-->`Analyzers` and you can see that it uses the new `MyProject.CodeAnalysis.ruleset`file just as I showed. First, please clean the nuget cache,delete all files under `C:\Users\xxx\.nuget\packages`. Second, when you use my function to pack this project, please try other version like `2.0.0` or `3.0.0` to separate from the previous version of the project. Then create a new net framework project 4.7.2 main project to install such new nuget version to test it. – Mr Qian May 26 '20 at 11:14