or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

compilation-services.mddaemon-connection.mdindex.mdrepl-client.mdservice-facades.mdstream-servers.md
tile.json

tessl/maven-org-jetbrains-kotlin--kotlin-daemon-client

Client library for communicating with the Kotlin compilation daemon, enabling remote compilation services and incremental compilation workflows.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.jetbrains.kotlin/kotlin-daemon-client@2.2.x

To install, run

npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-daemon-client@2.2.0

index.mddocs/

Kotlin Daemon Client

The Kotlin Daemon Client is a comprehensive client library for communicating with the Kotlin compilation daemon, enabling remote compilation services and incremental compilation workflows. It provides APIs for establishing compilation sessions, managing compiler services, and handling communication between build tools (like Gradle or Maven) and the Kotlin compiler daemon process.

Package Information

  • Package Name: kotlin-daemon-client
  • Package Type: maven
  • Language: Kotlin
  • Group ID: org.jetbrains.kotlin
  • Artifact ID: kotlin-daemon-client
  • Installation:
    implementation("org.jetbrains.kotlin:kotlin-daemon-client:2.2.0")
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-daemon-client</artifactId>
        <version>2.2.0</version>
    </dependency>

Core Imports

import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.client.CompilationServices
import org.jetbrains.kotlin.daemon.client.CompileServiceSession
import org.jetbrains.kotlin.daemon.client.DaemonReportingTargets

Basic Usage

import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.common.*
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
import java.io.File

// Set up daemon connection parameters
val compilerId = CompilerId()
val daemonJVMOptions = DaemonJVMOptions()
val daemonOptions = DaemonOptions()
val reportingTargets = DaemonReportingTargets(out = System.out)

// Connect to the daemon
val compileService = KotlinCompilerClient.connectToCompileService(
    compilerId = compilerId,
    daemonJVMOptions = daemonJVMOptions,
    daemonOptions = daemonOptions,
    reportingTargets = reportingTargets,
    autostart = true
)

if (compileService != null) {
    // Compile with the daemon
    val messageCollector = PrintingMessageCollector(System.out, null, false)
    val compilationResult = KotlinCompilerClient.compile(
        compilerService = compileService,
        sessionId = CompileService.NO_SESSION,
        targetPlatform = CompileService.TargetPlatform.JVM,
        args = arrayOf("MyClass.kt"),
        messageCollector = messageCollector
    )
    
    println("Compilation result: $compilationResult")
}

Architecture

The Kotlin Daemon Client is built around several key components:

  • KotlinCompilerClient: Main singleton object providing daemon connection and compilation services
  • Service Facades: Server implementations for handling compiler callbacks and services
  • REPL Support: Complete REPL (Read-Eval-Print Loop) client functionality for interactive compilation
  • Session Management: Lease/release mechanisms for managing compilation sessions
  • Stream Servers: Remote input/output stream handling for daemon communication
  • Reporting System: Comprehensive error reporting and diagnostic collection

Capabilities

Daemon Connection Management

Core functionality for finding, connecting to, and managing Kotlin compiler daemon processes. Handles daemon discovery, startup, and connection lifecycle.

object KotlinCompilerClient {
    fun connectToCompileService(
        compilerId: CompilerId,
        daemonJVMOptions: DaemonJVMOptions,
        daemonOptions: DaemonOptions,
        reportingTargets: DaemonReportingTargets,
        autostart: Boolean = true,
        checkId: Boolean = true
    ): CompileService?
    
    fun connectAndLease(
        compilerId: CompilerId,
        clientAliveFlagFile: File,
        daemonJVMOptions: DaemonJVMOptions,
        daemonOptions: DaemonOptions,
        reportingTargets: DaemonReportingTargets,
        autostart: Boolean,
        leaseSession: Boolean,
        sessionAliveFlagFile: File? = null
    ): CompileServiceSession?
}

Daemon Connection Management

Compilation Services

Compilation execution through the daemon with support for different target platforms, compiler modes, and incremental compilation workflows.

fun compile(
    compilerService: CompileService,
    sessionId: Int,
    targetPlatform: CompileService.TargetPlatform,
    args: Array<out String>,
    messageCollector: MessageCollector,
    outputsCollector: ((File, List<File>) -> Unit)? = null,
    compilerMode: CompilerMode = CompilerMode.NON_INCREMENTAL_COMPILER,
    reportSeverity: ReportSeverity = ReportSeverity.INFO,
    port: Int = SOCKET_ANY_FREE_PORT,
    profiler: Profiler = DummyProfiler()
): Int

data class CompilationServices(
    val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
    val lookupTracker: LookupTracker? = null,
    val compilationCanceledStatus: CompilationCanceledStatus? = null
)

Compilation Services

REPL Client

Interactive Kotlin compilation and evaluation through the daemon with state management and history tracking.

class KotlinRemoteReplCompilerClient(
    protected val compileService: CompileService,
    clientAliveFlagFile: File?,
    targetPlatform: CompileService.TargetPlatform,
    args: Array<out String>,
    messageCollector: MessageCollector,
    templateClasspath: List<File>,
    templateClassName: String,
    port: Int = SOCKET_ANY_FREE_PORT
) : ReplCompiler {
    fun dispose()
    override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*>
    override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult
    override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult
}

REPL Client

Service Facades

Server implementations for handling compiler callbacks, incremental compilation services, and communication with the daemon process.

class BasicCompilerServicesWithResultsFacadeServer(
    val messageCollector: MessageCollector,
    val outputsCollector: ((File, List<File>) -> Unit)? = null,
    port: Int = SOCKET_ANY_FREE_PORT
) : CompilerServicesFacadeBase

class CompilerCallbackServicesFacadeServer(
    val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
    val lookupTracker: LookupTracker? = null,
    val compilationCanceledStatus: CompilationCanceledStatus? = null,
    // ... additional parameters
    port: Int = SOCKET_ANY_FREE_PORT
) : CompilerCallbackServicesFacade

Service Facades

Stream Servers

Remote input/output stream handling for daemon communication, enabling data transfer between client and daemon processes.

class RemoteInputStreamServer(
    val `in`: InputStream,
    port: Int = SOCKET_ANY_FREE_PORT
) : RemoteInputStream

class RemoteOutputStreamServer(
    val out: OutputStream,
    port: Int = SOCKET_ANY_FREE_PORT
) : RemoteOutputStream

Stream Servers

Types

Core Data Classes

data class CompileServiceSession(
    val compileService: CompileService,
    val sessionId: Int
)

data class DaemonReportMessage(
    val category: DaemonReportCategory,
    val message: String
)

class DaemonReportingTargets(
    val out: PrintStream? = null,
    val messages: MutableCollection<DaemonReportMessage>? = null,
    val messageCollector: MessageCollector? = null,
    val compilerServices: CompilerServicesFacadeBase? = null
)