How can I run Kotlin-Script (*.kts) filles from within Gradle?

How can I run Kotlin-Script (*.kts) filles from within Gradle?

From the command line, I can call:
kotlinc -script foo.kts

How can I do this from gradle (build.gradle)?

Hello, Richard.

If I understand you correctly, you want to execute an arbitrary Kotlin script from a Gradle build script, right?

If that’s the case, a somewhat straightforward way would be to drive kotlinc (org.jetbrains.kotlin.cli.jvm.K2JVMCompiler) directly
as exemplified by this gist: https://gist.github.com/bamboo/f29e738c2a17a36e87c814b7452afe31

A simpler, more performant and more manageable solution would be to use a Kotlin build script (build.gradle.kts) and express the intent of each separate script as one or more tasks.

// build.gradle.kts
//
// Execute Kotlin task with
//
//    gradle -b build.gradle.kts -q foo
//

task("foo") {
  group = "My"
  description = "Foos away"
  doLast {
    println("Hello from foo task")
  }
}

Let me know if that answers your question.

Cheers,
Rodrigo

Hello Rodrigo,

I tried to run the build.gradle.kts file from the gist, but got many errors (see below). What am I doing wrong?

Thanks.
Richard

Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$ ls
build.gradle.kts
Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$ cat build.gradle.kts
// Execute Kotlin script with:
//
// gradle -q foo
//

// For correctness, we’re going configure two independent classpath configurations, one for kotlinc and the other for the
// script(s) we want to execute.

configurations {
kotlinc
scripts
}

ext.kotlinVersion = “1.1.0-rc-91”

