Fix exception when Groovy lazy string (GString) is in freeCompilerArgs
#KT-15500 fixed
GString is an object that represents string literal like `"${project.name}"`
in Groovy. It is not an instance of string.
Groovy automatically converts GString to String when it is passed where String is expected.
However freeCompilerArgs is a List<String>, so type parameter info is lost at runtime.
When iterating freeCompilerArgs in Kotlin as a list of string, an exception
is thrown because GString cannot be casted to String (toString should be called instead).
This commit is contained in:
+19
@@ -346,4 +346,23 @@ class KotlinGradleIT: BaseGradleIT() {
|
|||||||
assertFileExists("libJs/build/classes/test/libJs_test.js")
|
assertFileExists("libJs/build/classes/test/libJs_test.js")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFreeCompilerArgs() {
|
||||||
|
val project = Project("kotlinProject", GRADLE_VERSION)
|
||||||
|
project.setupWorkingDir()
|
||||||
|
|
||||||
|
File(project.projectDir, "build.gradle").modify {
|
||||||
|
// lazy eval is important
|
||||||
|
val customModuleName = "\${project.name}"
|
||||||
|
it + """
|
||||||
|
compileKotlin {
|
||||||
|
kotlinOptions.freeCompilerArgs = [ "-module-name", "$customModuleName" ]
|
||||||
|
}"""
|
||||||
|
}
|
||||||
|
|
||||||
|
project.build("build") {
|
||||||
|
assertSuccessful()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+4
-2
@@ -19,11 +19,13 @@ package org.jetbrains.kotlin.gradle.dsl
|
|||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
|
||||||
import org.jetbrains.kotlin.cli.js.K2JSCompiler
|
import org.jetbrains.kotlin.cli.js.K2JSCompiler
|
||||||
|
|
||||||
internal class KotlinJsOptionsImpl() : KotlinJsOptionsBase() {
|
internal class KotlinJsOptionsImpl : KotlinJsOptionsBase() {
|
||||||
override var freeCompilerArgs: List<String> = listOf()
|
override var freeCompilerArgs: List<String> = listOf()
|
||||||
|
|
||||||
override fun updateArguments(args: K2JSCompilerArguments) {
|
override fun updateArguments(args: K2JSCompilerArguments) {
|
||||||
super.updateArguments(args)
|
super.updateArguments(args)
|
||||||
K2JSCompiler().parseArguments(freeCompilerArgs.toTypedArray(), args)
|
// cast to List<Any> is important because in Groovy a GString can be inside of a list
|
||||||
|
val freeArgsArray = (freeCompilerArgs as List<Any>).map(Any::toString).toTypedArray()
|
||||||
|
K2JSCompiler().parseArguments(freeArgsArray, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -24,6 +24,8 @@ internal class KotlinJvmOptionsImpl : KotlinJvmOptionsBase() {
|
|||||||
|
|
||||||
override fun updateArguments(args: K2JVMCompilerArguments) {
|
override fun updateArguments(args: K2JVMCompilerArguments) {
|
||||||
super.updateArguments(args)
|
super.updateArguments(args)
|
||||||
K2JVMCompiler().parseArguments(freeCompilerArgs.toTypedArray(), args)
|
// cast to List<Any> is important because in Groovy a GString can be inside of a list
|
||||||
|
val freeArgsArray = (freeCompilerArgs as List<Any>).map(Any::toString).toTypedArray()
|
||||||
|
K2JVMCompiler().parseArguments(freeArgsArray, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user