Kzu's Today I Learned
GitHubX/TwitterBlog
  • Today I Learned
  • dotnet
    • How to emit descriptions for exported JSON schema using JsonSchemaExporter
    • NuGet
      • Suppress dependencies when packing
      • Hide contentFiles from your nuget packages
      • Packaging transitive analyzers with NuGet
      • How to add search to static nuget feed
      • Populate RepositoryBranch in CI for NuGet Pack
    • Ignore folder from dotnet-format
    • Accessing Tor .onion URLs via HttpClient with .NET6
    • Installing .NET 5.0 on Raspberry Pi 4
    • Quickly check C# compiler and language version
    • Disable diagnostic analyzers for entire folder/submodules
    • Persisting output files from source generators
    • Use C# 9 records in non-net5.0 projects
    • AsyncLocal never leaks and is safe for CallContext-like state
    • Using HashCode in .NETFramework
    • How to locate dotnet
  • testing
    • Conditional unit tests
    • Skip tagged scenarios in SpecFlow with Xunit
  • msbuild
    • How to get user home dir ~ cross-platform
    • Modifying the build for every solution in a repository
    • Detect CI builds for every CI system
    • Modify all command-line builds in entire repo
    • Write entire XML fragments in MSBuild with XmlPoke
    • How to select first item in an ItemGroup
    • How to include commit URL in nuget package description
    • How to include package reference files in your nuget
    • How to build project when content files change
  • azure
    • How to launch multiple Azure Functions apps on different ports
    • C# script function apps beyond Azure portal
    • Publishing function app from GitHub folder
    • Exploring Azure Data with Kusto and Dashboards
    • Shared secret authorization with Azure SignalR Service
    • Using Azure File Copy from DevOps yaml pipeline
    • Code-less redirection with serverless Azure Functions
  • DevOps/CI/CD
    • How to run Azure Storage unit tests in CI
    • How to skip steps or jobs in GitHub Actions for PRs from forks
    • Update version and publish npm from GH
    • Push to protected branch from GitHub actions
Powered by GitBook
On this page
  1. dotnet
  2. NuGet

Packaging transitive analyzers with NuGet

PreviousHide contentFiles from your nuget packagesNextHow to add search to static nuget feed

Last updated 3 years ago

Consider the scenario of and its referenced packages: the main package is essentially a meta-package so that anyone wanting to leverage all the codegen in all the ThisAssembly.* packages can reference a single one. By default, NuGet pack will create a package that declares the project reference dependencies like so:

    <dependencies>
      <group targetFramework=".NETStandard2.0">
        <dependency id="ThisAssembly.AssemblyInfo" version="42.42.42" exclude="Build,Analyzers" />
        <dependency id="ThisAssembly.Metadata" version="42.42.42" exclude="Build,Analyzers" />
        <dependency id="ThisAssembly.Project" version="42.42.42" exclude="Build,Analyzers" />
        <dependency id="ThisAssembly.Strings" version="42.42.42" exclude="Build,Analyzers" />
      </group>
    </dependencies>

Note all those exclude. Not good since now it means projects referencing this package will not get the transitive analyzers, which are precisely the point of this meta-package.

The fix is : you must explicitly state that none of the referenced project assets are to be flagged as private:

  <ItemGroup>
    <ProjectReference Include="../ThisAssembly.AssemblyInfo/ThisAssembly.AssemblyInfo.csproj" PrivateAssets="none" />
    <ProjectReference Include="../ThisAssembly.Metadata/ThisAssembly.Metadata.csproj" PrivateAssets="none" />
    <ProjectReference Include="../ThisAssembly.Project/ThisAssembly.Project.csproj" PrivateAssets="none" />
    <ProjectReference Include="../ThisAssembly.Strings/ThisAssembly.Strings.csproj" PrivateAssets="none" />
  </ItemGroup>

This properly allows the transitive build and analyzer assets to be properly installed on the referencing project.

ThisAssembly
highly non-obvious