Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to support environment variables? #39

Open
thunderz99 opened this issue Jun 25, 2020 · 4 comments
Open

Any way to support environment variables? #39

thunderz99 opened this issue Jun 25, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@thunderz99
Copy link
Contributor

thunderz99 commented Jun 25, 2020

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

@fwcd fwcd added the enhancement New feature or request label Jun 25, 2020
@fwcd
Copy link
Owner

fwcd commented Jun 25, 2020

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: microsoft/java-debug#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).

@thunderz99
Copy link
Contributor Author

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

@thunderz99
Copy link
Contributor Author

thunderz99 commented Jun 26, 2020

"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
Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants