在哪里放置MSBuild 15的规则集(Visual Studio 2017的构建工具)

时间:2017-11-14 20:39:56

标签: msbuild visual-studio-2017 code-analysis azure-pipelines msbuild-15

在我的构建中启用静态代码分析时,我收到MSBuild抛出的以下错误:

  

C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ BuildTools \ MSBuild \ 15.0 \ Bin \ Microsoft.CSharp.CurrentVersion.targets(134,9):警告MSB3884:找不到规则集文件" AllRules.ruleset"。 [C:\ Program Files \ VSTS Agent_work \ PATH_TO_MY.csproj]

要使静态代码分析在构建代理上工作(仅限MSBuild,不使用Visual Studio),对于旧版本的构建工具,可以:

  
      
  1. C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets\复制到构建服务器
  2.   
  3. 添加指向该文件夹的注册表项
  4.         

    (另见this bug report

但是,这些说明不适用于"Build Tools for Visual Studio 2017"。我想知道如何解决这个问题,而无需将规则集复制到我自己的存储库中或更改csproj中的任何内容?

更新:

我尝试过设置以下注册表项,但无济于事:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\15.0\Setup\EDev]
"StanDir"="C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\Team Tools\\Static Analysis Tools"

通过注册表,我发现以下注册表项。在我的机器上,我已经安装了VS2017构建工具和测试代理,这似乎指向后者,我在安装了构建工具后安装了它:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7]
"15.0"="C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\TestAgent\\"
"14.0"="C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\"

更新2:
两个答案都提到了文件Microsoft.CodeAnalysis.Targets。但是,我的构建服务器上不存在此文件。我尝试从VS2017安装中复制此文件夹:

from: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\VisualStudio\v15.0\CodeAnalysis
to: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\CodeAnalysis

然而,除了找不到规则集之外,它现在也会因无法找到Microsoft.WebApplication.targets而引发错误:

  

XXX.csproj(296,11):错误MSB4226:导入的项目" C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ BuildTools \ MSBuil   d \微软\ VisualStudio的\ V15.0 \ web应用\ Microsoft.WebApplication.targets"没找到。此外,试图找到" WebApplications \ Microsoft.WebApplication.targets"在$(VSToo。)的后备搜索路径中   lsPath) - " C:\ Program Files(x86)\ MSBuild \ Microsoft \ VisualStudio \ v15.0" 。这些搜索路径在" C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ BuildTools \ MSBuild \ 15.0 \ Bin \ MSBuild.exe.Config"中定义。    确认声明中的路径是正确的,并且该文件存在于其中一个搜索路径的磁盘上。

2 个答案:

答案 0 :(得分:2)

正式地说,您需要将Visual Studio安装到构建服务器上,因为这些规则来自的代码分析是Visual Studio的一部分,而不是MsBuild的一部分。

您仍然可以将Static Analysis目录复制到构建服务器,并在Additional Arguments选项中将以下参数提供给MsBuild:

/p:CodeAnalysisStaticAnalysisDirectory=c:\analysisdirectory

或者仅复制规则文件并指定此MsBuild参数,当您将规则文件放在源代码管理中时,这可能是最简单的:

/p:CodeAnalysisRuleDirectories=c:\analysisdirectory\rules

您可以选择编辑项目文件并在属性组中设置属性。

答案 1 :(得分:2)

更新

根据我的测试,您无需手动更改任何内容,只需安装Build Tools for VS2017的默认组件,然后将自动添加必要的文件。

enter image description here

旧:

关于代码分析文件的默认文件夹,您可以查看Microsoft.CodeAnalysis.targets文件:

<PropertyGroup>
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'=='' and Exists('$(VsInstallRoot)\Team Tools\Static Analysis Tools\')">$(VsInstallRoot)\Team Tools\Static Analysis Tools\</CodeAnalysisStaticAnalysisDirectory>
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'==''">$(Registry:HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\15.0\Setup\EDev@StanDir)</CodeAnalysisStaticAnalysisDirectory>
    <!-- If we didn't find the first registry path, assume we're in a 64bit process. -->
    <!-- read registry from Wow6432Node -->
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'==''">$(Registry:HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\VisualStudio\15.0\Setup\EDev@StanDir)</CodeAnalysisStaticAnalysisDirectory>

    <!-- if we didn't find the registry path yet, then try Win8 Express location -->
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'==''">$(Registry:HKEY_LOCAL_MACHINE\Software\Microsoft\VSWinExpress\15.0\Setup\EDev@StanDir)</CodeAnalysisStaticAnalysisDirectory>
    <!-- If we didn't find the registry path yet, then try Win8 Express 64-bit location -->
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'==''">$(Registry:HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\VSWinExpress\15.0\Setup\EDev@StanDir)</CodeAnalysisStaticAnalysisDirectory>

    <!-- If we didn't find the registry path yet, try WP Express locations -->
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'==''">$(Registry:HKEY_LOCAL_MACHINE\Software\Microsoft\VPDExpress\15.0\Setup\EDev@StanDir)</CodeAnalysisStaticAnalysisDirectory>
    <!-- If we didn't find the registry path yet, try WP Express 64-bit locations -->
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'==''">$(Registry:HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\VPDExpress\15.0\Setup\EDev@StanDir)</CodeAnalysisStaticAnalysisDirectory>

    <!-- If we didn't find the registry path yet, try WD Express locations -->
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'==''">$(Registry:HKEY_LOCAL_MACHINE\Software\Microsoft\WDExpress\15.0\Setup\EDev@StanDir)</CodeAnalysisStaticAnalysisDirectory>
    <!-- If we didn't find the registry path yet, try WD Express 64-bit locations -->
    <CodeAnalysisStaticAnalysisDirectory Condition="'$(CodeAnalysisStaticAnalysisDirectory)'==''">$(Registry:HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\WDExpress\15.0\Setup\EDev@StanDir)</CodeAnalysisStaticAnalysisDirectory>

    <!-- Default rule set search paths -->
    <CodeAnalysisRuleSetDirectories Condition=
            "'$(CodeAnalysisIgnoreBuiltInRuleSets)'!='true' and
             '$(CodeAnalysisStaticAnalysisDirectory)'!=''">$(CodeAnalysisRuleSetDirectories);$(CodeAnalysisStaticAnalysisDirectory)\Rule Sets</CodeAnalysisRuleSetDirectories>

    <!-- 
         In scenario where we run CA from command-line , we don't get "CodeAnalysisVSSku" variable passed by the IDE, so in case of Express-only installation
         we attempt to load rule sets that are only present on Pro and above - and fail. Let's assume we're in "Express" mode if the Pro+ rule set doesn't exist.
     -->
    <CodeAnalysisVSSku Condition="'$(CodeAnalysisVSSku)'=='' and !Exists('$(CodeAnalysisStaticAnalysisDirectory)\Rule Sets\NativeRecommendedRules.ruleset')">Express</CodeAnalysisVSSku>
  </PropertyGroup>

根据代码,如果您只安装Visual Studio 2017的构建工具,则只需将代码分析规则文件放在$(VsInstallRoot)\Team Tools\Static Analysis Tools文件夹中,$(VsInstallRoot)的值为C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools在构建服务器上。

总而言之,您只需将规则文件放在C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Team Tools\Static Analysis Tools文件夹中。