hello. question about custom .kts scripting w/ `@K...
# scripting
r
hello. question about custom .kts scripting w/
@KotlinScript
:
i built a module that contains a script definition using
@KotlinScript
, and am having classpath issues in the consuming module
Copy code
class FnScriptDefinition : ScriptCompilationConfiguration({
    ide {
        acceptedLocations(ScriptAcceptedLocation.Everywhere)
    }

    jvm {
        baseClass(FnScriptTemplate::class)
        dependenciesFromClassloader(wholeClasspath = true)
    }

    defaultImports(FnScriptTemplate::class)
})
my configuration ^^^
everything works, but if i add dependencies in the consuming gradle project, they are unavailable (at least in the IDE) to the script
Copy code
@KotlinScript(
    fileExtension = "fn.kts",
    compilationConfiguration = FnScriptDefinition::class
)
abstract class FnScriptTemplate
my template
is there something i need to add to
FnScriptDefinition
to allow the scripts to use host project's classpath?
hmmm - i mischaracterized the problem. the host classpath seems available, but the class i'm trying to import is a generated source in the project's
build
directory
i
If you're trying access the classes from the same project in the script, it will work olnly sometimes at the moment. The root problem is the same as here - https://youtrack.jetbrains.com/issue/KT-31176 - we cannot yet index such dependencies right from the sources, because there is no appropriate project infrastructure for script dependencies. So it will work only when the required classes are compiled during the script definition loading.
e
Not sure if the same problem but I work-arounded this in my personal project by adding the task to `build`/`assemble` the jar of the script definition module to run after IDEA Sync (using the gradle-idea-ext-plugin). That way IDEA indexes the jar that contains the script definition and picks it up to use in the module that consumes/depends on the script definition module.
r
thanks for responding - this is actually a different problem
the script definition module is being imported, but the script can only see the module's source classes on the classpath. the host module is also generating code (via protobuf), and the script does not "see" the generated code.
i
Ah, I see. In general -
dependenciesFromClassloader
may cause such problems with IDE support, because the context classloader that will be used in IDE is not the one that is supplied from the host. To make it work properly in IDE and in host, it makes sense to extract classpath creation/collection logic into a function that is called directly from compilation configuration DSL, instead of
dependenciesFromClassloader
. You can do it dynamic too by using
refineConfiguration
calls. See
kotlin-main-kts
as an example.
r
ah i can do it at runtime! excellent. thank you!