问题
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
回答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.
来源:https://stackoverflow.com/questions/47892974/code-analysis-is-not-working-with-ruleset-from-nuget-package-from-props