Skip to content

Commit ee44c53

Browse files
committed
Add MainComponentRegistrar to classpath only when plugins are given. Resolves #302
1 parent 2eef5ff commit ee44c53

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
8888

8989
var languageVersion: String? = null
9090

91+
/** Use the new experimental K2 compiler */
92+
var useK2: Boolean by default { false }
93+
9194
/** Additional string arguments to the Kotlin compiler */
9295
var kotlincArguments: List<String> = emptyList()
9396

@@ -126,6 +129,7 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
126129
args.allWarningsAsErrors = allWarningsAsErrors
127130
args.reportOutputFiles = reportOutputFiles
128131
args.reportPerf = reportPerformance
132+
args.useK2 = useK2
129133

130134
if (languageVersion != null)
131135
args.languageVersion = this.languageVersion
@@ -191,7 +195,16 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
191195
} else {
192196
emptyList()
193197
}
194-
args.pluginClasspaths = (args.pluginClasspaths ?: emptyArray()) + arrayOf(getResourcesPath())
198+
args.pluginClasspaths = (args.pluginClasspaths ?: emptyArray()) +
199+
/** The resources path contains the MainComponentRegistrar and MainCommandLineProcessor which will
200+
be found by the Kotlin compiler's service loader. We add it only when the user has actually given
201+
us ComponentRegistrar instances to be loaded by the MainComponentRegistrar because the experimental
202+
K2 compiler doesn't support plugins yet. This way, users of K2 can prevent MainComponentRegistrar
203+
from being loaded and crashing K2 by setting both [compilerPlugins] and [commandLineProcessors] to
204+
the emptyList. */
205+
if (compilerPlugins.isNotEmpty() || commandLineProcessors.isNotEmpty())
206+
arrayOf(getResourcesPath())
207+
else emptyArray()
195208
}
196209

197210
val compilerMessageCollector = PrintingMessageCollector(

core/src/main/kotlin/com/tschuchort/compiletesting/MainComponentRegistrar.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal class MainComponentRegistrar : ComponentRegistrar {
4747
companion object {
4848
/** This compiler plugin is instantiated by K2JVMCompiler using
4949
* a service locator. So we can't just pass parameters to it easily.
50-
* Instead we need to use a thread-local global variable to pass
50+
* Instead, we need to use a thread-local global variable to pass
5151
* any parameters that change between compilations
5252
*/
5353
val threadLocalParameters: ThreadLocal<ThreadLocalParameters> = ThreadLocal()

core/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt

+13
Original file line numberDiff line numberDiff line change
@@ -904,5 +904,18 @@ class KotlinCompilationTests {
904904
}
905905
}
906906

907+
@Test
908+
fun `runs the K2 compiler without compiler plugins`() {
909+
val result = defaultCompilerConfig().apply {
910+
sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource"))
911+
compilerPlugins = emptyList()
912+
pluginClasspaths = emptyList()
913+
useK2 = true
914+
}.compile()
915+
916+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
917+
assertClassLoadable(result, "KSource")
918+
}
919+
907920
class InheritedClass {}
908921
}

0 commit comments

Comments
 (0)