Skip to content

Commit 1ed006e

Browse files
committed
Fixed compiler tests classpath
1 parent 0f7fcbf commit 1ed006e

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

compiler-plugin/compiler-plugin-k2/build.gradle.kts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
56
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
67
import util.enableContextReceivers
78
import util.otherwise
@@ -10,6 +11,49 @@ import util.whenForIde
1011
plugins {
1112
alias(libs.plugins.conventions.jvm)
1213
alias(libs.plugins.compiler.specific.module)
14+
alias(libs.plugins.shadow.jar)
15+
}
16+
17+
/*
18+
This is a hack to solve the next problem:
19+
20+
There is PsiElement class that is used in compiler and plugin.
21+
22+
If we use kotlin-compiler-embeddable.jar to compile the module,
23+
PsiElement is org.jetbrains.kotlin.com.jetbrains.intellij.PsiElement.
24+
And it works ok in the user projects!
25+
26+
But.
27+
28+
If we run tests, which use kotlin-compiler.jar, we run into ClassNotFoundException.
29+
Because the class it has in th classpath is com.jetbrains.intellij.PsiElement
30+
31+
- Alright, we can use kotlin-compiler.jar to compile the plugin.
32+
- No, because the same error now will occur in the user projects,
33+
but it is com.jetbrains.intellij.PsiElement that is not found.
34+
35+
- Ok, we can use kotlin-compiler-embeddable.jar to compile tests.
36+
- Then we ran into java.lang.VerifyError: Bad type on operand stack, which I have no idea how to fix.
37+
38+
This solution replaces org.jetbrains.kotlin.com.jetbrains.intellij.PsiElement usages in plugin
39+
with com.jetbrains.intellij.PsiElement only for the tests, fixing both use cases.
40+
It is basically a reverse engineering of what Kotlin does for the embedded jar.
41+
*/
42+
val shadowJar = tasks.named<ShadowJar>("shadowJar") {
43+
configurations = listOf(project.configurations.compileClasspath.get())
44+
relocate("org.jetbrains.kotlin.com.intellij.psi", "com.intellij.psi")
45+
46+
exclude("javaslang/**")
47+
exclude("kotlin/**")
48+
exclude("messages/**")
49+
exclude("misc/**")
50+
exclude("org/**")
51+
52+
archiveFileName.set("plugin-k2-for-tests.jar")
53+
}
54+
55+
tasks.jar {
56+
finalizedBy(shadowJar)
1357
}
1458

1559
kotlin {

tests/compiler-plugin-tests/build.gradle.kts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ kotlin {
2727
val testDataClasspath: Configuration by configurations.creating
2828
val testRuntimeClasspath: Configuration by project.configurations.getting
2929

30+
val globalRootDir: String by extra
31+
3032
/**
3133
* I should probably explain this.
3234
*
@@ -36,7 +38,7 @@ val testRuntimeClasspath: Configuration by project.configurations.getting
3638
*
3739
* `NioFiles` is problematic.
3840
* It was packed with kotlin-compiler jar, but Proguard which excluded `deleteRecursively` method from it.
39-
* It this method is called.
41+
* And this method is called.
4042
* So tests fail with:
4143
* ```
4244
* java.lang.NoSuchMethodError: com.intellij.openapi.util.io.NioFiles.deleteRecursively(Ljava/nio/file/Path;)V
@@ -62,7 +64,6 @@ sourceSets.test.configure {
6264
}
6365

6466
dependencies {
65-
@Suppress("UnstableApiUsage")
6667
testPriorityRuntimeClasspath(libs.intellij.util) { isTransitive = false }
6768

6869
implementation(projects.core)
@@ -79,7 +80,10 @@ dependencies {
7980
testImplementation(libs.serialization.plugin)
8081
}
8182

82-
testImplementation(libs.compiler.plugin.cli)
83+
testImplementation(libs.compiler.plugin.cli) {
84+
exclude(group = "org.jetbrains.kotlinx", module = "compiler-plugin-k2")
85+
}
86+
testImplementation(files("$globalRootDir/compiler-plugin/compiler-plugin-k2/build/libs/plugin-k2-for-tests.jar"))
8387

8488
testImplementation(libs.kotlin.reflect)
8589
testImplementation(libs.kotlin.compiler)
@@ -99,8 +103,6 @@ dependencies {
99103
testDataClasspath(libs.serialization.core)
100104
}
101105

102-
val globalRootDir: String by extra
103-
104106
val updateTestData = (project.findProperty("kotlin.test.update.test.data") as? String) ?: "false"
105107

106108
tasks.test {
@@ -154,7 +156,7 @@ fun Test.setJarPathAsProperty(
154156

155157
val path = testRuntimeClasspath
156158
.files
157-
.first { includedRegex.matches(it.name) }
159+
.firstOrNull { includedRegex.matches(it.name) }
158160
?: run {
159161
logger.warn("Can't find $jarName in testRuntimeClasspath configuration")
160162
return

versions-root/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ junit5 = "5.11.3"
2020
intellij = "233.13135.128"
2121
gradle-doctor = "0.10.0"
2222
kotlinx-browser = "0.2"
23+
shadow-jar = "9.0.0-beta2"
2324

2425
# Stub versions – relpaced based on kotlin, mostly for gradle-related (plugins) dependencies
2526
# but also for dependencies for compiler-specific modules.
@@ -112,6 +113,7 @@ atomicfu = { id = "org.jetbrains.kotlinx.atomicfu", version.ref = "atomicfu" }
112113
gradle-kotlin-dsl = { id = "org.gradle.kotlin.kotlin-dsl", version.ref = "gradle-kotlin-dsl" }
113114
kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }
114115
gradle-plugin-publish = { id = "com.gradle.plugin-publish", version.ref = "gradle-plugin-publish" }
116+
shadow-jar = { id = "com.gradleup.shadow", version.ref = "shadow-jar" }
115117

116118
# gradle-conventions project
117119
conventions-common = { id = "conventions-common", version.ref = "kotlinx-rpc" }

0 commit comments

Comments
 (0)