ygini
ygini

Reputation: 145

How to configure VSCode to debug a Gradle / Kotlin project?

Currently trying to consolidate development workflow for back and front in VSCode with devcontainer, instead of IntelliJ for back and VSCode for front, with the use of Codespace/Gitpod in mind and a goal to ease the debug of the whole stack.

I've read multiple tutorials for gradle in VSCode and ended to something working to build and run, but the debugger don't work and I don't find any tutorials taking care of that part.

The best result so far is with the following task and the Gradle extension for VSCode:

        {
            "type": "gradle",
            "id": "/workspaces/mycode/serverrunMyCode",
            "script": "run",
            "description": "Runs this project as a JVM application",
            "group": "application",
            "project": "MyCode",
            "buildFile": "/workspaces/mycode/server/build.gradle.kts",
            "rootProject": "MyCode",
            "projectFolder": "/workspaces/mycode/server",
            "workspaceFolder": "/workspaces/mycode/server",
            "args": "",
            "javaDebug": true,
            "problemMatcher": [
                "$gradle"
            ],
            "label": "Server Debug"
        }

The server correctly build and run via VSCode but the debugger does not work.

I've tried with the Kotlin extensions and the following launch settings but here I get a missing dependencies issue

    "configurations": [
        {
            "type": "kotlin",
            "request": "launch",
            "name": "Kotlin Launch",
            "projectRoot": "${workspaceFolder}",
            "mainClass": "io.mycode.ServerKt"
        }
    ]

The dependency error:

[INFO] main      Connected to client
[INFO] async1    Resolving dependencies for 'server' through Gradle's CLI using tasks [kotlinLSPProjectDeps]...
[WARN] async1    Could not resolve classpath using Gradle: ClassLoader.getSystemResourceAsStream(scriptName) must not be null
[INFO] async1    Could not resolve kotlin-stdlib using Maven: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[INFO] async1    Successfully resolved kotlin-stdlib using Gradle
[INFO] async1    Starting JVM debug session with main class io.mycode.ServerKt
Error: Unable to initialize main class io.mycode.ServerKt
Caused by: java.lang.NoClassDefFoundError: mu/KLogger
[INFO] eventBus  Sent exit event
[INFO] async0    Exiting JDI session

So my question is simple: what is the working config to have VSCode working with a gradle/kotlin project? Debug included.

Thanks

Upvotes: 7

Views: 4147

Answers (1)

Ginger
Ginger

Reputation: 303

I had a very similar problem, and resolved it by compiling the Kotlin debug adapter from source and configuring the Kotlin VSCode extension to use the compiled version. You can find the source here.

Upvotes: 3

Related Questions