kscripting / kscript

Scripting enhancements for Kotlin
MIT License
2.09k stars 126 forks source link

Include interferes with imports of enum instances #296

Open reitzig opened 3 years ago

reitzig commented 3 years ago

Consider this file repro.kts:

import Repro.Foo.Bar

enum class Foo {
    Bar
}

println(Bar)

This runs and prints Bar -- all is well. Now, add an include directive (file can be empty):

@file:Include("shared.kt")

import Repro.Foo.Bar

enum class Foo {
    Bar
}

println(Bar)

And we get an error:

OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.ReflectionUtil to method java.util.ResourceBundle.setParent(java.util.ResourceBundle)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
/tmp/tmp16041171746066319857.tmp/scriptlet.803d758a4961398c.kts:1:8: error: unresolved reference: Repro
import Repro.Foo.Bar
       ^
/tmp/tmp16041171746066319857.tmp/scriptlet.803d758a4961398c.kts:11:9: error: unresolved reference: Bar
println(Bar)
        ^

Reproduces with and without content in shared.kt, and also with comment include directive. Also reproduces with other nested constructs such as inner classes:

sealed class Foo {
    object Bar: Foo()
}

Does not reproduce with IDEA, which runs the files just fine (and created the import statement for me, in face). Therefore, I suspect a bug in kscript.


Real-life example: link


Version : v3.0.2 Kotlin : 1.4.21-release-351 Java : JRE 14.0.2+12)


Reproduces with JRE 11 as well.
reitzig commented 3 years ago

Ah. The problem is the file name of the aggregate file, isn't it? kscript would either have to use the original file names, or rewrite the imports. :thinking:

holgerbrandl commented 3 years ago

It indeed assumed to the included file to be in the same package. It partially rewrites the imports to show up on top of the merged script.