Skip to content

Commit 8607d90

Browse files
committed
Handle unset thread local parameters in MainComponentRegistrar and MainCommandLineProcessor gracefully. Resolves #301
1 parent 8ae3922 commit 8607d90

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ class KotlinCompilation : AbstractKotlinCompilation<K2JVMCompilerArguments>() {
418418
/** The main compiler plugin (MainComponentRegistrar)
419419
* is instantiated by K2JVMCompiler using
420420
* a service locator. So we can't just pass parameters to it easily.
421-
* Instead we need to use a thread-local global variable to pass
421+
* Instead, we need to use a thread-local global variable to pass
422422
* any parameters that change between compilations
423423
*
424424
*/

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ internal class MainCommandLineProcessor : CommandLineProcessor {
1313

1414
override val pluginOptions: Collection<AbstractCliOption>
1515
get() = threadLocalParameters.get()?.pluginOptions
16-
?: error("MainCommandLineProcessor::pluginOptions accessed before thread local parameters have been set")
16+
?: emptyList<AbstractCliOption>().also {
17+
// Handle unset parameters gracefully because this plugin may be accidentally called by other tools that
18+
// discover it on the classpath (for example the kotlin jupyter kernel).
19+
System.err.println("WARNING: MainCommandLineProcessor::pluginOptions accessed before thread local parameters have been set")
20+
}
1721

1822
companion object {
1923
const val pluginId = "com.tschuchort.compiletesting.maincommandlineprocessor"
2024

2125
/** This CommandLineProcessor is instantiated by K2JVMCompiler using
2226
* a service locator. So we can't just pass parameters to it easily.
23-
* Instead we need to use a thread-local global variable to pass
27+
* Instead, we need to use a thread-local global variable to pass
2428
* any parameters that change between compilations
2529
*/
2630
val threadLocalParameters: ThreadLocal<ThreadLocalParameters> = ThreadLocal()
@@ -67,6 +71,13 @@ internal class MainCommandLineProcessor : CommandLineProcessor {
6771
}
6872

6973
override fun processOption(option: AbstractCliOption, value: String, configuration: CompilerConfiguration) {
74+
// Handle unset parameters gracefully because this plugin may be accidentally called by other tools that
75+
// discover it on the classpath (for example the kotlin jupyter kernel).
76+
if (threadLocalParameters.get() == null) {
77+
System.err.println("WARNING: MainCommandLineProcessor::processOption accessed before thread local parameters have been set")
78+
return
79+
}
80+
7081
val (foreignPluginId, foreignOptionName) = decodeForeignOptionName(option.optionName)
7182
val params = threadLocalParameters.get()
7283

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ import org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor
2727
internal class MainComponentRegistrar : ComponentRegistrar {
2828

2929
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
30+
// Handle unset parameters gracefully because this plugin may be accidentally called by other tools that
31+
// discover it on the classpath (for example the kotlin jupyter kernel).
32+
if (threadLocalParameters.get() == null) {
33+
System.err.println("WARNING: MainComponentRegistrar::registerProjectComponents accessed before thread local parameters have been set")
34+
return
35+
}
36+
3037
val parameters = threadLocalParameters.get()
3138

3239
/*

0 commit comments

Comments
 (0)