[[viewing-debugging-dependencies]]
= Viewing and debugging dependencies

Gradle provides sufficient tooling to navigate large dependency graphs and mitigate situations that can lead to link:https://en.wikipedia.org/wiki/Dependency_hell[dependency hell].
Users can choose to render the full graph of dependencies as well as identify the selection reason and origin for a dependency.
The origin of a dependency can be a declared dependency in the build script or a transitive dependency in graph plus their corresponding configuration.
Gradle offers both capabilities through visual representation via build scans and as command line tooling.

[[sec:debugging-build-scans]]
== Build scans

[NOTE]
====
If you do not know what link:https://scans.gradle.com/[build scan]s are, be sure to check them out!
====

A build scan can visualize dependencies as a navigable, searchable tree.
Additional context information can be rendered by clicking on a specific dependency in the graph.

.Dependency tree in a build scan
image::dependency-management-dependencies-report-build-scan.png[]

[[sec:listing_dependencies]]
== Listing dependencies in a project

Gradle can visualize the whole dependency tree for every <<declaring_dependencies.adoc#sec:what-are-dependency-configurations,configuration>> available in the project.

Rendering the dependency tree is particularly useful if you'd like to identify which dependencies have been resolved at runtime.
It also provides you with information about any dependency conflict resolution that occurred in the process and clearly indicates the selected version.
The dependency report always contains declared and transitive dependencies.

Let's say you'd want to create tasks for your project that use the link:https://www.eclipse.org/jgit/[JGit library] to execute SCM operations e.g. to model a release process.
You can declare dependencies for any external tooling with the help of a <<declaring_dependencies.adoc#sec:what-are-dependency-configurations,custom configuration>> so that it doesn't pollute other contexts like the compilation classpath for your production source code.

Every Gradle project provides the task `dependencies` to render the so-called _dependency report_ from the command line.
By default the dependency report renders dependencies for all configurations.
To focus on the information about one configuration, provide the optional parameter `--configuration`.

.Declaring the JGit dependency with a custom configuration
====
include::sample[dir="snippets/userguide/dependencyManagement/inspectingDependencies/dependenciesReport/groovy",files="build.gradle[tags=dependency-declaration]"]
include::sample[dir="snippets/userguide/dependencyManagement/inspectingDependencies/dependenciesReport/kotlin",files="build.gradle.kts[tags=dependency-declaration]"]
====

=== Example: Rendering the dependency report for a custom configuration

.Output of **`gradle -q dependencies --configuration scm`**
----
> gradle -q dependencies --configuration scm
include::{samplesPath}/userguide/dependencyManagement/inspectingDependencies/dependenciesReport/dependencyReport.out[]
----

The dependencies report provides detailed information about the dependencies available in the graph.
Any dependency that could not be resolved is marked with `FAILED` in red color.
Dependencies with the same coordinates that can occur multiple times in the graph are omitted and indicated by an asterisk.
Dependencies that had to undergo conflict resolution render the requested and selected version separated by a right arrow character.

[[sec:identifying_reason_dependency_selection]]
== Identifying which dependency version was selected and why

Large software projects inevitably deal with an increased number of dependencies either through direct or transitive dependencies.
The <<#sec:listing_dependencies,dependencies report>> provides you with the raw list of dependencies but does not explain _why_ they have been selected or _which_ dependency is responsible for pulling them into the graph.

Let's have a look at a concrete example.
A project may request two different versions of the same dependency either as direct or transitive dependency.
Gradle applies <<dependency_resolution.adoc#sec:version-conflict,version conflict resolution>> to ensure that only one version of the dependency exists in the dependency graph.
In this example the conflicting dependency is represented by `commons-codec:commons-codec`.

.Declaring the JGit dependency and a conflicting dependency
====
include::sample[dir="snippets/userguide/dependencyManagement/inspectingDependencies/dependencyInsightReport/groovy",files="build.gradle[tags=dependency-declaration]"]
include::sample[dir="snippets/userguide/dependencyManagement/inspectingDependencies/dependencyInsightReport/kotlin",files="build.gradle.kts[tags=dependency-declaration]"]
====

The dependency tree in a link:https://scans.gradle.com/[build scan] renders the selection reason (conflict resolution) as well as the origin of a dependency if you click on a dependency and select the "Required By" tab.

.Dependency insight capabilities in a build scan
image::dependency-management-dependency-insight-report-build-scan.png[]

Every Gradle project provides the task `dependencyInsight` to render the so-called  _dependency insight report_ from the command line.
Given a dependency in the dependency graph you can identify the selection reason and track down the origin of the dependency selection. You can think of the dependency insight report as the inverse representation of the dependency report for a given dependency. When executing the task you have to provide the mandatory parameter `--dependency` to specify the coordinates of the dependency under inspection. The parameters `--configuration` and `--singlepath` are optional but help with filtering the output.

=== Example: Using the dependency insight report for a given dependency

.Output of **`gradle -q dependencyInsight --dependency commons-codec --configuration scm`**
----
> gradle -q dependencyInsight --dependency commons-codec --configuration scm
include::{samplesPath}/userguide/dependencyManagement/inspectingDependencies/dependencyInsightReport/dependencyInsightReport.out[]
----

[[sec:resolving-version-conflict]]
== Resolving version conflicts

If the selected version does not match your expectation, Gradle offers a series of tools to help you <<dependency_constraints.adoc#,control transitive dependencies>>.

[[sec:resolving-variant-aware-errors]]
== Resolving variant selection errors

Sometimes a selection error will happen at the <<variant_model.adoc#,variant selection level>>.
Have a look at the <<variant_model.adoc#sec:variant-select-errors,dedicated section>> to understand these errors and how to resolve them.

[[sub:resolving-unsafe-configuration-resolution-errors]]
== Resolving unsafe configuration resolution errors

Configurations need to be resolved safely when crossing project boundaries because resolving a configuration can have side effects on Gradle's project model.
Gradle can manage this safe access, but the configuration needs to be accessed in a way that enables Gradle to do so.
There are a number of ways a configuration might be resolved unsafely and Gradle will produce a deprecation warning for each unsafe access.

For example:

* A task from one project directly resolves a configuration in another project.
* A task specifies a configuration from another project as an input file collection.
* A build script for a project resolves a configuration in another project during evaluation.

If your build has an unsafe access deprecation warning, it needs to be fixed.
It's a symptom of these bad practices and can cause strange and indeterminate errors.

In most cases, this issue can be resolved by creating a cross-project dependency on the other project.  See the documentation for <<cross_project_publications.adoc#cross_project_publications, sharing outputs between projects>> for more information.
If you find a use case that can't be resolved using these techniques, please let us know by filing a https://github.com/gradle/gradle/issues[GitHub Issue] adhering to our issue guidelines.
