3

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
Mateusz Moska
  • 1,921
  • 1
  • 14
  • 18

1 Answers1

6

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.

enter image description here

Then add this package to the test project:

enter image description here

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.

enter image description here

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • We were trying to avoid copying ruleset file to each project. Developers tend to change it and push to repository ;) I think it's a Visual Studio bug that it cannot see it in the proper location, because build is seeing proper ruleset. – Mateusz Moska Dec 21 '17 at 10:31
  • @MateuszMoska, 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. With a sample test, it works fine on my side. You can check my updated answer for detail. – Leo Liu Dec 22 '17 at 07:14
  • @MateuszMoska, Any update for this issue? Have you resolved your issue? If not, please let me know the latest status for this issue. – Leo Liu Dec 27 '17 at 01:54
  • Sorry but not, shared folder is not the solution neither. We had to go with version with file in content, as we cannot waste more time on that. Other solution I've seen works, is with editing csproj file with install.ps1 script and put the file into solution as a link from package, but from what I've read, the nuget team is moving away from those scripts. Also adding path in property group to ruleset file directly, not with the .props import work. I think it's a bug in Visual Studio, I've reported it, will see result later. Thanks for your professional help. – Mateusz Moska Dec 27 '17 at 16:42
  • @MateuszMoska, that is OK. Since you have other solution, would you please share your solution with a answer here and mark it, so it could help other community members who get the same issues. – Leo Liu Dec 29 '17 at 02:48
  • We used solution with Content finally, we couldn't afford on loosing more time on that issue. But I've reported bug to VS team so maybe they will solve it in one of next updates :) – Mateusz Moska Jan 16 '18 at 14:29
  • 1
    One caveat as to why this was not working on my end... you [must name the props file to match the name of the NuGet package](https://learn.microsoft.com/en-us/nuget/create-packages/creating-a-package#including-msbuild-props-and-targets-in-a-package) such as: .props – felickz May 31 '18 at 13:07