Filter out embedded Gradle stdlib from Test process runtime classpath

Since 2.2.20 uncaught exceptions in coroutines make the Test process
hang if the stdlib is downgraded without appropriate -api-version
downgrade. In test runtime we don't really want to see Gradle's
embedded stdlib.

^KT-79528
diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KT79528StdlibDowngradeHangs.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KT79528StdlibDowngradeHangs.kt
new file mode 100644
index 0000000..a5ba7bf
--- /dev/null
+++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KT79528StdlibDowngradeHangs.kt
@@ -0,0 +1,22 @@
+package org.jetbrains.kotlin.gradle
+
+import kotlin.test.Test
+import kotlinx.coroutines.async
+import kotlinx.coroutines.runBlocking
+import org.jetbrains.kotlin.util.assertThrows
+
+class KT79528StdlibDowngradeHangs {
+
+    @Test
+    fun `test KT-79528 - test Gradle doesn't downgrade stdlib and cause uncaught exceptions in coroutines to hang coroutines machinery`() {
+        class Foo : RuntimeException()
+        assertThrows<Foo> {
+            runBlocking {
+                async {
+                    throw Foo()
+                }.await()
+            }
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/KT79528StdlibDowngradeHangs.kt b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/KT79528StdlibDowngradeHangs.kt
new file mode 100644
index 0000000..6d0e63a
--- /dev/null
+++ b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/KT79528StdlibDowngradeHangs.kt
@@ -0,0 +1,21 @@
+package org.jetbrains.kotlin.gradle.unitTests
+
+import kotlin.test.Test
+import kotlinx.coroutines.async
+import kotlinx.coroutines.runBlocking
+import org.jetbrains.kotlin.util.assertThrows
+
+class KT79528StdlibDowngradeHangs {
+
+    @Test
+    fun `test KT-79528 - test Gradle doesn't downgrade stdlib and cause uncaught exceptions in coroutines to hang coroutines machinery`() {
+        class Foo : RuntimeException()
+        assertThrows<Foo> {
+            runBlocking {
+                async {
+                    throw Foo()
+                }.await()
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/GradleCommon.kt b/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/GradleCommon.kt
index cf47269..57cdf2d 100644
--- a/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/GradleCommon.kt
+++ b/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/GradleCommon.kt
@@ -10,6 +10,7 @@
 import org.gradle.api.GradleException
 import org.gradle.api.Project
 import org.gradle.api.artifacts.Configuration
+import org.gradle.api.artifacts.FileCollectionDependency
 import org.gradle.api.artifacts.ModuleDependency
 import org.gradle.api.artifacts.VersionCatalogsExtension
 import org.gradle.api.artifacts.type.ArtifactTypeDefinition
@@ -31,6 +32,7 @@
 import org.gradle.api.tasks.SourceSet
 import org.gradle.api.tasks.TaskProvider
 import org.gradle.api.tasks.compile.JavaCompile
+import org.gradle.api.tasks.testing.Test
 import org.gradle.jvm.tasks.Jar
 import org.gradle.jvm.toolchain.JavaToolchainService
 import org.gradle.kotlin.dsl.*
@@ -808,4 +810,17 @@
                 }
             }
         }
+}
+
+// See KT-79528 and https://github.com/gradle/gradle/issues/34453
+fun Project.avoidStdlibDowngradeInTests() {
+    afterEvaluate {
+        tasks.withType<Test>().configureEach {
+            val gradleEmbeddedStdlib =
+                (dependencies.gradleApi() as FileCollectionDependency).files.single { it.name.startsWith("kotlin-stdlib") }
+            classpath = classpath.filter {
+                it != gradleEmbeddedStdlib
+            }
+        }
+    }
 }
\ No newline at end of file
diff --git a/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-common-configuration.gradle.kts b/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-common-configuration.gradle.kts
index 7a7c5ba..d52f44e 100644
--- a/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-common-configuration.gradle.kts
+++ b/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-common-configuration.gradle.kts
@@ -123,3 +123,5 @@
     )
     publishShadowedJar(gradle813SourceSet, commonSourceSet)
 }
+
+avoidStdlibDowngradeInTests()
\ No newline at end of file
diff --git a/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-dependency-configuration.gradle.kts b/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-dependency-configuration.gradle.kts
index 83e3cff..97ee7bf 100644
--- a/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-dependency-configuration.gradle.kts
+++ b/repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-dependency-configuration.gradle.kts
@@ -86,3 +86,4 @@
     }
 }
 
+avoidStdlibDowngradeInTests()
\ No newline at end of file