https://kotlinlang.org logo
s

Sergio Pro

07/29/2020, 1:16 PM
Hi! How do I import another script file?
@Import
annotation does not seem to be working for me: `common.main.kts`:
Copy code
#!/usr/bin/env -S kotlinc -script --

val greeting = "Hello"
`sample.main.kts`:
Copy code
#!/usr/bin/env -S kotlinc -script --

@file:org.jetbrains.kotlin.script.util.Import("common.main.kts")

print(greeting) // unresolved reference and nothing to import
Both files are in the same directory and I've tried relative and absolute paths. Am I doing something wrong here? I know it's possible with
kscript
but I would prefer to keep extra dependencies to a minimum. Doesn't kotlin have built-in support for this and I'm totally misunderstanding what
@Import
annotation is supposed to do?
i

ilya.chernikov

07/31/2020, 3:17 PM
Which kotlin version are you playing with? The
Import
annotation should be accessible without FQN in the versions that support it. so it should work as
Copy code
@file:Import("common.main.kts")
e.g. here is how it looks in the tests https://github.com/JetBrains/kotlin/blob/1.3.70/libraries/tools/kotlin-main-kts-test/testData/import-test.main.kts
s

Sergio Pro

07/31/2020, 3:18 PM
I think I've figured it out. It works when I execute the code, it's the IDEA that was complaining because the scripts were not in the "sources" directories.
i

ilya.chernikov

07/31/2020, 3:19 PM
Ah, yes, I see. The IntelliJ support for imports is quite limited, unfortunately.
👍 1
4 Views