本文介绍了代码分析不适用于来自 nuget 包(来自 .props)的规则集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我们的 nuget 包中使用自定义规则集文件.我已经添加到包 .props 文件的 build 文件夹中:

<属性组><代码分析规则集>$(MSBuildThisFileDirectory)..\My.Shared.ruleset</代码分析规则集><RunCodeAnalysis>true</RunCodeAnalysis></PropertyGroup></项目>

规则集文件在包根目录下,路径正确,将.props文件导入csproj文件.

<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')"/><导入项目="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>...

但是 Visual Studio 没有看到正确的规则集.当我从 References -> Analyzers 打开活动规则集时,它指向不同的文件:MinimumRecommendedRules.ruleset 并且它使用的是这个文件中的规则而不是我的自定义规则.

  • Visual Studio 社区 2017 版本 15.5.0
  • 项目目标框架 4.6.1

解决方案

你应该在包文件夹的content文件夹中设置规则集文件,这样VS/MSBuild就会把这个文件添加到你的项目中,然后VS\MSBuild就可以改变默认的MinimumRecommendedRules.ruleset 到您的 My.Shared.ruleset.ruleset 文件.

例如,您的 NuGet 源文件夹结构可能如下所示(My.Shared.ruleset"是您的包 ID):

  • 构建

    • My.Shared.ruleset.props
  • 内容

    • My.Shared.ruleset.ruleset

My.Shared.ruleset.props 的内容如下:

<属性组><RunCodeAnalysis>true</RunCodeAnalysis><CodeAnalysisRuleSet>My.Shared.Rulesets.ruleset</CodeAnalysisRuleSet></PropertyGroup></项目>

创建nuget包的步骤:

  • 打开

    然后将此包添加到测试项目中:

    然后您会发现活动规则集已更改为来自 nuget 包的规则集.

    评论更新:

    我们试图避免将规则集文件复制到每个项目.开发人员倾向于更改它并推送到存储库

    如果不想将规则集文件复制到每个项目中,只需更改.props文件中的路径即可,但应确保路径正确.props 文件,例如.我在本地路径中设置了 .ruleset 文件:D:\ShareFolder\My.Shared.Rulesets.ruleset,然后从 nuget 包中移动 .ruleset 并更改.props 中 D:\ShareFolder\My.Shared.Rulesets.ruleset 的路径.

    希望这会有所帮助.

    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

    解决方案

    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->EditMetadata, 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:

    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.

    这篇关于代码分析不适用于来自 nuget 包(来自 .props)的规则集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 21:10