In a .NET PackageReference, what do IncludeAssets, ExcludeAssets and PrivateAssets control, and why do analyzer packages usually get PrivateAssets="all"?
answer
- consumed versus flowing to the parent
- IncludeAssets all, ExcludeAssets none by default
- PrivateAssets default: contentfiles, analyzers, build
- all keeps it out of the nuspec
- compile, runtime, build, analyzers, native
basics
~20 sIncludeAssets and ExcludeAssets decide which parts of a package this project consumes; PrivateAssets decides which parts stop here instead of flowing to projects or packages that depend on it. PrivateAssets="all" makes a development-only package invisible to consumers.
solid answer
~40 sA NuGet package carries several **asset** kinds: `compile` and `runtime` assemblies, `contentFiles`, `build` and `buildTransitive` props and targets, `analyzers` and `native` binaries. `IncludeAssets` (default `all`) minus `ExcludeAssets` (default `none`) is what **this** project consumes. `PrivateAssets` (default `contentfiles;analyzers;build`) lists the assets that are used here but **do not flow** to a parent — another project through `ProjectReference`, or consumers of the package you pack. With `PrivateAssets="all"` nothing flows: the package is not a dependency in the packed `.nuspec`, and a referencing project does not inherit it. That is the right setting for analyzers, source-generator polyfills such as `PolySharp` and versioning tools such as `MinVer`, which matter only while building this project.
code
xml · 7 lines<ItemGroup>
<!-- build-time only: nothing flows to consumers or into the nuspec -->
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.3" PrivateAssets="all" />
<!-- hand EF Core's build assets and analyzers on to consumers -->
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.12" PrivateAssets="none" />
</ItemGroup>go deeper
Recall that PrivateAssets="all" is what you put on analyzer and tooling packages so they do not leak to projects that use yours.
Explain the asset kinds, the three metadata items and their defaults, and how assets flow through ProjectReference and packed dependencies.
Use asset metadata deliberately in libraries: keep tools private, hand on build assets with none when consumers need them, and use ExcludeAssets runtime for host-supplied contracts.
Decide which build tooling a repository ships to its consumers versus keeps internal, and how central rules such as global references enforce that.
## What an asset is A NuGet package is more than a DLL. NuGet sorts its contents into **asset** groups, and the three metadata items on a `PackageReference` decide which groups are used and which are passed on. | Asset | Comes from | Effect | |---|---|---| | `compile` | `lib` / `ref` | assemblies you compile against | | `runtime` | `lib`, `runtimes` | assemblies copied to the output | | `contentFiles` | `contentFiles` | source or content files added to the project | | `build` | `build` | `.props` / `.targets` imported into this project | | `buildTransitive` | `buildTransitive` | props and targets meant to reach every consumer | | `analyzers` | `analyzers` | Roslyn analyzers and source generators | | `native` | `native` | native binaries | `all` and `none` are shorthands and must appear on their own; other values combine with semicolons. ## The three metadata items - **`IncludeAssets`** — what this project consumes. Default `all`. - **`ExcludeAssets`** — subtracted from the above. Default `none`. - **`PrivateAssets`** — what is consumed here but **does not flow** to a parent. Default `contentfiles;analyzers;build`. "Parent" means two things. In a solution, a project that has a `ProjectReference` to this one inherits this project's packages transitively. In packaging, the consumers of the `.nuspec` produced by `dotnet pack` inherit its dependencies. `PrivateAssets` governs both. The defaults explain everyday behaviour: if a library references a package, its consumers get the package's compile and runtime assets, but not its `build` targets or analyzers, while `buildTransitive` assets do flow — which is why package authors put consumer-facing targets there. ## Why development-only packages get PrivateAssets="all" An analyzer package such as `Microsoft.CodeAnalysis.PublicApiAnalyzers`, a versioning tool such as `MinVer`, or a polyfill generator such as `PolySharp` is only useful while this project builds; Serilog's own project, for one, references `PolySharp` with `PrivateAssets="All"`. With the default `PrivateAssets`, the package would still be listed as a dependency of the library you pack, and every consumer would download it. Setting `PrivateAssets="all"`: 1. keeps the package out of the packed `.nuspec` — NuGet's pack code skips any dependency whose private assets are `All`; 2. stops it flowing to projects that reference this one, so they neither restore it nor run its analyzers. NuGet's own `GlobalPackageReference` applies exactly this: `PrivateAssets="All"` plus `IncludeAssets` without `compile`. ## Following the flow through a solution Picture three projects in the accounting suite: `Ledger.Domain` references `Microsoft.EntityFrameworkCore` and `MinVer`; `Ledger.Api` has a `ProjectReference` to `Ledger.Domain`; and `Ledger.Domain` is also packed and published for other teams. - With default metadata, `Ledger.Api` gets EF Core's compile and runtime assets transitively and can use `DbContext` without its own reference, but EF Core's `build` assets and analyzers stay private to `Ledger.Domain`. - `MinVer` with `PrivateAssets="all"` stamps `Ledger.Domain`'s version and stops there: `Ledger.Api` never restores it, and the published package lists no MinVer dependency. - If `MinVer` had default metadata, the package would appear as a dependency of the published library, and every consumer would download a tool it never runs. ## The opposite choice Sometimes a library wants consumers to receive what the default withholds. The Npgsql EF Core provider references `Microsoft.EntityFrameworkCore` with `PrivateAssets="none"`, so EF Core's build assets and analyzers reach projects that use the provider. And `ExcludeAssets="runtime"` is the classic plugin pattern: compile against a contracts package but do not copy it, because the host supplies it at run time. ## .NET 10 and pruning In projects targeting .NET 10, NuGet prunes packages that the shared framework already provides. When a direct `PackageReference` falls within the pruning range, NuGet applies `PrivateAssets="all"` and `IncludeAssets="none"` to it and raises **NU1510** if the reference can be removed entirely — the same metadata, used by NuGet itself to neutralise a reference that does nothing. ## Summary for an interview - Include and Exclude are about this project; Private is about everyone downstream. - Defaults keep build logic and analyzers private, compile and runtime public. - `all` makes a package a private build tool; `none` hands everything on.
- If a library references a package with default metadata, do consumers get the package's analyzers?No. The default `PrivateAssets` is `contentfiles;analyzers;build`, so analyzers, build props and targets and content files stay with the library, while compile and runtime assets flow. A package that wants its targets to reach consumers ships them in `buildTransitive`, or the library sets `PrivateAssets="none"`.
- What is the difference between ExcludeAssets="all" and PrivateAssets="all"?`ExcludeAssets="all"` means this project consumes nothing from the package. `PrivateAssets="all"` means this project consumes the package normally but passes nothing on, so it disappears from the packed dependencies and from projects that reference this one.
- PrivateAssets controls which assets the current project itself can use.
- By default a package's analyzers flow to every project that references the library.
- A package marked PrivateAssets="all" is still listed in the packed nuspec.
- IncludeAssets defaults to compile only, so runtime assemblies must be opted in.
- ExcludeAssets="runtime" removes the package from compilation as well.
Assumes NuGet 7 with the .NET 10 SDK; the asset metadata and its defaults are long-standing, while pruning that applies PrivateAssets="all" and IncludeAssets="none" to framework-provided references (NU1510) is new for projects targeting .NET 10.