repositories {
maven { url ‘http://dl.bintray.com/kotlin/kotlin-eap-1.1’ }
jcenter()
}

def kotlinModule(String name) {
“org.jetbrains.kotlin:kotlin-$name:$kotlinVersion”
}

dependencies {

kotlinc(kotlinModule(“compiler”))
kotlinc(kotlinModule(“stdlib”))
kotlinc(kotlinModule(“runtime”))
kotlinc(kotlinModule(“reflect”))
kotlinc(kotlinModule(“script-runtime”))

// Our simple script depends only on the stdlib but additional dependencies could be
// added to this configuration.
scripts(kotlinModule(“stdlib”))
}

def kotlinHome = file("$buildDir/kotlin")

task prepareKotlinHome(type: Copy) {
from(configurations.kotlinc)
into("$kotlinHome/lib")
rename {
// strip the version suffix to satisfy the compiler
(it - “-${kotlinVersion}.jar”) + “.jar”
}
}

task foo(type: JavaExec) {
group "My"
description “Executes foo.kts”

dependsOn prepareKotlinHome
main = “org.jetbrains.kotlin.cli.jvm.K2JVMCompiler"
classpath = configurations.kotlinc
args(
”-kotlin-home", kotlinHome,
"-classpath", configurations.scripts.asPath,
"-script", “foo.kts”)
}

Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$
Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$
Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$ gradle -version

Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$ gradle -version


Gradle 3.4

Build time: 2017-02-20 14:49:26 UTC
Revision: 73f32d68824582945f5ac1810600e8d87794c3d4

Groovy: 2.4.7
Ant: Apache Ant™ version 1.9.6 compiled on June 29 2015
JVM: 1.8.0_101 (Oracle Corporation 25.101-b13)
OS: Mac OS X 10.11.6 x86_64

Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$
Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$
Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$ kotlin -version
Kotlin version 1.1.0-rc-91 (JRE 1.8.0_101-b13)
Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$
Richards-MacBook-Pro:gradle-run-kotlinc richardcatlin$ gradle -b build.gradle.kts -q foo
Error: Unexpected tokens (use ‘;’ to separate expressions on the same line) (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (17:15))
Error: Expecting an element (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (21:29))
Error: Expecting an element (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (38:16))
Error: Expecting ‘)’ (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (40:28))
Error: Unexpected tokens (use ‘;’ to separate expressions on the same line) (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (40:28))
Error: Expecting ‘)’ (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (49:14))
Error: Unexpected tokens (use ‘;’ to separate expressions on the same line) (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (49:14))
Error: Unexpected tokens (use ‘;’ to separate expressions on the same line) (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (50:9))
Error: Unexpected tokens (use ‘;’ to separate expressions on the same line) (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (51:15))
Error: Type mismatch: inferred type is () → ??? but Closure<(raw) Any!>! was expected (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (9:16))
Error: Unresolved reference: kotlinc (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (10:3))
Error: Unresolved reference: scripts (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (11:3))
Error: Unresolved reference: ext (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (14:1))
Error: Unresolved reference: def (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (21:1))
Error: Unresolved reference: name (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (21:25))
Error: Unresolved reference: kotlinVersion (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (22:39))
Error: Unresolved reference: kotlinc (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (27:3))
Error: Unresolved reference: kotlinc (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (28:3))
Error: Unresolved reference: kotlinc (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (29:3))
Error: Unresolved reference: kotlinc (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (30:3))
Error: Unresolved reference: kotlinc (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (31:3))
Error: Unresolved reference: scripts (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (35:3))
Error: Unresolved reference: def (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (38:1))
Error: Function invocation ‘task(…)’ expected (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (40:1))
Error: None of the following functions can be called with the arguments supplied:
public open fun task(p0: String!): Task! defined in Build_gradle
public open fun task(p0: String!, p1: Closure<(raw) Any!>!): Task! defined in Build_gradle
public open fun task(p0: (Mutable)Map<String!, *>!, p1: String!): Task! defined in Build_gradle
public open fun task(p0: (Mutable)Map<String!, *>!, p1: String!, p2: Closure<(raw) Any!>!): Task! defined in Build_gradle (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (40:1))
Error: Unresolved reference: type (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (40:24))
Error: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun CopySourceSpec.from(source: Any, action: CopySpec.() → Unit): CopySourceSpec defined in org.gradle.script.lang.kotlin
public fun CopySpec.from(source: Any, action: CopySpec.() → Unit): CopySpec defined in org.gradle.script.lang.kotlin
public fun AbstractCopyTask.from(source: Any, action: CopySpec.() → Unit): AbstractCopyTask defined in org.gradle.script.lang.kotlin (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (41:3))
Error: Unresolved reference: kotlinc (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (41:23))
Error: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun CopySpec.into(target: Any, action: CopySpec.() → Unit): CopySpec defined in org.gradle.script.lang.kotlin
public fun AbstractCopyTask.into(target: Any, action: CopySpec.() → Unit): CopySpec defined in org.gradle.script.lang.kotlin
public fun AbstractArchiveTask.into(target: Any, action: CopySpec.() → Unit): CopySpec defined in org.gradle.script.lang.kotlin (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (42:3))
Error: Unresolved reference: kotlinHome (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (42:10))
Error: Unresolved reference: rename (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (43:3))
Error: Unresolved reference: it (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (45:6))
Error: Unresolved reference: kotlinVersion (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (45:15))
Error: Function invocation ‘task(…)’ expected (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (49:1))
Error: None of the following functions can be called with the arguments supplied:
public open fun task(p0: String!): Task! defined in Build_gradle
public open fun task(p0: String!, p1: Closure<(raw) Any!>!): Task! defined in Build_gradle
public open fun task(p0: (Mutable)Map<String!, *>!, p1: String!): Task! defined in Build_gradle
public open fun task(p0: (Mutable)Map<String!, *>!, p1: String!, p2: Closure<(raw) Any!>!): Task! defined in Build_gradle (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (49:1))
Error: Unresolved reference: type (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (49:10))
Error: Unresolved reference: dependsOn (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (53:3))
Error: Variable expected (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (53:3))
Error: Unresolved reference: main (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (54:3))
Error: Unresolved reference: classpath (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (55:3))
Error: Unresolved reference: kotlinc (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (55:30))
Error: Unresolved reference: args (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (56:3))
Error: Unresolved reference: kotlinHome (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (57:21))
Error: Unresolved reference: scripts (/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (58:34))
Error: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Error type encountered: [ERROR : Cannot be inferred] (ErrorTypeImpl).
Cause: Error type encountered: [ERROR : Cannot be inferred] (ErrorTypeImpl).
File being compiled and position: (9,16) in /Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts
PsiElement: {
kotlinc
scripts
}
The root cause was thrown at: KotlinTypeMapper.java:118
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:312)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:275)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:318)
at org.jetbrains.kotlin.codegen.CallGenerator$DefaultCallGenerator.genValueAndPut(CallGenerator.kt:57)
at org.jetbrains.kotlin.codegen.CallBasedArgumentGenerator.generateExpression(CallBasedArgumentGenerator.java:73)
at org.jetbrains.kotlin.codegen.ArgumentGenerator.generate(ArgumentGenerator.kt:59)
at org.jetbrains.kotlin.codegen.CallBasedArgumentGenerator.generate(CallBasedArgumentGenerator.java:60)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2847)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2820)
at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:44)
at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:23)
at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:65)
at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:111)
at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:104)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.putStackValue(ExpressionCodegen.java:338)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:323)
at org.jetbrains.kotlin.codegen.MemberCodegen.generateInitializers(MemberCodegen.java:427)
at org.jetbrains.kotlin.codegen.ScriptCodegen.genConstructor(ScriptCodegen.java:203)
at org.jetbrains.kotlin.codegen.ScriptCodegen.generateBody(ScriptCodegen.java:102)
at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:124)
at org.jetbrains.kotlin.codegen.PackageCodegen.generateFile(PackageCodegen.java:115)
at org.jetbrains.kotlin.codegen.PackageCodegen.generate(PackageCodegen.java:63)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.generatePackage(KotlinCodegenFacade.java:99)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.doGenerateFiles(KotlinCodegenFacade.java:77)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:44)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:417)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.kt:328)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.kt:532)
at org.gradle.script.lang.kotlin.support.KotlinCompilerKt.compileKotlinScriptToDirectory(KotlinCompiler.kt:67)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler$compileTo$1.invoke(CachingKotlinCompiler.kt:103)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler$compileTo$1.invoke(CachingKotlinCompiler.kt:44)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.withProgressLoggingFor(CachingKotlinCompiler.kt:149)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.compileTo(CachingKotlinCompiler.kt:101)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.access$compileTo(CachingKotlinCompiler.kt:44)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler$compileWithCache$cacheDir$2.execute(CachingKotlinCompiler.kt:86)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler$compileWithCache$cacheDir$2.execute(CachingKotlinCompiler.kt:44)
at org.gradle.cache.internal.DefaultPersistentDirectoryCache$Initializer.initialize(DefaultPersistentDirectoryCache.java:92)
at org.gradle.cache.internal.FixedSharedModeCrossProcessCacheAccess$1.run(FixedSharedModeCrossProcessCacheAccess.java:73)
at org.gradle.cache.internal.DefaultFileLockManager$DefaultFileLock.doWriteAction(DefaultFileLockManager.java:184)
at org.gradle.cache.internal.DefaultFileLockManager$DefaultFileLock.writeFile(DefaultFileLockManager.java:174)
at org.gradle.cache.internal.FixedSharedModeCrossProcessCacheAccess.open(FixedSharedModeCrossProcessCacheAccess.java:71)
at org.gradle.cache.internal.DefaultCacheAccess.open(DefaultCacheAccess.java:130)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:55)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:30)
at org.gradle.cache.internal.DefaultCacheFactory.doOpen(DefaultCacheFactory.java:89)
at org.gradle.cache.internal.DefaultCacheFactory.open(DefaultCacheFactory.java:63)
at org.gradle.cache.internal.DefaultCacheRepository$PersistentCacheBuilder.open(DefaultCacheRepository.java:116)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.compileWithCache(CachingKotlinCompiler.kt:88)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.compileBuildScript(CachingKotlinCompiler.kt:70)
at org.gradle.script.lang.kotlin.provider.KotlinBuildScriptCompiler.compileScriptFile(KotlinBuildScriptCompiler.kt:137)
at org.gradle.script.lang.kotlin.provider.KotlinBuildScriptCompiler.access$compileScriptFile(KotlinBuildScriptCompiler.kt:38)
at org.gradle.script.lang.kotlin.provider.KotlinBuildScriptCompiler$onePassScript$1.invoke(KotlinBuildScriptCompiler.kt:99)
at org.gradle.script.lang.kotlin.provider.KotlinBuildScriptCompiler$onePassScript$1.invoke(KotlinBuildScriptCompiler.kt:38)
at org.gradle.script.lang.kotlin.provider.KotlinScriptPlugin.apply(KotlinScriptPlugin.kt:40)
at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:39)
at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:26)
at org.gradle.configuration.project.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34)
at org.gradle.configuration.project.LifecycleProjectEvaluator.doConfigure(LifecycleProjectEvaluator.java:70)
at org.gradle.configuration.project.LifecycleProjectEvaluator.access$000(LifecycleProjectEvaluator.java:33)
at org.gradle.configuration.project.LifecycleProjectEvaluator$1.execute(LifecycleProjectEvaluator.java:53)
at org.gradle.configuration.project.LifecycleProjectEvaluator$1.execute(LifecycleProjectEvaluator.java:50)
at org.gradle.internal.Transformers$4.transform(Transformers.java:169)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:106)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:61)
at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:50)
at org.gradle.api.internal.project.DefaultProject.evaluate(DefaultProject.java:599)
at org.gradle.api.internal.project.DefaultProject.evaluate(DefaultProject.java:125)
at org.gradle.execution.TaskPathProjectEvaluator.configure(TaskPathProjectEvaluator.java:35)
at org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:60)
at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:38)
at org.gradle.initialization.DefaultGradleLauncher$ConfigureBuildAction.execute(DefaultGradleLauncher.java:233)
at org.gradle.initialization.DefaultGradleLauncher$ConfigureBuildAction.execute(DefaultGradleLauncher.java:230)
at org.gradle.internal.Transformers$4.transform(Transformers.java:169)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:106)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:56)
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:160)
at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:119)
at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:102)
at org.gradle.launcher.exec.GradleBuildController.run(GradleBuildController.java:71)
at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:41)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)
at org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:75)
at org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:49)
at org.gradle.tooling.internal.provider.ServicesSetupBuildActionExecuter.execute(ServicesSetupBuildActionExecuter.java:49)
at org.gradle.tooling.internal.provider.ServicesSetupBuildActionExecuter.execute(ServicesSetupBuildActionExecuter.java:31)
at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:67)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:47)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72)
at org.gradle.util.Swapper.swap(Swapper.java:38)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.LogAndCheckHealth.execute(LogAndCheckHealth.java:55)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:60)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:72)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50)
at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:297)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Error type encountered: [ERROR : Cannot be inferred] (ErrorTypeImpl).
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper$1.processErrorType(KotlinTypeMapper.java:118)
at org.jetbrains.kotlin.load.kotlin.TypeSignatureMappingKt.mapType(typeSignatureMapping.kt:87)
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.mapType(KotlinTypeMapper.java:417)
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.writeGenericArguments(KotlinTypeMapper.java:541)
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.writeGenericType(KotlinTypeMapper.java:492)
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.access$500(KotlinTypeMapper.java:94)
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper$2.invoke(KotlinTypeMapper.java:422)
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper$2.invoke(KotlinTypeMapper.java:419)
at org.jetbrains.kotlin.load.kotlin.TypeSignatureMappingKt.mapType(typeSignatureMapping.kt:62)
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.mapType(KotlinTypeMapper.java:417)
at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.mapSupertype(KotlinTypeMapper.java:373)
at org.jetbrains.kotlin.codegen.ClosureCodegen.generateDeclaration(ClosureCodegen.java:147)
at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:122)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genClosure(ExpressionCodegen.java:1613)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genClosure(ExpressionCodegen.java:1587)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitLambdaExpression(ExpressionCodegen.java:1578)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitLambdaExpression(ExpressionCodegen.java:120)
at org.jetbrains.kotlin.psi.KtLambdaExpression.accept(KtLambdaExpression.java:37)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:293)
… 115 more
(/Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts (9:16))

