Skip to content

support cwd, env and envFile parameters using sun jdi #90

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename 'envs' to 'env' to align with other debug adapters
  • Loading branch information
NecroKote committed Aug 7, 2024
commit 4ed5648ac21de27a0be2984170757b7fd131c175
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class KotlinDebugAdapter(

// Cast from com.google.gson.internal.LinkedTreeMap
@Suppress("UNCHECKED_CAST")
var envs = args["envs"] as? Map<String, String> ?: mapOf()
val env = args["env"] as? Map<String, String> ?: mapOf()

setupCommonInitializationParams(args)

Expand All @@ -114,7 +114,7 @@ class KotlinDebugAdapter(
projectRoot,
vmArguments,
cwd,
envs
env
)
debuggee = launcher.launch(
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class LaunchConfiguration(
val projectRoot: Path,
val vmArguments: String = "",
val cwd: Path = projectRoot,
val envs: Map<String, String> = mapOf()
val env: Map<String, String> = mapOf()
)
21 changes: 11 additions & 10 deletions adapter/src/main/kotlin/org/javacs/ktda/jdi/launch/JDILauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JDILauncher(

override fun launch(config: LaunchConfiguration, context: DebugContext): JDIDebuggee {
val connector = createLaunchConnector()
LOG.info("Starting JVM debug session with main class {}", config.mainClass)
LOG.info("Starting JVM debug session with main class {} and connector {}", config.mainClass, connector)

LOG.debug("Launching VM")
val vm = connector.launch(createLaunchArgs(config, connector)) ?: throw KotlinDAException("Could not launch a new VM")
Expand All @@ -50,11 +50,11 @@ class JDILauncher(

private fun createLaunchArgs(config: LaunchConfiguration, connector: Connector): Map<String, Connector.Argument> = connector.defaultArguments()
.also { args ->
args["suspend"]!!.setValue("true")
args["options"]!!.setValue(formatOptions(config))
args["main"]!!.setValue(formatMainClass(config))
args["cwd"]!!.setValue(config.cwd.toAbsolutePath().toString())
args["envs"]!!.setValue(KDACommandLineLauncher.urlEncode(config.envs.map { "${it.key}=${it.value}" }) ?: "")
args.get("suspend")?.setValue("true")
args.get("options")?.setValue(formatOptions(config))
args.get("main")?.setValue(formatMainClass(config))
args.get("cwd")?.setValue(config.cwd.toAbsolutePath().toString())
args.get("env")?.setValue(KDACommandLineLauncher.urlEncode(config.env.map { "${it.key}=${it.value}" }) ?: "")
}

private fun createAttachArgs(config: AttachConfiguration, connector: Connector): Map<String, Connector.Argument> = connector.defaultArguments()
Expand All @@ -65,12 +65,13 @@ class JDILauncher(
}

private fun createAttachConnector(): AttachingConnector = vmManager.attachingConnectors()
.let { it.find { it.name() == "com.sun.jdi.SocketAttach" } ?: it.firstOrNull() }
.let { it.find { it.name() == "com.sun.jdi.SocketAttach" } }
?: throw KotlinDAException("Could not find an attaching connector (for a new debuggee VM)")

private fun createLaunchConnector(): LaunchingConnector = vmManager.launchingConnectors().also { LOG.debug("connectors: $it") }

private fun createLaunchConnector(): LaunchingConnector = vmManager.launchingConnectors()
.also { LOG.debug("connectors: $it") }
// Using our own connector to support cwd and envs
.let { it.find { it.name() == KDACommandLineLauncher::class.java.name } ?: it.firstOrNull() }
.let { it.find { it.name() == KDACommandLineLauncher::class.java.name } }
?: throw KotlinDAException("Could not find a launching connector (for a new debuggee VM)")

private fun sourcesRootsOf(projectRoot: Path): Set<Path> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal const val ARG_SUSPEND = "suspend"
internal const val ARG_QUOTE = "quote"
internal const val ARG_VM_EXEC = "vmexec"
internal const val ARG_CWD = "cwd"
internal const val ARG_ENVS = "envs"
internal const val ARG_ENV = "env"

/**
* A custom LaunchingConnector that supports cwd and env variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract class DebugAdapterTestFixture(
"mainClass" to mainClass,
"vmArguments" to vmArguments,
"cwd" to cwd,
"envs" to envs
"env" to envs
)).join()
println("Launched")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class KDACommandLineLauncherTest {
assertThat(args.size, greaterThanOrEqualTo(2))

assertThat(args["cwd"], notNullValue())
assertThat(args["envs"], notNullValue())
assertThat(args["env"], notNullValue())
//suspend should default to true
assertThat(args["suspend"]?.value(), equalTo("true"))

Expand Down