View Nuget Package Dependency Hierarchy

How to show NuGet package dependencies?

Agree with Matt, what you want for now is not supported in .net framework projects. This feature is available only for new SDK format projects like .net core and .net standard projects. See similar issue here.

And as I know, there doesn't exist extensions which supports this behavior. So I'm afraid the answer is negative, we can't use nuget dependency tree in .net framework projects cause both VS IDE itself and VS extensions don't support this.

If you do need this feature, I recommend you can post your feature request in Developer Community Forum
(Our User Voice forum). If it get enough votes, the Product Team would consider it seriously. Thanks for your help to make VS better :)

.NET Core Dependency Tree

You can add an msbuild target to your project file (inside the <Project> element) like this:

<Target Name="PrintAllReferences" DependsOnTargets="RunResolvePackageDependencies">
<Message Importance="high" Text="Referenced package: %(PackageDefinitions.Identity)" />
</Target>

Which you can call like this (a line without a parent package name means it is referenced by the project directly):

$ dotnet msbuild /nologo /t:PrintAllReferences
Referenced package: Microsoft.NETCore.Platforms/1.1.0
Referenced package: Microsoft.NETCore.Targets/1.1.0
Referenced package: Microsoft.Win32.Primitives/4.3.0
Referenced package: NETStandard.Library/1.6.1
Referenced package: runtime.debian.8-x64.runtime.native.System.Security.Cryptography.OpenSsl/4.3.0
Referenced package: runtime.fedora.23-x64.runtime.native.System.Security.Cryptography.OpenSsl/4.3.0
Referenced package: runtime.fedora.24-x64.runtime.native.System.Security.Cryptography.OpenSsl/4.3.0
Referenced package: runtime.native.System/4.3.0
Referenced package: runtime.native.System.IO.Compression/4.3.0
Referenced package: runtime.native.System.Net.Http/4.3.0
Referenced package: runtime.native.System.Security.Cryptography.Apple/4.3.0
Referenced package: runtime.native.System.Security.Cryptography.OpenSsl/4.3.0
Referenced package: runtime.opensuse.13.2-x64.runtime.native.System.Security.Cryptography.OpenSsl/4.3.0
Referenced package: runtime.opensuse.42.1-x64.runtime.native.System.Security.Cryptography.OpenSsl/4.3.0
Referenced package: System.Buffers/4.3.0
Referenced package: System.Collections/4.3.0

If you wanted a "reverse dependency tree" - a list of packages and which packages reference them - you can do something similar to:

<Target Name="PrintPackagesAndParents" DependsOnTargets="RunResolvePackageDependencies">
<Message Importance="high" Text="* %(PackageDependencies.Identity) referenced by:%0a^---@(PackageDependencies->'%(ParentPackage) - target %(ParentTarget)', '%0a^---')" />
</Target>

which produces the following output:

$ dotnet msbuild /nologo /t:PrintPackagesAndParents
* JetBrains.Annotations/10.2.1 referenced by:
^--- - target .NETStandard,Version=v1.3
* System.IO.FileSystem.Primitives/4.0.1 referenced by:
^---NETStandard.Library/1.6.0 - target .NETStandard,Version=v1.3
^---System.IO.Compression.ZipFile/4.0.1 - target .NETStandard,Version=v1.3
^---System.IO.FileSystem/4.0.1 - target .NETStandard,Version=v1.3
^---System.Xml.ReaderWriter/4.0.11 - target .NETStandard,Version=v1.3
* System.Linq/4.1.0 referenced by:
^---NETStandard.Library/1.6.0 - target .NETStandard,Version=v1.3
^---System.Security.Cryptography.Encoding/4.0.0 - target .NETStandard,Version=v1.3
* System.Linq.Expressions/4.1.0 referenced by:
^---NETStandard.Library/1.6.0 - target .NETStandard,Version=v1.3
* System.Net.Http/4.1.0 referenced by:
^---NETStandard.Library/1.6.0 - target .NETStandard,Version=v1.3
* System.Net.Primitives/4.0.11 referenced by:
^---NETStandard.Library/1.6.0 - target .NETStandard,Version=v1.3
^---System.Net.Http/4.1.0 - target .NETStandard,Version=v1.3
^---System.Net.Sockets/4.1.0 - target .NETStandard,Version=v1.3

There isn't really documentation about these items, but they have "public" name and are generated by the ResolvePackageDependencies task which is executed as part of the RunResolvePackageDependencies target and produces a few very useful items: TargetDefinitions, PackageDefinitions, PackageDependencies, FileDependencies and DiagnosticMessages.

How to get NuGet package dependency report in Visual Studio 2015?

When installing a package, it will pop up a Preview window that list what dependencies will be installed with this package.

For example, if I want to install Microsoft.Azure.Common package, when click Install button, it will pop up following window to tell me what dependencies will be installed.
Sample Image

But if you want to know the dependencies hierarchy between these dependencies, please try run following code to output the dependencies hierarchy. Please install NuGet.Core package before write following code.

Refer to: https://gist.github.com/panicoenlaxbox/894a904afabf90ecdd6f

using NuGet;
using System.Runtime.Versioning;

static void Main(string[] args)
{
var frameworkName = new FrameworkName(".NETFramework, Version=4.5");
var repository = PackageRepositoryFactory.Default.CreateRepository(@"D:\Visual Studio 2015 Project\CAT Case Sample\ApplicationInsights\packages");
var packages =
repository.GetPackages().Where(
p => p.GetFullName().ToLower().Contains("azure") || p.GetCompatiblePackageDependencies(frameworkName).Any(t => repository.ResolveDependency(t, false, true).GetFullName().ToLower().Contains("azure")));
foreach (IPackage package in packages)
{
PrintPackageInformation(repository, frameworkName, package, 0);
}
Console.ReadLine();

}

private static void PrintPackageInformation(IPackageRepository repository, FrameworkName frameworkName, IPackage package, int level)
{
Console.WriteLine("{0}{1}", new string(' ', level * 3), package);
foreach (PackageDependency dependency in package.GetCompatiblePackageDependencies(frameworkName))
{
IPackage subPackage = repository.ResolveDependency(dependency, false, true);
PrintPackageInformation(repository, frameworkName, subPackage, level + 1);
}
}

Displaying a NuGet package's dependencies

Yes, there is.

# shows all available packages
PM> get-package -list

# get single package info
PM> get-package -list solrnet.nhibernate

# view dependencies
PM> get-package -list solrnet.nhibernate | select dependencies
NHibernate:[2.1.2.4000]|CommonServiceLocator:[1.0]|SolrNet:[0.3.1]

List all the nuget packages with dependencies recursively for a given project/Solution for .NET Core project in VS2017

You can use the dotnet cli: dotnet list package --include-transitive. The dotnet-outdated global tool probably has similar functionality.



Related Topics



Leave a reply



Submit