kotlin-language-server: The Kotlin Language Server server crashed 5 times in the last 3 minutes. The server will not be restarted.

I have just installed this extension in VS Code Insiders 1.26.0 on a Windows 10 x64 machine. However, when opening Kotlin projects, I get the message I put in the title. The Kotlin output window only shows this:

[Info  - 9:52:56 AM] Connection to server got closed. Server will restart.
[Info  - 9:52:56 AM] Connection to server got closed. Server will restart.
[Info  - 9:52:56 AM] Connection to server got closed. Server will restart.
[Info  - 9:52:56 AM] Connection to server got closed. Server will restart.
[Error - 9:52:56 AM] Connection to server got closed. Server will not be restarted.

And when looking in DevTools, I see this:

ERR Message header must separate key and value using :: Error: Message header must separate key and value using :
    at C:\Users\Noah\.vscode-insiders\extensions\fwcd.kotlin-0.1.7\node_modules\vscode-jsonrpc\lib\messageReader.js:68:23
    at Array.forEach (<anonymous>)
    at MessageBuffer.tryReadHeaders (C:\Users\Noah\.vscode-insiders\extensions\fwcd.kotlin-0.1.7\node_modules\vscode-jsonrpc\lib\messageReader.js:65:17)
    at StreamMessageReader.onData (C:\Users\Noah\.vscode-insiders\extensions\fwcd.kotlin-0.1.7\node_modules\vscode-jsonrpc\lib\messageReader.js:194:43)
    at Socket.<anonymous> (C:\Users\Noah\.vscode-insiders\extensions\fwcd.kotlin-0.1.7\node_modules\vscode-jsonrpc\lib\messageReader.js:185:19)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at Pipe.onread (net.js:594:20)

That message is repeated 4 or 5 times in fast succession. Any solutions to this?

C:\Users\Noah\Desktop\kotlin> java -version
  java version "1.8.0_151"
  Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
  Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

C:\Users\Noah\Desktop\kotlin> kotlin -version
  Kotlin version 1.2.60-eap-75 (JRE 1.8.0_151-b12)

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 5
  • Comments: 19 (3 by maintainers)

Most upvoted comments

I experienced this issue on VS Code with WSL2. I have installed the extension within WSL2 and tried to use it on Windows 10 2004. What I did (in part based on @Polarbear08’s solution):

  1. Explicitly install openjdk-14-jdk-headless on WSL2.
  2. Change 2 configurations below Kotlin > Language Server: Port: 5050 Kotlin > Language Server: Transport: tcp

  3. Install Kotlin compiler.
curl -sSLOf https://github.com/JetBrains/kotlin/releases/download/v1.3.72/kotlin-native-linux-1.3.72.tar.gz
tar zxf kotlin-native-linux-1.3.72.tar.gz
sudo mv kotlin-native-linux-1.3.72 /usr/bin/kotlin-native-linux-1.3.72
export PATH="$PATH:/usr/bin/kotlin-native-linux-1.3.72/bin" # Optionally, add to .bashrc or something.
which kotlinc # Check if it works.
  1. Restart VS Code.

Now the language server starts up properly.

WSL2’s OS: Debian Linux bullseye

I have the same issue. My project is a huge android project with hundreds of modules and it has at least 15000 Kotlin files.

After tracing the source codes, I found the Kotlin Language Server failed on the following method:

/** Searches the folder for all build-files. */
private fun folderResolvers(workspaceRoot: Path, folder: Path, ignored: List<PathMatcher>): Collection<ClassPathResolver> {
    var resolvers = mutableListOf<ClassPathResolver>()

    for (file in Files.list(folder)) {
        // Only test whether non-ignored file is a build-file
        if (ignored.none { it.matches(workspaceRoot.relativize(file)) }) {
            val resolver = asClassPathProvider(file)
            if (resolver != null) {
                resolvers.add(resolver)
                break
            } else if (Files.isDirectory(file)) {
                resolvers.addAll(folderResolvers(workspaceRoot, file, ignored))
            }
        }
    }

    return resolvers
}

https://github.com/fwcd/kotlin-language-server/blob/master/shared/src/main/kotlin/org/javacs/kt/classpath/DefaultClassPathResolver.kt#L22

This method is looking for all the maven / gradle / shell modules from the project root to its subfolder recursively. For example, it searches all of “.build.gradle" or ".build.gradle.kts” to identify it is gradle module or not. The reason to do that is it wants to resolve the dependencies on each maven / gradle / shell module. But this search takes too much time, especially for the huge project. Although it ignores not necessary folders from the project root’s “.gitignore”, it still causes the Kotlin Language Server failed. My android project has a flat structure and it only has two layers of structure. So it doesn’t need to search so deeply. I modify this method to only run two levels of recursion and this modification works for my project. But this solution only works for your projects has two layers of the structure. I have not idea how to traverse the project trees efficiently to resolve all of the dependencies.

We have a mix of node/Kotlin code in our monorepo (and we’re using yarn workspaces so the root node_modules tends to contain pretty much everything), but the thing that’s weird is it works on some machines and not others. Without better error diagnostics it’s really hard to know where to continue to look

I’m seeing the same error on OSX, what’s strange is as far as I can tell my environment is the same as a coworker’s and his is running with no issues.

The Kotlin diagnostic output doesn’t show much, I just get the following 5 times and then it quits:

Info  - 10:15:09 AM] main      Connected to client
[Info  - 10:15:09 AM] client    Adding workspace file:///Users/matt/code/pg/mono to source path
[Info  - 10:15:10 AM] client    Adding 151 files under /Users/matt/code/pg/mono to source path
[Info  - 10:15:11 AM] client    Searching for dependencies in workspace root /Users/matt/code/pg/mono
[Info  - 10:15:16 AM] Connection to server got closed. Server will restart.

Is there a way to get additional debugging info regarding why the language server is shutting down?

The error actually occurred because my JAVA_HOME was set but to the wrong location. Probably a lot more fringe than I initially thought so it’s probably not the same issues others are having.

Had the same error. I went and changed the source code on messageReader.js to console.log the header since it seemed to be causing the problem. It returned an array containing this:

"ERROR: JAVA_HOME is set to an invalid directory: "

Whoops. Setting the java home environment variable fixed the problem. Maybe it’s possible to throw a better error message? I think the issue can be closed unless the original person comes back and says they’re still having issues.

Tested on Linux and there was no issue so this seems to be a windows only issue. Also hi another Noah!