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?
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.
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!
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. ...
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.
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:
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+).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With