Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add additional logging for KotlinProjectExtension failures #125

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.adamko.dokkatoo.dokka.parameters.DokkaSourceSetSpec
import dev.adamko.dokkatoo.dokka.parameters.KotlinPlatform
import dev.adamko.dokkatoo.internal.DokkatooInternalApi
import dev.adamko.dokkatoo.internal.not
import dev.adamko.dokkatoo.internal.warn
import java.io.File
import javax.inject.Inject
import org.gradle.api.Named
Expand Down Expand Up @@ -55,7 +56,7 @@ abstract class DokkatooKotlinAdapter @Inject constructor(
) : Plugin<Project> {

override fun apply(project: Project) {
logger.info("applied DokkatooKotlinAdapter to ${project.path}")
logger.info("Applying $dkaName to ${project.path}")

project.plugins.withType<DokkatooBasePlugin>().configureEach {
project.pluginManager.apply {
Expand All @@ -68,11 +69,28 @@ abstract class DokkatooKotlinAdapter @Inject constructor(
}

private fun exec(project: Project) {
val kotlinExtension = project.extensions.findKotlinExtension() ?: run {
logger.info("could not find Kotlin Extension")
val kotlinExtension = project.extensions.findKotlinExtension()
if (kotlinExtension == null) {
if (project.extensions.findByName("kotlin") != null) {
// uh oh - the Kotlin extension is present but findKotlinExtension() failed.
// Is there a class loader issue? https://github.com/gradle/gradle/issues/27218
logger.warn {
val allPlugins =
project.plugins.joinToString { it::class.qualifiedName ?: "${it::class}" }
val allExtensions =
project.extensions.extensionsSchema.elements.joinToString { "${it.name} ${it.publicType}" }

/* language=TEXT */ """
|$dkaName failed to get KotlinProjectExtension in ${project.path}
| Applied plugins: $allPlugins
| Available extensions: $allExtensions
""".trimMargin()
}
}
logger.info("Skipping applying $dkaName in ${project.path} - could not find KotlinProjectExtension")
return
}
logger.info("Configuring Dokkatoo in Gradle Kotlin Project ${project.path}")
logger.info("Configuring $dkaName in Gradle Kotlin Project ${project.path}")

val dokkatooExtension = project.extensions.getByType<DokkatooExtension>()

Expand Down Expand Up @@ -161,6 +179,8 @@ abstract class DokkatooKotlinAdapter @Inject constructor(

@DokkatooInternalApi
companion object {
private val dkaName: String = DokkatooKotlinAdapter::class.simpleName!!

private val logger = Logging.getLogger(DokkatooKotlinAdapter::class.java)

/** Try and get [KotlinProjectExtension], or `null` if it's not present */
Expand All @@ -174,7 +194,10 @@ abstract class DokkatooKotlinAdapter @Inject constructor(
when (e) {
is TypeNotPresentException,
is ClassNotFoundException,
is NoClassDefFoundError -> null
is NoClassDefFoundError -> {
logger.info("$dkaName failed to find KotlinExtension ${e::class} ${e.message}")
null
}

else -> throw e
}
Expand Down Expand Up @@ -258,9 +281,9 @@ private class KotlinCompilationDetailsBuilder(

private fun KotlinProjectExtension.allKotlinCompilations(): Collection<KotlinCompilation<*>> =
when (this) {
is KotlinMultiplatformExtension -> targets.flatMap { it.compilations }
is KotlinMultiplatformExtension -> targets.flatMap { it.compilations }
is KotlinSingleTargetExtension<*> -> target.compilations
else -> emptyList() // shouldn't happen?
else -> emptyList() // shouldn't happen?
}

/**
Expand Down
8 changes: 8 additions & 0 deletions modules/dokkatoo-plugin/src/main/kotlin/internal/logUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.adamko.dokkatoo.internal

import org.gradle.api.logging.Logger

/** Only evaluate and log [msg] when [Logger.isWarnEnabled] is `true`. */
internal fun Logger.warn(msg: () -> String) {
if (isWarnEnabled) warn(msg())
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.kotest.matchers.paths.shouldBeAFile
import io.kotest.matchers.paths.shouldNotExist
import io.kotest.matchers.string.shouldBeEmpty
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldNotContain
import org.gradle.testkit.runner.TaskOutcome.*

class MultiModuleFunctionalTest : FunSpec({
Expand Down Expand Up @@ -302,7 +303,6 @@ class MultiModuleFunctionalTest : FunSpec({
val project = initDokkatooProject("logging")

test("expect no logs when built using --quiet log level") {

project.runner
.addArguments(
"clean",
Expand All @@ -318,7 +318,6 @@ class MultiModuleFunctionalTest : FunSpec({
}

test("expect no Dokkatoo logs when built using lifecycle log level") {

project.runner
.addArguments(
"clean",
Expand Down Expand Up @@ -377,6 +376,31 @@ class MultiModuleFunctionalTest : FunSpec({
}
}
}

context("KotlinProjectExtension failure warning") {
val project = initDokkatooProject("kpe-warning") {
buildGradleKts = buildGradleKts.lines().joinToString("\n") { line ->
when {
line.startsWith(""" kotlin("jvm")""") -> "//$line"

else -> line
}
}
}

test("expect warning regarding KotlinProjectExtension") {
project.runner
.addArguments("clean")
.forwardOutput()
.build {
// the root project doesn't have the KGP applied, so KotlinProjectExtension shouldn't be applied
output shouldNotContain "DokkatooKotlinAdapter failed to get KotlinProjectExtension in :\n"

output shouldContain "DokkatooKotlinAdapter failed to get KotlinProjectExtension in :subproject-hello\n"
output shouldContain "DokkatooKotlinAdapter failed to get KotlinProjectExtension in :subproject-goodbye\n"
}
}
}
})

private fun initDokkatooProject(
Expand Down