Compare commits

...

1 Commits

Author SHA1 Message Date
Alexey Tsvetkov
fef38710c7 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).
2017-01-30 21:19:30 +03:00
3 changed files with 26 additions and 3 deletions

View File

@@ -346,4 +346,23 @@ class KotlinGradleIT: BaseGradleIT() {
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()
}
}
}

View File

@@ -19,11 +19,13 @@ package org.jetbrains.kotlin.gradle.dsl
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.js.K2JSCompiler
internal class KotlinJsOptionsImpl() : KotlinJsOptionsBase() {
internal class KotlinJsOptionsImpl : KotlinJsOptionsBase() {
override var freeCompilerArgs: List<String> = listOf()
override fun updateArguments(args: K2JSCompilerArguments) {
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)
}
}

View File

@@ -24,6 +24,8 @@ internal class KotlinJvmOptionsImpl : KotlinJvmOptionsBase() {
override fun updateArguments(args: K2JVMCompilerArguments) {
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)
}
}