Transitive Dependencies & Assets
You reference one package; you get seven. Every package's own dependencies (and theirs) flow into your project as transitive dependencies, and so do the packages of every project you reference. This lesson shows how to see that graph, how to ask why a package is in it, and how to stop a dependency from leaking to your consumers.
Seeing the graph
mkdir tests && cd tests
cat > tests.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.9.3" />
</ItemGroup>
</Project>
XML
dotnet restore -v:q
dotnet package list --include-transitive
dotnet nuget why tests.csproj xunit.abstractions Determining projects to restore...
All projects are up-to-date for restore.
Project 'tests' has the following package references
[net10.0]:
Top-level Package Requested Resolved
> xunit 2.9.3 2.9.3
Transitive Package Resolved
> xunit.abstractions 2.0.3
> xunit.analyzers 1.18.0
> xunit.assert 2.9.3
> xunit.core 2.9.3
> xunit.extensibility.core 2.9.3
> xunit.extensibility.execution 2.9.3
Project 'tests' has the following dependency graph(s) for 'xunit.abstractions':
[net10.0]
└── xunit (v2.9.3)
└── xunit.core (v2.9.3)
├── xunit.extensibility.core (v2.9.3)
│ └── xunit.abstractions (v2.0.3)
└── xunit.extensibility.execution (v2.9.3)
└── xunit.extensibility.core (v2.9.3)
└── xunit.abstractions (v2.0.3)
dotnet package list(ordotnet list packagebefore .NET 10) restores if needed, then shows the top-level packages (with the requested range and resolved version) and, with--include-transitive, everything below them. Thexunitpackage itself is almost empty: it's a metapackage that pulls in the pieces.dotnet nuget why <project> <package>prints every path through which a package enters the graph. It's the first command to run when a vulnerable or unexpected package shows up.- Add
--format jsonto either list command for machine-readable output in CI scripts.
Outdated and vulnerable packages
The same command can compare your versions with what the sources offer:
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
</ItemGroup>
</Project>
XML
dotnet restore -v:q
dotnet package list --outdated Determining projects to restore...
All projects are up-to-date for restore.
The following sources were used:
/opt/nuget-feed
Project `app` has the following updates to its packages
[net10.0]:
Top-level Package Requested Resolved Latest
> Microsoft.NET.Test.Sdk 17.14.1 17.14.1 18.0.1
> Newtonsoft.Json 13.0.3 13.0.3 13.0.4
--outdated asks every configured source for newer versions, so here it ran against the local feed. Add --include-prerelease to consider previews, or --highest-minor/--highest-patch to stay within a major version.
--vulnerable works the same way but needs vulnerability data, which nuget.org publishes and a plain folder feed doesn't. Run against the local feed it reports "no vulnerable packages given the current sources", which only means "no data". On a connected machine:
$ dotnet package list --vulnerable --include-transitivelists packages with known advisories, their severity and an advisory URL (sample command, not run here). Restore also checks this data on its own since .NET 8 and raises NU1901–NU1904 warnings (low to critical), including for transitive packages since .NET 9. Treating NU1903;NU1904 as errors in CI is a cheap safety net.
Controlling what flows: PrivateAssets, IncludeAssets, ExcludeAssets
A package contributes several kinds of assets: compile (reference assemblies), runtime (DLLs copied to bin/), build (props and targets imported into your build), analyzers, contentFiles and native. Metadata on the PackageReference controls them:
| Metadata | Effect |
|---|---|
PrivateAssets="all" |
use the package here, but don't flow it to projects or packages that depend on you |
IncludeAssets="compile;runtime" |
only consume those asset types |
ExcludeAssets="build" |
consume everything except those |
The classic use of PrivateAssets="all" is a build-time-only package: analyzers, SourceLink, source generators, versioning tools. Your consumers shouldn't inherit them. Watch the difference between two references from a library:
mkdir -p Lib App
cat > Lib/Lib.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" PrivateAssets="all" />
</ItemGroup>
</Project>
XML
cat > App/App.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Lib/Lib.csproj" />
</ItemGroup>
</Project>
XML
echo 'public static class Greeter { }' > Lib/Greeter.cs
echo 'System.Console.WriteLine("app");' > App/Program.cs
dotnet restore App -v:q
dotnet package list --project App --include-transitive | sed -n '/Transitive/,$p'
dotnet build App -nologo -v:q > /dev/null
ls App/bin/Debug/net10.0 | grep -E '\.dll