Skip to content

Commit e71624a

Browse files
oheger-boschsschuberth
authored andcommitted
chore(gradle): Improve logging of unresolved dependencies
The error message produced for an unresolved dependency was not really helpful because the causes of the top-level `ModuleVersionResolveException` are again exceptions of this type with generic messages. The interesting information is found in the causes of these exceptions. Signed-off-by: Oliver Heger <[email protected]>
1 parent 341130f commit e71624a

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

plugins/package-managers/gradle-plugin/src/main/kotlin/OrtModelBuilder.kt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,7 @@ internal class OrtModelBuilder : ToolingModelBuilder {
295295
append(dep.from)
296296
append(".")
297297

298-
val causes = (dep.failure as? ModuleVersionResolveException)?.causes?.takeIf { it.isNotEmpty() }
299-
if (causes != null) {
300-
appendLine(" Causes are:")
301-
append(causes.joinToString("\n") { it.toString() })
302-
}
298+
appendCauses(dep.failure)
303299
}
304300

305301
logger.error(message)
@@ -319,3 +315,27 @@ internal class OrtModelBuilder : ToolingModelBuilder {
319315
}
320316
}
321317
}
318+
319+
/**
320+
* Add a string with information about the causes of the given [exception] to this [StringBuilder]. This is used to
321+
* log the reason why a dependency could not be resolved. To get meaningful information, all causes need to be obtained
322+
* recursively. This is because the top-level [ModuleVersionResolveException] typically has only other
323+
* [ModuleVersionResolveException]s as causes with generic messages. The actual information about what went wrong is
324+
* hidden somewhere down the cause chain.
325+
*/
326+
private fun StringBuilder.appendCauses(exception: Throwable) {
327+
val causes = (exception as? ModuleVersionResolveException)?.causes?.takeIf { it.isNotEmpty() }
328+
if (causes != null) {
329+
appendLine(" Causes are:")
330+
val allCauses = mutableSetOf<String>()
331+
332+
fun getAllCauses(exception: Throwable) {
333+
exception.message?.also(allCauses::add)
334+
exception.cause?.also { getAllCauses(it) }
335+
}
336+
337+
causes.forEach { getAllCauses(it) }
338+
339+
append(allCauses.joinToString("\n"))
340+
}
341+
}

0 commit comments

Comments
 (0)