CtrlK
BlogDocsLog inGet started
Tessl Logo

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.

Pending
Overview
Eval results
Files

daemon-connection.mddocs/

Daemon Connection Management

Core functionality for finding, connecting to, and managing Kotlin compiler daemon processes. This includes daemon discovery, startup, connection lifecycle management, and session handling.

Capabilities

Connect to Compile Service

Connects to an existing Kotlin compiler daemon or starts a new one if needed.

/**
 * Connect to a Kotlin compiler daemon service
 * @param compilerId Identifies the compiler version and classpath
 * @param daemonJVMOptions JVM configuration for the daemon
 * @param daemonOptions Daemon-specific configuration options
 * @param reportingTargets Where to send diagnostic and status messages
 * @param autostart Whether to start a new daemon if none found
 * @param checkId Whether to verify compiler ID matches
 * @return CompileService instance or null if connection failed
 */
fun connectToCompileService(
    compilerId: CompilerId,
    daemonJVMOptions: DaemonJVMOptions,
    daemonOptions: DaemonOptions,
    reportingTargets: DaemonReportingTargets,
    autostart: Boolean = true,
    checkId: Boolean = true
): CompileService?

Usage Example:

import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.common.*

val compilerId = CompilerId().apply {
    compilerClasspath = listOf("/path/to/kotlin-compiler.jar")
}

val daemonJVMOptions = DaemonJVMOptions().apply {
    maxMemory = "2g"
    maxPermSize = "512m"
}

val daemonOptions = DaemonOptions().apply {
    runFilesPath = "/tmp/kotlin-daemon"
}

val reportingTargets = DaemonReportingTargets(out = System.out)

val compileService = KotlinCompilerClient.connectToCompileService(
    compilerId = compilerId,
    daemonJVMOptions = daemonJVMOptions,
    daemonOptions = daemonOptions,
    reportingTargets = reportingTargets,
    autostart = true
)

Connect with Client Flag File

Connects to daemon using an explicit client alive flag file for process lifecycle management.

/**
 * Connect to daemon with explicit client flag file
 * @param compilerId Compiler identification
 * @param clientAliveFlagFile File indicating client is alive
 * @param daemonJVMOptions JVM options for daemon
 * @param daemonOptions Daemon configuration
 * @param reportingTargets Diagnostic reporting targets
 * @param autostart Whether to start daemon if not found
 * @return CompileService instance or null
 */
fun connectToCompileService(
    compilerId: CompilerId,
    clientAliveFlagFile: File,
    daemonJVMOptions: DaemonJVMOptions,
    daemonOptions: DaemonOptions,
    reportingTargets: DaemonReportingTargets,
    autostart: Boolean = true
): CompileService?

Connect and Lease Session

Connects to daemon and optionally leases a compilation session for exclusive use.

/**
 * Connect to daemon and lease a compilation session
 * @param compilerId Compiler identification
 * @param clientAliveFlagFile Client lifecycle flag file
 * @param daemonJVMOptions JVM configuration
 * @param daemonOptions Daemon configuration
 * @param reportingTargets Diagnostic targets
 * @param autostart Whether to start daemon automatically
 * @param leaseSession Whether to lease an exclusive session
 * @param sessionAliveFlagFile Session lifecycle flag file
 * @return CompileServiceSession with service and session ID
 */
fun connectAndLease(
    compilerId: CompilerId,
    clientAliveFlagFile: File,
    daemonJVMOptions: DaemonJVMOptions,
    daemonOptions: DaemonOptions,
    reportingTargets: DaemonReportingTargets,
    autostart: Boolean,
    leaseSession: Boolean,
    sessionAliveFlagFile: File? = null
): CompileServiceSession?

Daemon Shutdown

Shuts down a running Kotlin compiler daemon.

/**
 * Shutdown a compiler daemon
 * @param compilerId Identifies which daemon to shut down
 * @param daemonOptions Daemon configuration for finding the daemon
 */
fun shutdownCompileService(compilerId: CompilerId, daemonOptions: DaemonOptions): Unit

/**
 * Shutdown daemon with default options
 * @param compilerId Identifies which daemon to shut down
 */
fun shutdownCompileService(compilerId: CompilerId): Unit

Session Management

Lease and release compilation sessions for exclusive daemon access.

/**
 * Lease a compilation session from the daemon
 * @param compilerService The daemon service
 * @param aliveFlagPath Path to session alive flag file
 * @return Session ID for the leased session
 */
fun leaseCompileSession(compilerService: CompileService, aliveFlagPath: String?): Int

/**
 * Release a previously leased compilation session
 * @param compilerService The daemon service
 * @param sessionId Session ID to release
 */
fun releaseCompileSession(compilerService: CompileService, sessionId: Int): Unit

Client Flag File Management

Creates and manages client alive flag files for daemon communication.

/**
 * Get or create a client flag file for daemon communication
 * @param daemonOptions Daemon configuration containing flag file path
 * @return File representing the client alive flag
 */
fun getOrCreateClientFlagFile(daemonOptions: DaemonOptions): File

Compiler Classpath Detection

Automatically detects the Kotlin compiler classpath from the current environment.

/**
 * Detect compiler classpath automatically
 * @return List of classpath entries or null if detection failed
 */
fun detectCompilerClasspath(): List<String>?

Types

CompileServiceSession

/**
 * Represents a daemon service with an associated session
 * @param compileService The daemon service instance
 * @param sessionId The session ID (CompileService.NO_SESSION for no session)
 */
data class CompileServiceSession(
    val compileService: CompileService,
    val sessionId: Int
)

Client Options

/**
 * Configuration options for the daemon client
 * @param stop Whether to stop/shutdown mode
 */
data class ClientOptions(
    var stop: Boolean = false
) : OptionsGroup

Usage Patterns

Basic Connection

val compileService = KotlinCompilerClient.connectToCompileService(
    compilerId, daemonJVMOptions, daemonOptions, reportingTargets
)

compileService?.let { service ->
    // Use the service for compilation
    // ...
    
    // Optionally shutdown when done
    KotlinCompilerClient.shutdownCompileService(compilerId, daemonOptions)
}

Session-Based Usage

val session = KotlinCompilerClient.connectAndLease(
    compilerId, clientFlagFile, daemonJVMOptions, daemonOptions,
    reportingTargets, autostart = true, leaseSession = true, sessionFlagFile
)

session?.let { (service, sessionId) ->
    try {
        // Use service with sessionId for exclusive access
        // ...
    } finally {
        KotlinCompilerClient.releaseCompileSession(service, sessionId)
    }
}

Auto-Detection Setup

val compilerId = CompilerId().apply {
    val detectedClasspath = KotlinCompilerClient.detectCompilerClasspath()
    if (detectedClasspath != null) {
        compilerClasspath = detectedClasspath
    } else {
        throw IllegalStateException("Could not detect Kotlin compiler classpath")
    }
}

Install with Tessl CLI

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

docs

compilation-services.md

daemon-connection.md

index.md

repl-client.md

service-facades.md

stream-servers.md

tile.json