0

I'm making a nuget package which only includes references to a bunch of analyzers and a ruleset file.

I am struggling with getting it to add the <CodeAnalyzersRuleSet> tag to the .csproj file during package install.

After searching for a few hours I stumbled upon this 5 year old question which attempts to solve the same thing but I can't get it to work.

I've configured my NuGet project as follows:

Foo.csproj file:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <!-- Author, Description, ect removed for brevity -->
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="AsyncFixer" Version="1.6.0">
            <IncludeAssets>analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
            <IncludeAssets>analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
            <IncludeAssets>analyzers</IncludeAssets>
        </PackageReference>
    </ItemGroup>

</Project>

build\Foo.targets file:

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

And then the build\Foo.ruleset itself.

I run dotnet pack (dotnet version 7.0.102) in order to create the nuget package.

During package install the only thing that happens is that the package is added like a normal <PackageReference>, and I get all the analyser warnings but it doesn't add the ruleset file, and it doesn't add the <CodeAnalyzerRuleSet> tag to the .csproj file.

I've inspected the nuget package and it doesn't include the .targets and .ruleset files unless I add the following to my .csproj file as well:

<ItemGroup>
    <None Include="build\**" Pack="True" PackagePath="build\" />
</ItemGroup>

This adds the files to the .nupkg but they are still not added or applied to my project during package install.

Any help or pointers are greatly appreciated.

Asser
  • 109
  • 1
  • 9
  • Have you made sure that the `.targets` file is named exactly like your package? You could use `` to make sure of that and prevent it from breaking on your next package rename. – svenhuebner Jan 29 '23 at 13:10

0 Answers0