MSBuild 15 规则集的放置位置(Visual Studio 2017 的构建工具)
Where to put Rule Sets for MSBuild 15 (Build Tools for Visual Studio 2017)
在我的构建上启用静态代码分析时,MSBuild 抛出以下错误:
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\Microsoft.CSharp.CurrentVersion.targets(134,9): warning MSB3884: Could not find rule set file "AllRules.ruleset". [C:\Program Files\VSTS Agent_work\PATH_TO_MY.csproj]
要让静态代码分析在构建代理上运行(仅限 MSBuild,无 Visual Studio),对于旧版本的构建工具,可以:
- Copy
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets\
to the build server
- Add a registry entry pointing to that folder
(see also this bug report)
但是,这些说明不适用于 "Build Tools for Visual Studio 2017"。我想知道如何解决这个问题,而不需要将规则集复制到我自己的存储库中或更改我的 csproj 中的任何内容?
更新:
我已尝试设置以下注册表项,但无济于事:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio.0\Setup\EDev]
"StanDir"="C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Team Tools\Static Analysis Tools"
通过注册表挖掘,我发现了以下注册表项。在我的机器上我已经安装了 VS2017 Build Tools 和 Test Agent,这似乎指向后者,我在安装 Build Tools 之后安装了它:
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\"
更新二:
两个答案都提到了文件 Microsoft.CodeAnalysis.Targets
。但是我的构建服务器上没有这个文件。我试过从我的 VS2017 安装复制这个文件夹:
from: C:\Program Files (x86)\Microsoft Visual Studio17\Community\MSBuild\Microsoft\VisualStudio\v15.0\CodeAnalysis
to: C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\CodeAnalysis
然而,除了找不到规则集,它现在还会抛出无法找到的错误 Microsoft.WebApplication.targets
:
XXX.csproj(296,11): error MSB4226: The imported project "C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuil
d\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found. Also, tried to find "WebApplications\Microsoft.WebApplication.targets" in the fallback search path(s) for $(VSToo
lsPath) - "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0" . These search paths are defined in "C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild.exe.Config".
Confirm that the path in the declaration is correct, and that the file exists on disk in one of the search paths.
正式而言,您需要将 Visual Studio 安装到构建服务器上,因为这些规则来自的代码分析是 Visual Studio 的一部分,而不是 MsBuild 的一部分。
您仍然可以将静态分析目录复制到您的构建服务器,并在附加参数选项中向 MsBuild 提供以下参数:
/p:CodeAnalysisStaticAnalysisDirectory=c:\analysisdirectory
或者只复制规则文件并指定此 MsBuild 参数,当您将规则文件放入源代码管理时,这可能是最简单的方法:
/p:CodeAnalysisRuleDirectories=c:\analysisdirectory\rules
您可以选择编辑项目文件并在 属性 组中设置 属性。
更新:
根据我的测试,您无需手动更改任何内容,只需安装 Build Tools for VS2017 的默认组件,然后会自动添加必要的文件。
旧:
关于代码分析文件的默认文件夹,可以查看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.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.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.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.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.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.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.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.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>
根据代码,只需要将代码分析规则文件放在$(VsInstallRoot)\Team Tools\Static Analysis Tools
文件夹下,$(VsInstallRoot)
的值为C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools
如果你只安装Build Tools for Visual Studio 2017 在构建服务器上。
总而言之,您只需将规则文件放在 C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\Team Tools\Static Analysis Tools
文件夹中即可。
在我的构建上启用静态代码分析时,MSBuild 抛出以下错误:
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\Microsoft.CSharp.CurrentVersion.targets(134,9): warning MSB3884: Could not find rule set file "AllRules.ruleset". [C:\Program Files\VSTS Agent_work\PATH_TO_MY.csproj]
要让静态代码分析在构建代理上运行(仅限 MSBuild,无 Visual Studio),对于旧版本的构建工具,可以:
- Copy
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets\
to the build server- Add a registry entry pointing to that folder
(see also this bug report)
但是,这些说明不适用于 "Build Tools for Visual Studio 2017"。我想知道如何解决这个问题,而不需要将规则集复制到我自己的存储库中或更改我的 csproj 中的任何内容?
更新:
我已尝试设置以下注册表项,但无济于事:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio.0\Setup\EDev]
"StanDir"="C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Team Tools\Static Analysis Tools"
通过注册表挖掘,我发现了以下注册表项。在我的机器上我已经安装了 VS2017 Build Tools 和 Test Agent,这似乎指向后者,我在安装 Build Tools 之后安装了它:
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\"
更新二:
两个答案都提到了文件 Microsoft.CodeAnalysis.Targets
。但是我的构建服务器上没有这个文件。我试过从我的 VS2017 安装复制这个文件夹:
from: C:\Program Files (x86)\Microsoft Visual Studio17\Community\MSBuild\Microsoft\VisualStudio\v15.0\CodeAnalysis
to: C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\CodeAnalysis
然而,除了找不到规则集,它现在还会抛出无法找到的错误 Microsoft.WebApplication.targets
:
XXX.csproj(296,11): error MSB4226: The imported project "C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuil d\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found. Also, tried to find "WebApplications\Microsoft.WebApplication.targets" in the fallback search path(s) for $(VSToo lsPath) - "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0" . These search paths are defined in "C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild.exe.Config". Confirm that the path in the declaration is correct, and that the file exists on disk in one of the search paths.
正式而言,您需要将 Visual Studio 安装到构建服务器上,因为这些规则来自的代码分析是 Visual Studio 的一部分,而不是 MsBuild 的一部分。
您仍然可以将静态分析目录复制到您的构建服务器,并在附加参数选项中向 MsBuild 提供以下参数:
/p:CodeAnalysisStaticAnalysisDirectory=c:\analysisdirectory
或者只复制规则文件并指定此 MsBuild 参数,当您将规则文件放入源代码管理时,这可能是最简单的方法:
/p:CodeAnalysisRuleDirectories=c:\analysisdirectory\rules
您可以选择编辑项目文件并在 属性 组中设置 属性。
更新:
根据我的测试,您无需手动更改任何内容,只需安装 Build Tools for VS2017 的默认组件,然后会自动添加必要的文件。
旧:
关于代码分析文件的默认文件夹,可以查看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.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.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.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.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.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.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.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.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>
根据代码,只需要将代码分析规则文件放在$(VsInstallRoot)\Team Tools\Static Analysis Tools
文件夹下,$(VsInstallRoot)
的值为C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools
如果你只安装Build Tools for Visual Studio 2017 在构建服务器上。
总而言之,您只需将规则文件放在 C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\Team Tools\Static Analysis Tools
文件夹中即可。