<@U0CHHN4F4> Hey! I'm trying out my scripting stuf...
# scripting
t
@ilya.chernikov Hey! I'm trying out my scripting stuff in 1.3.70 now and I still can't get IntelliJ to recognise the implicit receivers. I have the META-INF in the right place (afaik) and I've set
Copy code
ide { acceptedLocations(ScriptAcceptedLocation.Everywhere) }
in the compilation settings
i
I'm trying to find out, what is the actual status of this. Will come back to you as soon as it becomes clearer.
t
okay, thanks a bunch! ๐Ÿ™‚
Hey @ilya.chernikov Any updates on this?
r
@Tmpod Hi, what version of Kotlin Plugin are you using?
t
Not on my computer atm, but I'll tell you exactly as soon as I can, but I can say it's post kotlin 1.3.70
๐Ÿ‘Œ 1
i
@Tmpod I think I understood the problem, sorry that it took that long. I was distracted by the fact that the receivers in IDE, for some reasons appeared broken on my test project too. But now I rechecked, it works for me and I see why it doesn't work in u=your case. so:
In order to make it work with IDE, you have to make the valid script compilation configuration available for the IDE via the annotation. See, e.g.. https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/scriptDef.kt#L32
IDE only sees the configuration which is specified in the
KotlinScript
annotation.
t
Oh I see
Imma try changing that and will report back to you
thanks a lot ๐Ÿ™‚
btw @Roman Golyshev, not needed anymore but still, just updated the kt plugin to 1.3.71-release-IJ2020.
๐Ÿ‘ 1
@ilya.chernikov I specify the implicit receiver class in the compilation config tho https://gitlab.com/Tmpod/Korius/-/blob/dev/src/main/kotlin/dev/tmpod/korius/framework/scripting/ModuleScriptCompiler.kt#L48
I just tried putting the compilation config in a seperate class and pass it to the
KotlinScript
annotation and IntelliJ still can't properly highlight my scripts
i
In my test project I simply modify the example I linked above, adding an implicit receiver there, and it works for me. Let's try to check possible failure pointes: - You need to put the configuration into an object or class inheriting
ScriptCompilationConfiguration
(see
SimpleMainKtsScriptDefinition
in the example) and put a class literal reference to this class to the
@KotlinScript
annotation. (The reason is that the IDE cannot know which configuration you're passing to the
eval
call, so it only checks the one accessible via the annotation.) -
dependenciesFromCurrentContext
could be tricky in IntelliJ, in your case it will take the classpath from IntelliJ itself, which is probably not what you want. In the IDE setting you probably want to find your jars via some other mechanism and put it to dependencies e.g. using
updateClasspath
helper. - make sure that the updated definition is loaded by the IntelliJ. The reloading mechanisms could be unreliable, so try to restart IDE after compiling the new jar. - the implicit receivers are not addressable via
this
, use it's member without qualifier. That's the main failure points I can think of for a moment, please check them. You can also try to browse the IntelliJ log file (check the
Help
menu) for the lines containing
[kts]
and check whether there are any errors there.
๐Ÿ‘ 1
t
not very sure what you mean with the third point, could you expand on it, please?
i
You mean - reloading the definition"? Then you change a script definition, you usually produce an updated jar with it, which you should provide to the IntelliJ. There are 3 ways - via module dependencies (works only for scripts in source roots), via IntelliJ plugin, or via IntelliJ Kotlin Compiler settings. But the changed jar is not always properly detected by the IntelliJ, so you can restart the IDE to make sure that the new definition is loaded.
t
Oops, my bad it was the second point (the one about the classpath)... ๐Ÿ˜• Thanks for that response though
i
This is a part of your definition:
Copy code
jvm {
            dependenciesFromCurrentContext(wholeClasspath = true)
        }
It means that on the creating this configuration (eagerly) the
dependenciesFromCurrentContext
helper will try to extract the whole classpath from the current context (via system properties or thread context classloader) and put all the extracted classpath as script compilation and evaluation dependencies, using
dependencies
configuration key. In your runtime environment it works as expected, because you creating the configuration in the correct runtime environment. But if the same configuration is created in the IntelliJ, it will take classpath of the IDE itself, so if your jar with the definition requires external dependencies, (in particular for the implicit receiver), then it will not be found, that may lead to the red code.
Not that if these dependencies are provided to the IDE via some other means (e.g. if they are part of the script definition classpath, or module classpath) then everything should be fine.
t
@ilya.chernikov sorry for super late reply but I've been busy with other stuff ๐Ÿ˜• anyways, I see the issue. The receiver is found when actually running the app because that would be the right environment, which IntelliJ can't really guess. I'm still not sure what I should do though. How can I pass the proper classpath to
updateClasspath
? thanks a lot for your support so far ๐Ÿ™‚
i
I can only suggest either pack required functionality into the jar with the definition, or to use some mechanism of jar searching and put it into a refinement callaback, similar to the maven/ivy resolving in the main-kts.