FAILURE: Build failed with an exception.

  • What went wrong:
    A problem occurred configuring root project ‘gradle-run-kotlinc’.

Could not open cache directory bqygmo56ibmprels45ez10c7h (/Users/richardcatlin/.gradle/caches/3.4/gradle-script-kotlin/bqygmo56ibmprels45ez10c7h).
Internal error: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Error type encountered: [ERROR : Cannot be inferred] (ErrorTypeImpl).
Cause: Error type encountered: [ERROR : Cannot be inferred] (ErrorTypeImpl).
File being compiled and position: (9,16) in /Users/richardcatlin/workspace-catlin/gradle-run-kotlinc/build.gradle.kts
PsiElement: {
kotlinc
scripts
}
The root cause was thrown at: KotlinTypeMapper.java:118
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:312)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:275)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:318)
at org.jetbrains.kotlin.codegen.CallGenerator$DefaultCallGenerator.genValueAndPut(CallGenerator.kt:57)
at org.jetbrains.kotlin.codegen.CallBasedArgumentGenerator.generateExpression(CallBasedArgumentGenerator.java:73)
at org.jetbrains.kotlin.codegen.ArgumentGenerator.generate(ArgumentGenerator.kt:59)
at org.jetbrains.kotlin.codegen.CallBasedArgumentGenerator.generate(CallBasedArgumentGenerator.java:60)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2847)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2820)
at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:44)
at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:23)
at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:65)
at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:111)
at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:104)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.putStackValue(ExpressionCodegen.java:338)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:323)
at org.jetbrains.kotlin.codegen.MemberCodegen.generateInitializers(MemberCodegen.java:427)
at org.jetbrains.kotlin.codegen.ScriptCodegen.genConstructor(ScriptCodegen.java:203)
at org.jetbrains.kotlin.codegen.ScriptCodegen.generateBody(ScriptCodegen.java:102)
at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:124)
at org.jetbrains.kotlin.codegen.PackageCodegen.generateFile(PackageCodegen.java:115)
at org.jetbrains.kotlin.codegen.PackageCodegen.generate(PackageCodegen.java:63)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.generatePackage(KotlinCodegenFacade.java:99)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.doGenerateFiles(KotlinCodegenFacade.java:77)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:44)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:417)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.kt:328)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.kt:532)
at org.gradle.script.lang.kotlin.support.KotlinCompilerKt.compileKotlinScriptToDirectory(KotlinCompiler.kt:67)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler$compileTo$1.invoke(CachingKotlinCompiler.kt:103)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler$compileTo$1.invoke(CachingKotlinCompiler.kt:44)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.withProgressLoggingFor(CachingKotlinCompiler.kt:149)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.compileTo(CachingKotlinCompiler.kt:101)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.access$compileTo(CachingKotlinCompiler.kt:44)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler$compileWithCache$cacheDir$2.execute(CachingKotlinCompiler.kt:86)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler$compileWithCache$cacheDir$2.execute(CachingKotlinCompiler.kt:44)
at org.gradle.cache.internal.DefaultPersistentDirectoryCache$Initializer.initialize(DefaultPersistentDirectoryCache.java:92)
at org.gradle.cache.internal.FixedSharedModeCrossProcessCacheAccess$1.run(FixedSharedModeCrossProcessCacheAccess.java:73)
at org.gradle.cache.internal.DefaultFileLockManager$DefaultFileLock.doWriteAction(DefaultFileLockManager.java:184)
at org.gradle.cache.internal.DefaultFileLockManager$DefaultFileLock.writeFile(DefaultFileLockManager.java:174)
at org.gradle.cache.internal.FixedSharedModeCrossProcessCacheAccess.open(FixedSharedModeCrossProcessCacheAccess.java:71)
at org.gradle.cache.internal.DefaultCacheAccess.open(DefaultCacheAccess.java:130)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:55)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:30)
at org.gradle.cache.internal.DefaultCacheFactory.doOpen(DefaultCacheFactory.java:89)
at org.gradle.cache.internal.DefaultCacheFactory.open(DefaultCacheFactory.java:63)
at org.gradle.cache.internal.DefaultCacheRepository$PersistentCacheBuilder.open(DefaultCacheRepository.java:116)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.compileWithCache(CachingKotlinCompiler.kt:88)
at org.gradle.script.lang.kotlin.provider.CachingKotlinCompiler.compileBuildScript(CachingKotlinCompiler.kt:70)
at org.gradle.script.lang.kotlin.provider.KotlinBuildScriptCompiler.compileScriptFile(KotlinBuildScriptCompiler.kt:137)
at org.gradle.script.lang.kotlin.provider.KotlinBuildScriptCompiler.access$compileScriptFile(KotlinBuildScriptCompiler.kt:38)
at org.gradle.script.lang.kotlin.provider.KotlinBuildScriptCompiler$onePassScript$1.invoke(KotlinBuildScriptCompiler.kt:99)
at org.gradle.script.lang.kotlin.provider.KotlinBuildScriptCompiler$onePassScript$1.invoke(KotlinBuildScriptCompiler.kt:38)
at org.gradle.script.lang.kotlin.provider.KotlinScriptPlugin.apply(KotlinScriptPlugin.kt:40)
at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:39)
at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:26)
at org.gradle.configuration.project.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34)
at org.gradle.configuration.project.LifecycleProjectEvaluator.doConfigure(LifecycleProjectEvaluator.java:70)
at org.gradle.configuration.project.LifecycleProjectEvaluator.access$000(LifecycleProjectEvaluator.java:33)
at org.gradle.configuration.project.LifecycleProjectEvaluator$1.execute(LifecycleProjectEvaluator.java:53)
at org.gradle.configuration.project.LifecycleProjectEvaluator$1.execute(LifecycleProjectEvaluator.java:50)
at org.gradle.internal.Transformers$4.transform(Transformers.java:169)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:106)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:61)
at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:50)
at org.gradle.api.internal.project.DefaultProject.evaluate(DefaultProject.java:599)
at org.gradle.api.internal.project.DefaultProject.evaluate(DefaultProject.java:125)
at org.gradle.execution.TaskPathProjectEvaluator.configure(TaskPathProjectEvaluator.java:35)
at org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:60)
at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:38)
at org.gradle.initialization.DefaultGradleLauncher$ConfigureBuildAction.execute(DefaultGradleLauncher.java:233)
at

I’m sorry for the confusion, Richard. The build script file in the gist should be saved as build.gradle (not build.gradle.kts) because it’s written in Groovy.

The Kotlin equivalent of that whole gist was presented inline instead of a gist because it was a single small script.