简体   繁体   中英

Code Analysis is not working with ruleset from nuget package (from .props)

I'm trying to use custom ruleset file form our nuget package. I've added to the build folder of the package .props file:

<Project>
  <PropertyGroup>
    <CodeAnalysisRuleSet>
      $(MSBuildThisFileDirectory)..\My.Shared.ruleset
    </CodeAnalysisRuleSet>
    <RunCodeAnalysis>true</RunCodeAnalysis>
  </PropertyGroup>
</Project>

Rule set file is in the package root folder, the paths are correct, it's adding import of the .props file into csproj file.

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\packages\My.Shared.Rulesets.1.0.0.7118\build\My.Shared.Rulesets.props" Condition="Exists('..\packages\My.Shared.Rulesets.1.0.0.7118\build\My.Shared.Rulesets.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 and it's using rules from this file not my custom one.

  • Visual Studio Comunity 2017 Version 15.5.0
  • Project Target framework 4.6.1

Code Analysis is not working with ruleset from nuget package (from .props)

You should set the Rule set file in the content folder in the package folder, so that VS/MSBuild will add this file to your project, then VS\\MSBuild could change the default MinimumRecommendedRules.ruleset to your My.Shared.ruleset.ruleset file.

For example, your NuGet source folder structure might look like this ("My.Shared.ruleset" is your package ID):

  • build

    • My.Shared.ruleset.props
  • content

    • My.Shared.ruleset.ruleset

where the contents of My.Shared.ruleset.props are something like the following:

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

Step to create nuget package:

  • Open NuGet Package Explorer , select new a package, Edit->Edit Metadata, change Id to My.Shared.Rulesets ->Save.

  • Content->Add->Content Folder->Add Existing file->Select your My.Shared.Rulesets.ruleset

  • Content->Add->Build Folder->Add Existing file->Select your My.Shared.ruleset.props ->Save.

在此处输入图片说明

Then add this package to the test project:

在此处输入图片说明

Then you will find the active ruleset was changed to the one from nuget package.

Update for comment:

We were trying to avoid copying ruleset file to each project. Developers tend to change it and push to repository

If you do not want to copy ruleset file to each project, you just need to change the path in the .props file, but you should make sure the path is correct in the .props file , for example. I set the .ruleset file in the local path: D:\\ShareFolder\\My.Shared.Rulesets.ruleset , then move the .ruleset from the nuget package and change the path to D:\\ShareFolder\\My.Shared.Rulesets.ruleset in the .props.

在此处输入图片说明

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM