kotlin-debug-adapter icon indicating copy to clipboard operation
kotlin-debug-adapter copied to clipboard

Any way to support environment variables?

Open thunderz99 opened this issue 4 years ago • 4 comments

Problem

Seems there is no way to pass environment variables to the kotlin-debug-adapter. I need to pass database connection strings by environment variables in my project.

I'll be appreciated if any way provided to do this. (If any hint to implement this, I'll try to add a pullrequest.)

To reproduce

Clone the kotlin-quick-start repo. Add the following line to Main.kt.

fun main(args: Array<String>) {

        // added to test environment variables
	println("""System.getenv("MESSAGE"): ${System.getenv("MESSAGE")} """)
	println("Hello world")
}

Use ./gradlew run you can get the environment variables(MESSAGE=test)

% export MESSAGE="test" && ./gradlew run

> Task :run
System.getenv("MESSAGE"): test 
Hello world

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

Use Kotlin Launch, I cannot get the environment variables.

[INFO] main      Connected to client
[INFO] async1    Resolving dependencies for 'kotlin-quick-start' through Gradle's CLI...
[INFO] async1    Creating temporary gradle file /var/folders/51/7dvp_d0969z9v2y05l6knplc0000gn/T/classpath14115259569083174887.gradle
[INFO] async1    Successfully resolved dependencies using Gradle dependencies CLI
[INFO] async1    Starting JVM debug session with main class MainKt
System.getenv("MESSAGE"): null 
Hello world
[INFO] eventBus  Sent exit event
[INFO] async0    Exiting JDI session

I tried to add a "env" property to launch.json but there is no luck. image

Appendix

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "kotlin",
            "request": "launch",
            "name": "Kotlin Launch",
            "projectRoot": "${workspaceFolder}",
            "mainClass": "MainKt",
            "preLaunchTask": "build"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "./gradlew build -x test",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

The kotlin-quick-start repo that reproduces. https://github.com/thunderz99/kotlin-quick-start/tree/topics/test-environment-variables

thunderz99 avatar Jun 25 '20 02:06 thunderz99

The debug adapter currently does not support this. To support custom environment variables, we might have to start the debuggee process externally and connect the debug adapter to it through a socket. This is, at least, what the Java extension does: https://github.com/microsoft/java-debug/pull/89

Edit: Note that you can already launch your process externally with the following JVM args: -Xdebug -agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,quiet=y,suspend=y ...then connect the debug adapter through VSCode to port $PORT (using the "request": "attach" configuration).

fwcd avatar Jun 25 '20 13:06 fwcd

Thanks for the reply. I'll have a try on the "attach" mode.

thunderz99 avatar Jun 26 '20 01:06 thunderz99

"request": "attach" configuration

I tried the attach configuration, unfortunately it didn't work.

To reproduce


% cd kotlin-quick-start

./gradlew run --debug-jvm

> Task :run
Listening for transport dt_socket at address: 5005

Run the Kotlin Attach config in vscode. But got the message below:

[INFO] main      Connected to client
[INFO] async0    Waiting for configuration done response for 10 seconds...
[INFO] async0    Waiting for configuration done response for 20 seconds...
[INFO] async0    Waiting for configuration done response for 30 seconds...
[INFO] async0    Waiting for configuration done response for 40 seconds...
[INFO] async0    Waiting for configuration done response for 50 seconds...
[INFO] async0    Waiting for configuration done response for 60 seconds...
[INFO] async0    Waiting for configuration done response for 70 seconds...
...

My launch.json is:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "kotlin",
            "request": "attach",
            "name": "Kotlin Attach",
            "projectRoot": "${workspaceFolder}",
            "hostName": "localhost",
            "port": 5005,
            "timeout": 30000
        },
        {
            "type": "kotlin",
            "request": "launch",
            "name": "Kotlin Launch",
            "projectRoot": "${workspaceFolder}",
            "mainClass": "MainKt",
            "preLaunchTask": "build"
        },
        {
            "type": "java",
            "name": "Java Debug (Attach)",
            "request": "attach",
            "hostName": "localhost",
            "port": 5005
        }
    ]
}

The Java Debug(Attach) works. It correctly attached and showed call stack. (Of cause it doesn't recognize kotlin breakpoints though.)

A reproducing repo can be found here: https://github.com/thunderz99/kotlin-quick-start/tree/topics/test-environment-variables

thunderz99 avatar Jun 26 '20 03:06 thunderz99

It worked when I build the kotlin-debug-adapter locally. Maybe the one in the latest-release is out of date. Anyway thank you for your advice!

thunderz99 avatar Jun 26 '20 08:06 thunderz99