From 49209804f23671f39d489bdcb061d3a433815d70 Mon Sep 17 00:00:00 2001 From: Hung Nguyen Date: Fri, 22 Jul 2022 16:02:51 +0100 Subject: [PATCH] Add ability to debug Kotlin daemon in integration tests ^KT-53231 In progress --- .../kotlin/gradle/testbase/testDsl.kt | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/testDsl.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/testDsl.kt index 91add3fe7f2..9d0e6ccef11 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/testDsl.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/testDsl.kt @@ -127,6 +127,7 @@ fun TestProject.build( vararg buildArguments: String, forceOutput: Boolean = this.forceOutput, enableGradleDebug: Boolean = this.enableGradleDebug, + kotlinDaemonDebugPort: Int? = this.kotlinDaemonDebugPort, enableBuildCacheDebug: Boolean = false, enableBuildScan: Boolean = this.enableBuildScan, buildOptions: BuildOptions = this.buildOptions, @@ -139,7 +140,8 @@ fun TestProject.build( buildOptions, enableBuildCacheDebug, enableBuildScan, - gradleVersion + gradleVersion, + kotlinDaemonDebugPort ) val gradleRunnerForBuild = gradleRunner .also { if (forceOutput) it.forwardOutput() } @@ -159,6 +161,7 @@ fun TestProject.buildAndFail( vararg buildArguments: String, forceOutput: Boolean = this.forceOutput, enableGradleDebug: Boolean = this.enableGradleDebug, + kotlinDaemonDebugPort: Int? = this.kotlinDaemonDebugPort, enableBuildCacheDebug: Boolean = false, enableBuildScan: Boolean = this.enableBuildScan, buildOptions: BuildOptions = this.buildOptions, @@ -171,7 +174,8 @@ fun TestProject.buildAndFail( buildOptions, enableBuildCacheDebug, enableBuildScan, - gradleVersion + gradleVersion, + kotlinDaemonDebugPort ) val gradleRunnerForBuild = gradleRunner .also { if (forceOutput) it.forwardOutput() } @@ -286,9 +290,18 @@ class TestProject( projectPath: Path, val buildOptions: BuildOptions, val gradleVersion: GradleVersion, - val enableGradleDebug: Boolean, val forceOutput: Boolean, - val enableBuildScan: Boolean + val enableBuildScan: Boolean, + /** + * Whether the test and the Gradle build launched by the test should be executed in the same process so that we can use the same + * debugger for both (see https://docs.gradle.org/current/javadoc/org/gradle/testkit/runner/GradleRunner.html#isDebug--). + */ + val enableGradleDebug: Boolean, + /** + * A port to debug the Kotlin daemon at. + * Note that we'll need to let the debugger start listening at this port first *before* the Kotlin daemon is launched. + */ + val kotlinDaemonDebugPort: Int? = null ) : GradleProject(projectName, projectPath) { fun subProject(name: String) = GradleProject(name, projectPath.resolve(name)) @@ -330,18 +343,20 @@ private fun commonBuildSetup( buildOptions: BuildOptions, enableBuildCacheDebug: Boolean, enableBuildScan: Boolean, - gradleVersion: GradleVersion + gradleVersion: GradleVersion, + kotlinDaemonDebugPort: Int? = null ): List { - val buildOptionsArguments = buildOptions.toArguments(gradleVersion) - val buildCacheDebugOption = if (enableBuildCacheDebug) "-Dorg.gradle.caching.debug=true" else null - val buildScanOption = if (enableBuildScan) "--scan" else null - return buildOptionsArguments + - buildArguments + - listOfNotNull( - "--full-stacktrace", - buildCacheDebugOption, - buildScanOption - ) + return buildOptions.toArguments(gradleVersion) + buildArguments + listOfNotNull( + "--full-stacktrace", + if (enableBuildCacheDebug) "-Dorg.gradle.caching.debug=true" else null, + if (enableBuildScan) "--scan" else null, + kotlinDaemonDebugPort?.let { + // Note that we pass "server=n", meaning that we'll need to let the debugger start listening at this port first *before* the + // Kotlin daemon is launched. That is usually easier than trying to attach the debugger when the Kotlin daemon is launched + // (currently if we don't attach fast enough, the Kotlin daemon will fail to launch). + "-Pkotlin.daemon.jvmargs=-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=$it" + } + ) } private fun TestProject.withBuildSummary(