Is there a way, either textual or graphical, to view the hierarchy of dependencies between NuGet packages?
13 Answers
If you're using the new .csproj
, you could get all dependencies with reference in here (after project built):
{ProjectDir}\obj\project.assets.json
-
1
-
9I think this works in any csproj that uses the
<PackageReference>
format instead of a packages.config file. I have a legacy app with an oldschool csproj, modified to use PackageReference and this file exists in my obj folder. Commented Sep 21, 2021 at 12:05
Like @neil-barnwell solution, but works with NuGet.Core 2.7+
Install-Package NuGet.Core
Here is the code
using System;
using System.Linq;
using System.Runtime.Versioning;
using System.IO;
using NuGet;
public class Program
{
public static void Main(string[] args)
{
var frameworkName = new FrameworkName(".NETFramework, Version=4.0");
// var packageSource = "https://www.nuget.org/api/v2/";
var packageSource = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "NuGet", "Cache");
var repository = PackageRepositoryFactory.Default.CreateRepository(packageSource);
const bool prerelease = false;
var packages = repository.GetPackages()
.Where(p => prerelease ? p.IsAbsoluteLatestVersion : p.IsLatestVersion)
.Where(p => VersionUtility.IsCompatible(frameworkName, p.GetSupportedFrameworks()));
foreach (IPackage package in packages)
{
GetValue(repository, frameworkName, package, prerelease, 0);
}
Console.WriteLine();
Console.WriteLine("Press Enter...");
Console.ReadLine();
}
private static void GetValue(IPackageRepository repository, FrameworkName frameworkName, IPackage package, bool prerelease, int level)
{
Console.WriteLine("{0}{1}", new string(' ', level * 3), package);
foreach (PackageDependency dependency in package.GetCompatiblePackageDependencies(frameworkName))
{
IPackage subPackage = repository.ResolveDependency(dependency, prerelease, true);
GetValue(repository, frameworkName, subPackage, prerelease, level + 1);
}
}
}
-
It is throwing Could not load type 'System.Windows.IWeakEventListener error– Amal PsCommented Sep 5, 2022 at 6:56
It is also possible to write code against the API in NuGet.Core
. Install it via NuGet:
install-package nuget.core
Then you can get a repository object and walk the graph. Here's a sample app I just built:
using System;
using System.Collections.Generic;
using System.Linq;
using NuGet;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
var repo = new LocalPackageRepository(@"C:\Code\Common\Group\Business-Logic\packages");
IQueryable<IPackage> packages = repo.GetPackages();
OutputGraph(repo, packages, 0);
}
static void OutputGraph(LocalPackageRepository repository, IEnumerable<IPackage> packages, int depth)
{
foreach (IPackage package in packages)
{
Console.WriteLine("{0}{1} v{2}", new string(' ', depth), package.Id, package.Version);
IList<IPackage> dependentPackages = new List<IPackage>();
foreach (var dependency in package.Dependencies)
{
dependentPackages.Add(repository.FindPackage(dependency.Id, dependency.VersionSpec.ToString()));
}
OutputGraph(repository, dependentPackages, depth += 3);
}
}
}
}
In my case, this app outputs something like this:
MyCompany.Castle v1.1.0.3
Castle.Windsor v2.5.3
Castle.Core v2.5.2
MyCompany.Common v1.1.0.6
CommonServiceLocator v1.0
MyCompany.Enum v1.1.0.7
MyCompany.Common v1.1.0.6
CommonServiceLocator v1.0
MyCompany.Enum v1.1.0.7
MyCompany.Enum v1.1.0.7
MyCompany.Versioning v1.3
Castle.Core v2.5.2
Castle.Windsor v2.5.3
Castle.Core v2.5.2
CommonServiceLocator v1.0
NUnit v2.5.10.11092
RhinoMocks v3.6
-
2There's a ResolveDependency extension method in NuGet that you can use. Commented Jul 19, 2011 at 9:17
-
7depth += 3 is probably a bug, change to depth + 3. You can then see all packages on the left and only dependencies of that packages are indented Commented Sep 26, 2013 at 16:59
-
5I tried to update this to the latest version of NuGet, but my edit was rejected. Change foreach dependency to package.DependencySets and add another foreach loop get the dependency. Commented Sep 26, 2013 at 17:08
-
1Make sure you run this in a .Net Framework console app and not a .Net Core app. The core version will not have access to all the framework dependencies and will blow up on the call to
FindPackages
– AdrianCommented Feb 2, 2021 at 21:51 -
1Also the Nuget.Core package should be 1.5.x or lower. With higher versions the code won't compile. Commented May 17, 2022 at 7:43
I've found a nice NPM package to print the dependency tree into console. Of course if you don't mind using/installing NPM/Node.JS.
Considering other solutions, this is the most simple one, you don't need to write your own code or register something, and you get just such dependency tree as you expect. But it works only with packages.config
format.
I can't believe this functionality is absent in free Visual Studio editions or nuget.exe too.
-
5This works great (for packages.config format) and should have more votes! Note that the version numbers shown for dependencies are not the minimum required version (or range) but the actual version installed. (It would be nice to know which packages depend on very old versions of dependencies, because there might be breaking changes in the currently installed version.)– YipYipCommented Mar 24, 2018 at 0:12
-
The other answers provided did not work for my Xamarin Visual Studio solution but this one does because it only looks at packages.config. Thank you– stepheawCommented Jul 25, 2018 at 16:02
-
Can confirm it after several hours of trial an error - THIS ANSWER WORKS. Thanks– KraegoCommented Jan 11, 2023 at 20:44
I Can Has .NET Core (GitHub repository) produces nice graphs of NuGet dependencies along with a Graphviz representation. And as its name implies, you also get .NET Core compatibility information for free.
If you prefer to run it locally on your computer, I Can Has .NET Core also offers a console mode.
-
10"We have decided to close down icanhasdot.net in May 2020." — GitHub - OctopusDeploy/ICanHasDotnetCore– PangCommented Mar 15, 2021 at 1:36
-
-
1It's a pity, because this looks like what I was searching for and still did not find an alternative Commented Sep 18, 2023 at 15:12
I add a compatible solution with the latest version of nuget-core
install-package nuget.core
This is the console App to get the dependencies graph
class Program
{
static void Main()
{
Console.Write("Enter the local repo folder: ");
var repoFolder = Console.ReadLine();
var repo = new LocalPackageRepository(repoFolder);
IQueryable<IPackage> packages = repo.GetPackages();
OutputGraph(repo, packages, 0);
}
static void OutputGraph(LocalPackageRepository repository, IEnumerable<IPackage> packages, int depth)
{
foreach (IPackage package in packages)
{
Console.WriteLine("{0}{1} v{2}", new string(' ', depth), package.Id, package.Version);
IList<IPackage> dependentPackages = new List<IPackage>();
foreach (var dependencySet in package.DependencySets)
{
foreach (var dependency in dependencySet.Dependencies)
{
var dependentPackage = repository.FindPackage(dependency.Id, dependency.VersionSpec, true, true);
if (dependentPackage != null)
{
dependentPackages.Add(dependentPackage);
}
}
}
OutputGraph(repository, dependentPackages, depth + 3);
}
}
}
Since this is an old question, it is important to note the following:
This is a built-in feature in the new csproj format. In Visual Studio 2017 and up, open the Solution Explorer and you can find you packages like:
{Your project}->Dependencies->Packages
You can open each NuGet dependency tree and run with it recursively, effectively seeing not only the dependency tree for specific packages, but also which NuGet packages your project actually installs.
-
5Where exactly do you see "Dependencies"? I don't see it anywhere in the Solution Explorer. Commented Dec 18, 2018 at 15:51
-
5If you're working on a .Net Standard or .Net Core project then you can see it by opening each project's tree. But if you're working on a .Net Framework project then you should use one of the other responses. Commented Dec 18, 2018 at 18:59
-
3Anyone that knows if this is going to make it into the Solution Explorer for those of us who use .Net Frameweork? Commented Nov 19, 2019 at 13:04
-
1This even works with Solution Explorer search (at least in VS 2019), super useful to quickly trace the dependency chain leading to a certain package. Commented Mar 25, 2021 at 17:29
-
There is a downside to this answer: In Solution Explorer the version number that is shown behind each dependency is not the version number needed by this special dependency, but the highest version that is required over all dependencies. If you need to find out exactly which Nuget packages pushes a required version number up, you will have to resort to the answer of @JohnJang (look into \obj\project.assets.json).– JpsyCommented Jan 11, 2023 at 16:19
Package Visualized from NuGet 1.4 should work. See http://docs.nuget.org/docs/release-notes/nuget-1.4
-
6Ahh that is helpful, but unfortunately I only have VS Pro, and the tools used for that are only in Ultimate. :( Commented Jul 11, 2011 at 17:14
-
Its unfortunate that is not included in pro version. IMHO considering NuGet is available even in express versions, all its features should be available even in express versions. Package graph information is very helpful sometime. Commented May 13, 2014 at 12:27
Another option you have is to use the nuget-deps-tree npm package. It supports both the packages.config format and the newer assets format used by .NET projects.
-
Unfortunately, the output of nuget-deps-tree is almost useless for casual viewing of a dependency tree, but it is the only solution I've seen that actually works.– EKWCommented Mar 16, 2021 at 20:48
If you need more than 1 depth of dependencies and more detailed information about your packages, you can also check the below file in your project:
{project directory}\bin\Debug\net8.0\<project name>.deps.json
FYI, MyGet.org has this kind of visualization built-in. You can view dependency graphs on the Feed Details page.
https://github.com/mikehadlow/AsmSpy using this to identify assembly version across a project
.NET has a built-in feature now:
dotnet list package --include-transitive
which can print an output as follows:
Project 'Maui.Controls.Sample.Sandbox' has the following package references
[net8.0-windows10.0.19041]:
Top-level Package Requested Resolved
> Microsoft.Extensions.DependencyInjection 8.0.0 8.0.0
Transitive Package Resolved
> libsodium 1.0.19
> LZMA-SDK 22.1.1
> Microsoft.AspNetCore.Authorization 8.0.0
> Microsoft.AspNetCore.Components 8.0.0
> Microsoft.AspNetCore.Components.Analyzers 8.0.0
> Microsoft.AspNetCore.Components.Forms 8.0.0
[..]
so unfortunately, it's not really hierarchical but it gives the dependencies.
For a better dependency hierarchy which does rely on external tools, one can enable generating of JSON lock files (packages.lock.json
files) in her C# project using
<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
then run dotnet restore
and check contents of your packages.lock.json
file:
{
"version": 1,
"dependencies": {
"net8.0-windows10.0.19041": {
"Microsoft.Extensions.DependencyInjection": {
"type": "Direct",
"requested": "[8.0.0, )",
"resolved": "8.0.0",
"contentHash": "V8S3bsm50ig6JSyrbcJJ8bW2b9QLGouz+G1miK3UTaOWmMtFwNNNzUf4AleyDWUmTrWMLNnFSLEQtxmxgNQnNQ==",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0"
}
},
"libsodium": {
"type": "Transitive",
"resolved": "1.0.19",
"contentHash": "tupm/HViwBN6Knd/gckR+cLaJGR39GLmiU4iDMM5hp/1BoczMr8fwJhSU+3/C2V4hi9nBK/4FICRKtTLU30TCA=="
},
"LZMA-SDK": {
"type": "Transitive",
"resolved": "22.1.1",
"contentHash": "lxG8/H53OCqebnDFlTStwzbgtmUN/DqnXN8kffa/ctHcSEHhIPDyEa3jLzpZxVX5F8bmJgvb1mna6gnhetykIw=="
},
"Microsoft.AspNetCore.Authorization": {
"type": "Transitive",
"resolved": "8.0.0",
"contentHash": "OGIGJMnlWvQgcweHcv1Mq/P24Zx/brUHeEdD05NzqkSXmQSnFomTvVyCuBtCXT4JPfv2m70y1RSocmd9bIbJRg==",
"dependencies": {
"Microsoft.AspNetCore.Metadata": "8.0.0",
"Microsoft.Extensions.Logging.Abstractions": "8.0.0",
"Microsoft.Extensions.Options": "8.0.0"
}
},
[..]
https://github.com/NuGet/Home/issues/6188 is unfortunately still open.