Skip to content

Commit

Permalink
feat: fwcd#39 support cwd anv env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderz99 committed Jul 1, 2020
1 parent c987ae2 commit 564c226
Show file tree
Hide file tree
Showing 15 changed files with 614 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
"java.configuration.updateBuildConfiguration": "disabled"
}
13 changes: 13 additions & 0 deletions adapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ startScripts {
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
maven { url 'https://repo.eclipse.org/content/groups/releases/' }

}

dependencies {
Expand All @@ -29,6 +31,17 @@ dependencies {
implementation 'com.github.fwcd.kotlin-language-server:shared:229c762a4d75304d21eba6d8e1231ed949247629'
// The Java Debug Interface classes (com.sun.jdi.*)
implementation files("${System.properties['java.home']}/../lib/tools.jar")
// The Implementation of jdi by eclipse
// implementation group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.debug', version: '3.15.100'
// Failed to use the one above because jdi.jar and jdimodel.jar are sub jars included in org.eclipse.jdt.debug-3.15.100.jar,
// and cannot be recognized correctly.
// TODO: a fix for this
implementation files('lib/org.eclipse.jdt.debug-3.15.100.jdi.jar')
implementation files('lib/org.eclipse.jdt.debug-3.15.100.jdimodel.jar')
// For org.eclipse.osgi.util.NLS used in eclipse.jdt.debug
implementation group: 'org.eclipse.platform', name: 'org.eclipse.osgi', version: '3.15.200'
// For com.ibm.icu.text.DateFormat used in eclipse.jdt.debug
implementation group: 'at.bestsolution.eclipse', name: 'com.ibm.icu.base', version: '54.1.1'
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest-all:1.3'
}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,19 @@ class KotlinDebugAdapter(

val vmArguments = (args["vmArguments"] as? String) ?: ""

var cwd = (args["cwd"] as? String).let { if(it.isNullOrBlank()) projectRoot else Paths.get(it) }

var envs = args["envs"] as? List<String>

setupCommonInitializationParams(args)

val config = LaunchConfiguration(
debugClassPathResolver(listOf(projectRoot)).classpathOrEmpty,
mainClass,
projectRoot,
vmArguments
vmArguments,
cwd,
envs
)
debuggee = launcher.launch(
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ class LaunchConfiguration(
val classpath: Set<Path>,
val mainClass: String,
val projectRoot: Path,
val vmArguments: String = ""
val vmArguments: String = "",
val cwd: Path = projectRoot,
val envs: Collection<String>? = null
)
25 changes: 7 additions & 18 deletions adapter/src/main/kotlin/org/javacs/ktda/jdi/launch/JDILauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,25 @@ import org.javacs.kt.LOG
import org.javacs.ktda.core.launch.DebugLauncher
import org.javacs.ktda.core.launch.LaunchConfiguration
import org.javacs.ktda.core.launch.AttachConfiguration
import org.javacs.ktda.core.Debuggee
import org.javacs.ktda.core.DebugContext
import org.javacs.ktda.util.KotlinDAException
import org.javacs.ktda.jdi.JDIDebuggee
import com.sun.jdi.Bootstrap
import com.sun.jdi.VirtualMachineManager
import com.sun.jdi.connect.Connector
import com.sun.jdi.connect.LaunchingConnector
import com.sun.jdi.connect.AttachingConnector
import java.io.File
import java.nio.file.Path
import java.nio.file.Files
import java.net.URLEncoder
import java.net.URLDecoder
import java.nio.charset.StandardCharsets
import java.util.stream.Collectors

class JDILauncher(
private val attachTimeout: Int = 50,
private val vmArguments: String? = null,
private val modulePaths: String? = null
) : DebugLauncher {
private val vmManager: VirtualMachineManager
get() = Bootstrap.virtualMachineManager()
//using our own manager to use KDALaunchingConnector
get() = KDAVirtualMachineManager.manager()

override fun launch(config: LaunchConfiguration, context: DebugContext): JDIDebuggee {
val connector = createLaunchConnector()
Expand Down Expand Up @@ -57,6 +52,8 @@ class JDILauncher(
args["suspend"]!!.setValue("true")
args["options"]!!.setValue(formatOptions(config))
args["main"]!!.setValue(formatMainClass(config))
args["cwd"]!!.setValue(config.cwd.toAbsolutePath().toString())
args["envs"]!!.setValue(KDALaunchingConnector.urlEncode(config.envs) ?: "")
}

private fun createAttachArgs(config: AttachConfiguration, connector: Connector): Map<String, Connector.Argument> = connector.defaultArguments()
Expand All @@ -71,8 +68,8 @@ class JDILauncher(
?: throw KotlinDAException("Could not find an attaching connector (for a new debuggee VM)")

private fun createLaunchConnector(): LaunchingConnector = vmManager.launchingConnectors()
// Workaround for JDK 11+ where the first launcher (RawCommandLineLauncher) does not properly support args
.let { it.find { it.javaClass.name == "com.sun.tools.jdi.SunCommandLineLauncher" } ?: it.firstOrNull() }
// Using our own connector to support cwd and envs
.let { it.find { it.name().equals(KDALaunchingConnector::class.java.name) } ?: it.firstOrNull() }
?: throw KotlinDAException("Could not find a launching connector (for a new debuggee VM)")

private fun sourcesRootsOf(projectRoot: Path): Set<Path> = projectRoot.resolve("src")
Expand Down Expand Up @@ -100,13 +97,5 @@ class JDILauncher(
private fun formatClasspath(config: LaunchConfiguration): String = config.classpath
.map { it.toAbsolutePath().toString() }
.reduce { prev, next -> "$prev${File.pathSeparatorChar}$next" }

private fun urlEncode(arg: Collection<String>?) = arg
?.map { URLEncoder.encode(it, StandardCharsets.UTF_8.name()) }
?.reduce { a, b -> "$a\n$b" }

private fun urlDecode(arg: String?) = arg
?.split("\n")
?.map { URLDecoder.decode(it, StandardCharsets.UTF_8.name()) }
?.toList()

}
Loading

0 comments on commit 564c226

Please sign in to comment.