Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing a code analysis ruleset to a .net core project through NuGet

I've run into a problem when upgrading a .NET 4.6 project to .NET Core 2.0. All our projects use a custom StyleCop ruleset which is provided by a NuGet package. The ruleset is in a file called custom.ruleset and lives in the content folder inside the package. All our projects consume this package and so get a copy of custom.ruleset.

However, in Core 2.0 and Standard 2.0 projects this doesn't work. Files are no longer copied from the content folder of a package, and we're told to use the contentFiles folder instead.

I have a nuspec that now looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <version>1.0.11</version>      
  <metadata>
    ...
    <contentFiles>
      <files include="content\*.ruleset" buildAction="None" copyToOutput="false" flatten="true"/>
    </contentFiles>
  </metadata>
  <files>
    <file src="content\**" target="contentFiles/any/any" />
  </files>
</package>

With this structure the ruleset appears in Visual Studio under the project, but trying to reference it from the project's .csproj file with <CodeAnalysisRuleSet>custom.ruleset</CodeAnalysisRuleSet> silently fails and reverts to using the default ruleset. I can force it to work by adding <CodeAnalysisRuleSet>$(NuGetPackageRoot)CustomRuleset\1.0.11\contentFiles\any\any\custom.ruleset</CodeAnalysisRuleSet> but this means the csproj will need updating whenever the ruleset changes, so it may as well be a manual process. Any ideas how to fix this?

like image 249
Meeji Avatar asked Sep 08 '17 11:09

Meeji


People also ask

How to set custom code analysis rule set in ASP NET Core?

To set a custom code analysis rule set on an ASP.NET Core project, right click on the project in solution explorer, select Edit <YourProjectName>.csproj, and then inside <PropertyGroup> add a new tag called CodeAnalysisRuleSet containing a path to the custom ruleset. The path can be a full path or a path relative to the project file.

What is a NuGet analyzer package?

A collection of .Net Analyzer NuGet Packages that help apply shift left mentality of security. This package contains configuration files to help you writing code according to Hagleitner's software development standards. Adds and enables code style analyzers with the Phimath coding style. PowerShell for every system!

How to create a code coverage report in NET Core?

Code Coverage in .NET Core Projects 1 Creating the Coverage Report. Install the NuGet package coverlet.msbuild to the MSTest project using the following command. ... 2 Publishing the Coverage Report. Many good coverage report visualization tools are out there. ... 3 Alternatively: Using the ReportGenerator. ...

Where does the ruleset appear in Visual Studio code analysis?

With this structure the ruleset appears in Visual Studio under the project, but trying to reference it from the project's .csproj file with <CodeAnalysisRuleSet>custom.ruleset</CodeAnalysisRuleSet> silently fails and reverts to using the default ruleset.


1 Answers

The idea is to not try to deploy the file as content but add build logic to the NuGet package.

Make sure that the package is structured in the following way:

  • build\
  • build\custom.ruleset
  • build\{YourPackageName}.targets (e.g. CustomRuleset.targets)

This structure causes the .targets file to be automatically imported into the consuming project by convention.

The .targets file should then contain:

<Project>
  <PropertyGroup>
    <CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)custom.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
</Project>

This will cause the project's ruleset property to be overwritten to the location relative to the .targets file.

Note that this also applies to .net framework projects using the new PackageReference style of NuGet packages (replacement of packages.config) which is opt-in in VS 2017 (15.2+).

like image 104
Martin Ullrich Avatar answered Sep 29 '22 19:09

Martin Ullrich