'How can I track down the source of a transitive dependency?

In a project/solution with lots of <PackageReference> dependencies, it can be difficult to find the source of a transitive dependency that's being pulled in. For example, no projects in my solution directly reference the package System.Data.SqlClient, but something is pulling it in transitively. Tracking that down "by hand" is virtually impossible in a large solution or project with lots of direct package references.

Is there any ready-made way (eg, a combination of .Net CLI commands) that, given a particular package, will find and reveal the source of the transitive reference? I use Rider, which has some awesome code navigation and "discovery"-type tools, but I can't find anything that helps with my goal.

Note: I also have VisualStudio if it has this capability built-in somewhere, I'd just need a pointer to where/how.



Solution 1:[1]

The capability is built into the latest Visual Studio 2019.

With Visual Studio 2019, Update 6, I can see something like the following:

Solution explorer dependencies tree

Note that you can also discover packages by searching in the solution explorer.

Solution explorer search

Unfortunately it's not available in the NuGet Package Manager installed view yet.

Solution 2:[2]

Searching in VS did not work for me, at least not for a BCL package like System.Net.Http. What did work is looking in obj\project.assets.json, which lists all dependencies.

It's still a somewhat manual process of searching for each package up the tree. https://www.jerriepelser.com/blog/analyze-dotnet-project-dependencies-part-2/ describes the process well and provides code to automate it, but I have not tried the code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 imps
Solution 2 EM0