View NuGet package dependency hierarchy

C#Nuget

C# Problem Overview


Is there a way, either textual or graphical, to view the hierarchy of dependencies between NuGet packages?

C# Solutions


Solution 1 - C#

If you're using the new .csproj, you could get all dependencies with reference in here (after project built): >{ProjectDir}\obj\project.assets.json

enter image description here

Solution 2 - C#

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);
        }
    }
}

Solution 3 - C#

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

Solution 4 - C#

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.

Solution 5 - C#

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.

NuGet dependencies graph sample (web)

NuGet dependencies graph sample (Graphviz)

Solution 6 - C#

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);
            }
        }
    }

Solution 7 - C#

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.

Solution 8 - C#

Package Visualized from NuGet 1.4 should work. See http://docs.nuget.org/docs/release-notes/nuget-1.4

Solution 9 - C#

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.

Solution 10 - C#

FYI, MyGet.org has this kind of visualization built-in. You can view dependency graphs on the Feed Details page.

Solution 11 - C#

https://github.com/mikehadlow/AsmSpy using this to identify assembly version across a project

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNeil BarnwellView Question on Stackoverflow
Solution 1 - C#John_JView Answer on Stackoverflow
Solution 2 - C#Gian MarcoView Answer on Stackoverflow
Solution 3 - C#Neil BarnwellView Answer on Stackoverflow
Solution 4 - C#N. KudryavtsevView Answer on Stackoverflow
Solution 5 - C#0xcedView Answer on Stackoverflow
Solution 6 - C#Javier SolisView Answer on Stackoverflow
Solution 7 - C#Josef GinermanView Answer on Stackoverflow
Solution 8 - C#Eugene PetrenkoView Answer on Stackoverflow
Solution 9 - C#Eyal Ben MosheView Answer on Stackoverflow
Solution 10 - C#Xavier DecosterView Answer on Stackoverflow
Solution 11 - C#MarteaView Answer on Stackoverflow