BasicJvmScriptingHost imports not working with StringScriptSource

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!

So I found the following snippet in org.jetbrains.kotlin.scripting.compiler.plugin.dependencies.ScriptsCompilationDependenciesKt#collectScriptsCompilationDependencies

val sourceDependenciesRoots = refinedConfiguration.value.importedScripts.mapNotNull {
    // TODO: support any kind of ScriptSource.
    val path = (it as? FileBasedScriptSource)?.file?.absolutePath ?: return@mapNotNull null
    KotlinSourceRoot(path, false)
}

which would explain why the imports are not working. I am still interested in a workaround or any information about when this will be implemented/fixed.

For future reference: I dug into the compilation dependencies code but found no way to add non-file-based script sources as dependencies. I ended up writing my imports to a temporary directory at the start of the app and importing them as FileScriptSources. This works for now but is obviously not an ideal solution.

This problem actually got fixed with 2.3.20 and the code from my original post now works as expected :partying_face: