Skip to content

Commit

Permalink
Add MainComponentRegistrar to classpath only when plugins are given. R…
Browse files Browse the repository at this point in the history
…esolves #302
  • Loading branch information
tschuchortdev committed Aug 12, 2022
1 parent 2eef5ff commit ee44c53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c

var languageVersion: String? = null

/** Use the new experimental K2 compiler */
var useK2: Boolean by default { false }

/** Additional string arguments to the Kotlin compiler */
var kotlincArguments: List<String> = emptyList()

Expand Down Expand Up @@ -126,6 +129,7 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
args.allWarningsAsErrors = allWarningsAsErrors
args.reportOutputFiles = reportOutputFiles
args.reportPerf = reportPerformance
args.useK2 = useK2

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

val compilerMessageCollector = PrintingMessageCollector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class MainComponentRegistrar : ComponentRegistrar {
companion object {
/** This compiler plugin is instantiated by K2JVMCompiler using
* a service locator. So we can't just pass parameters to it easily.
* Instead we need to use a thread-local global variable to pass
* Instead, we need to use a thread-local global variable to pass
* any parameters that change between compilations
*/
val threadLocalParameters: ThreadLocal<ThreadLocalParameters> = ThreadLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,5 +904,18 @@ class KotlinCompilationTests {
}
}

@Test
fun `runs the K2 compiler without compiler plugins`() {
val result = defaultCompilerConfig().apply {
sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource"))
compilerPlugins = emptyList()
pluginClasspaths = emptyList()
useK2 = true
}.compile()

assertThat(result.exitCode).isEqualTo(ExitCode.OK)
assertClassLoadable(result, "KSource")
}

class InheritedClass {}
}

0 comments on commit ee44c53

Please sign in to comment.