-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn if using Kotlin Compile Daemon Fallback (#195)
- Loading branch information
1 parent
a9e1e70
commit 87f378c
Showing
17 changed files
with
260 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...-plugin/src/integrationTest/java/com/osacky/doctor/KotlinDaemonFallbackIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.osacky.doctor | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
|
||
class KotlinDaemonFallbackIntegrationTest : AbstractIntegrationTest() { | ||
|
||
@Test | ||
fun testDisallowKotlinCompileDaemonFallback() { | ||
writeKotlinBuildGradle(true) | ||
writeSettingsFile() | ||
testProjectRoot.newFolder("src/main/java/foo") | ||
testProjectRoot.newFolder("src/test/java/foo") | ||
testProjectRoot.writeFileToName( | ||
"src/main/java/Foo.kt", | ||
""" | ||
package foo | ||
class Foo { | ||
fun bar() { | ||
println("Hello, world!") | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
testProjectRoot.writeFileToName( | ||
"src/test/java/Foo.kt", | ||
""" | ||
package foo | ||
class Foo { | ||
fun bar() { | ||
println("Hello, world!") | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
val result = assembleRunnerWithIncorrectDaemonArguments().build() | ||
|
||
assertThat(result.output).contains("Could not connect to kotlin daemon. Using fallback strategy.") | ||
assertThat(result.output).contains("The Kotlin Compiler Daemon failed to connect and likely won't recover on its own.") | ||
} | ||
|
||
@Test | ||
fun allowKotlinCompileFallback() { | ||
writeKotlinBuildGradle(false) | ||
writeSettingsFile() | ||
testProjectRoot.newFolder("src/main/java/foo") | ||
testProjectRoot.writeFileToName( | ||
"src/main/java/foo/Foo.kt", | ||
""" | ||
package foo | ||
class Foo { | ||
fun bar() { | ||
println("Hello, world!") | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
val result = assembleRunnerWithIncorrectDaemonArguments().build() | ||
|
||
assertThat(result.output).contains("Could not connect to kotlin daemon. Using fallback strategy.") | ||
assertThat(result.output).contains("SUCCESS") | ||
} | ||
|
||
private fun writeSettingsFile() { | ||
testProjectRoot.writeFileToName( | ||
"settings.gradle", | ||
""" | ||
pluginManagement { | ||
repositories { | ||
mavenCentral() | ||
gradlePluginPortal() | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
private fun assembleRunnerWithIncorrectDaemonArguments() = createRunner() | ||
.withArguments("check", "-Dkotlin.daemon.jvm.options=invalid_jvm_argument_to_fail_process_startup") | ||
|
||
private fun writeKotlinBuildGradle(allowDaemonFallback: Boolean) { | ||
testProjectRoot.writeBuildGradle( | ||
""" | ||
plugins { | ||
id "com.osacky.doctor" | ||
id "org.jetbrains.kotlin.jvm" version "1.6.10" | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
doctor { | ||
warnIfKotlinCompileDaemonFallback = $allowDaemonFallback | ||
javaHome { | ||
ensureJavaHomeMatches = false | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
doctor-plugin/src/main/java/com/osacky/doctor/KotlinCompileDaemonFallbackDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.osacky.doctor | ||
|
||
import com.osacky.doctor.internal.sysProperty | ||
import com.osacky.tagger.ScanApi | ||
import io.reactivex.rxjava3.disposables.CompositeDisposable | ||
import org.gradle.api.Project | ||
import org.gradle.internal.logging.LoggingManagerInternal | ||
import org.gradle.internal.logging.events.LogEvent | ||
import org.gradle.internal.logging.events.OutputEvent | ||
import org.gradle.internal.logging.events.OutputEventListener | ||
import org.gradle.kotlin.dsl.support.serviceOf | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
class KotlinCompileDaemonFallbackDetector( | ||
private val project: Project, | ||
private val extension: DoctorExtension | ||
) : BuildStartFinishListener, HasBuildScanTag { | ||
private val fallbackCounter = AtomicInteger(0) | ||
private val loggingService = project.gradle.serviceOf<LoggingManagerInternal>() | ||
private val failureEventListener = FailureEventListener(fallbackCounter) | ||
private val disposable = CompositeDisposable() | ||
|
||
override fun onStart() { | ||
if (!extension.warnIfKotlinCompileDaemonFallback.get() || isDaemonDisabled(project)) { | ||
return | ||
} | ||
loggingService.addOutputEventListener(failureEventListener) | ||
} | ||
|
||
override fun onFinish(): List<String> { | ||
loggingService.removeOutputEventListener(failureEventListener) | ||
disposable.dispose() | ||
if (hasUsedFallback()) { | ||
return listOf( | ||
""" | ||
The Kotlin Compiler Daemon failed to connect and likely won't recover on its own. | ||
The fallback strategy is incredibly slow and should be avoided. | ||
https://youtrack.jetbrains.com/issue/KT-48843 | ||
To recover, try killing the Kotlin Compiler Daemon: | ||
1. ./gradlew --stop | ||
2. Find Kotlin daemon process id (pid): `jps | grep Kotlin` | ||
3. kill <pid> | ||
If that didn't help, check that there are no invalid JVM arguments in "kotlin.daemon.jvm.options" property except for Xmx. | ||
""".trimIndent() | ||
) | ||
} | ||
return emptyList() | ||
} | ||
|
||
override fun addCustomValues(buildScanApi: ScanApi) { | ||
if (hasUsedFallback()) { | ||
buildScanApi.tag("doctor-kotlin-compile-daemon-fallback") | ||
} | ||
} | ||
|
||
private fun hasUsedFallback() = fallbackCounter.get() > 1 | ||
|
||
/** | ||
* Copy of internal logic in GradleKotlinCompilerRunner | ||
*/ | ||
private fun isDaemonDisabled(project: Project): Boolean { | ||
val strategy = sysProperty("kotlin.compiler.execution.strategy", project.providers).orElse("daemon") | ||
return strategy != "daemon" // "in-process", "out-of-process" | ||
} | ||
} | ||
|
||
internal class FailureEventListener( | ||
private val fallbacksCounter: AtomicInteger, | ||
) : OutputEventListener { | ||
|
||
override fun onOutput(event: OutputEvent) { | ||
if (isFallbackMessage(event)) { | ||
// Can't fail a build from OutputEventListener. So, only mark it | ||
fallbacksCounter.incrementAndGet() | ||
} | ||
} | ||
|
||
private fun isFallbackMessage(event: OutputEvent): Boolean { | ||
return event is LogEvent && | ||
event.message.contains("Could not connect to kotlin daemon. Using fallback strategy.") | ||
} | ||
} |
17 changes: 15 additions & 2 deletions
17
doctor-plugin/src/main/java/com/osacky/doctor/internal/CoCaHelpers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
package com.osacky.doctor.internal | ||
|
||
import org.gradle.api.invocation.Gradle | ||
import org.gradle.api.provider.ProviderFactory | ||
import org.gradle.util.GradleVersion | ||
import java.util.Optional | ||
|
||
fun Gradle.shouldUseCoCaClasses(): Boolean = GradleVersion.version(gradleVersion) >= GradleVersion.version("6.5") | ||
fun shouldUseCoCaClasses(): Boolean = isGradle65OrNewer() | ||
|
||
fun isGradle65OrNewer(): Boolean { | ||
return GradleVersion.current() >= GradleVersion.version("6.5") | ||
} | ||
|
||
fun sysProperty(name: String, providers: ProviderFactory): Optional<String> { | ||
if (isGradle65OrNewer()) { | ||
val property = providers.systemProperty(name).forUseAtConfigurationTime() | ||
return Optional.ofNullable(property.orNull) | ||
} | ||
return Optional.ofNullable(System.getProperty(name)) | ||
} |
Oops, something went wrong.