TExcelTAD
Home › .NET Builds: MSBuild & NuGet › Transitive Dependencies & Assets

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

bash · try it yourself
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 (or dotnet list package before .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. The xunit package 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 json to 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:

bash · try it yourself
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:

bash
$ dotnet package list --vulnerable --include-transitive

lists 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:

bash · try it yourself
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
  

   Transitive Package      Resolved
   > Newtonsoft.Json       13.0.4

App.dll
Lib.dll
Newtonsoft.Json.dll

Newtonsoft.Json flowed through the ProjectReference into App (it's in App's graph and its DLL was copied to App's output). Humanizer.Core stayed private to Lib: it isn't in App's graph, and its DLL didn't reach App's bin/. If Lib calls Humanizer at run time, that's a bug: PrivateAssets="all" is only correct when nothing of the package is needed at run time by consumers. The same metadata decides what a packed .nupkg declares as dependencies, as you'll see in the pack lesson.

Tip: Project references are transitive too: if App references Lib and Lib references Core, App can use Core's types. <DisableTransitiveProjectReferences>true</DisableTransitiveProjectReferences> makes App reference Core explicitly if it needs it, which some teams use to keep layering honest.

Test yourself

Which command shows every path through which package P enters your project's graph?

  1. dotnet package list
  2. dotnet nuget why <project> P
  3. dotnet restore -v:diag
  4. dotnet nuget locals all --list

A library references an analyzer package. How should it be referenced so consumers don't inherit it?

  1. ExcludeAssets="all"
  2. PrivateAssets="all"
  3. IncludeAssets="runtime"
  4. As a ProjectReference

Why can dotnet package list --vulnerable report nothing against a private folder feed?

  1. Folder feeds hide vulnerabilities
  2. The command checks vulnerability data published by sources such as nuget.org; a folder feed has none
  3. It only works on Windows
  4. Because the packages are signed