Hi all! I am trying to use imports with the BasicJvmScriptingHost with StringScriptSource instead of FileScriptSource. Unfortunately it is not working as expected. This is my test code:
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.FileScriptSource
import kotlin.script.experimental.host.StringScriptSource
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
data class CustomScriptSource(
override val text: String,
override val name: String?,
override val locationId: String?
) : SourceCode
fun main() {
val testCode = """
fun test(): TestInput {
return TestInput(1,2)
}
test()
""".trimIndent()
val path = Paths.get("src/main/resources/TestInput.kt")
val stringScriptSource = StringScriptSource(Files.readString(path), "TestInput.kt")
val compilationConfigStringSource = ScriptCompilationConfiguration {
importScripts(listOf(stringScriptSource))
}
val fileScriptSource = FileScriptSource(path.toFile())
val compilationConfigFileSource = ScriptCompilationConfiguration {
importScripts(listOf(fileScriptSource))
}
val customScriptSource =
CustomScriptSource(Files.readString(path), "TestInput.kt", "src\\main\\resources\\TestInput.kt")
val compilationConfigurationCustomSource = ScriptCompilationConfiguration {
importScripts(listOf(customScriptSource))
}
val codeSource = StringScriptSource(testCode)
val scriptingHost = BasicJvmScriptingHost()
val evaluationConfig = ScriptEvaluationConfiguration {}
println("\tWith FileScriptSource:")
printResult(scriptingHost.eval(codeSource, compilationConfigFileSource, evaluationConfig))
println()
println("\tWith StringScriptSource:")
printResult(scriptingHost.eval(codeSource, compilationConfigStringSource, evaluationConfig))
println()
println("\tWith CustomScriptSource:")
printResult(scriptingHost.eval(codeSource, compilationConfigurationCustomSource, evaluationConfig))
println()
println("\tWith no imports:")
printResult(scriptingHost.eval(codeSource, ScriptCompilationConfiguration {}, evaluationConfig))
}
private fun printResult(result: ResultWithDiagnostics<EvaluationResult>) {
if (result is ResultWithDiagnostics.Success) {
println("SUCCESS")
println(result.value.returnValue)
} else {
println("FAIL")
}
result.reports.filter { it.severity > ScriptDiagnostic.Severity.DEBUG }
.forEach {
println(it.severity.toString() + ": " + it.message + " in " + it.sourcePath + " at " + it.location?.start?.let { "${it.line}:${it.col}" } + " to " + it.location?.end?.let { "${it.line}:${it.col}" })
}
}
with TestInput.kt simply containing data class TestInput(val a: Int, val b: Int)
. This produces
With FileScriptSource:
SUCCESS
$$result: <root>.TestInput = TestInput(a=1, b=2)
With StringScriptSource:
FAIL
ERROR: Unresolved reference: TestInput in script.kts at 1:13 to 1:22
ERROR: Unresolved reference: TestInput in script.kts at 2:12 to 2:21
With CustomScriptSource:
FAIL
ERROR: Unresolved reference: TestInput in script.kts at 1:13 to 1:22
ERROR: Unresolved reference: TestInput in script.kts at 2:12 to 2:21
With no imports:
FAIL
ERROR: Unresolved reference: TestInput in script.kts at 1:13 to 1:22
ERROR: Unresolved reference: TestInput in script.kts at 2:12 to 2:21
Does anyone have an idea why StringScriptSource and the CustomScriptSource are not working? I am using kotlin, kotlin-scripting-jvm and kotlin-scripting-jvm-host all at version 1.6.21. I also tried with a import TestInput
at the start of the test code: No difference.
Thanks for any input!