Skip to content

feat: support cwd and env variable using sun jdi #45

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 3 commits into
base: main
Choose a base branch
from

Conversation

thunderz99
Copy link
Contributor

@thunderz99 thunderz99 commented Jul 2, 2020

An enhance for #39

Done

  • cwd

    • Add "cwd" for LaunchConfiguration
      • default to ${projectRoot} if null or blank
    • Add a custom KDACommandLineLauncher which implements LaunchingConnector
      • The implementation is in original code written in Kotlin
    • Add unit test for cwd
  • env variables

    • Add "envs" for LaunchConfiguration
    • Change "envs" config from List<String> to Map<String, String> style for more friendly user experience
    • Add unit test for envs
  • support for jdk1.8

  • support for jdk11

    • SunCommandLineLauncher cannot be extended under jdk11, so KDACommandLineLauncher implements
      LaunchingConnector directly
    • SocketTransportService cannot be constructed under jdk11, so it is newed by Class.forName

Linked pullreq

@thunderz99 thunderz99 marked this pull request as draft July 2, 2020 07:44
@thunderz99 thunderz99 force-pushed the feat/support-cwd-and-env-variable-using-sun-jdi branch from 34a9e2e to 15203b1 Compare July 2, 2020 07:57
@thunderz99 thunderz99 marked this pull request as ready for review July 3, 2020 05:04
@thunderz99 thunderz99 changed the title [WIP]feat: support cwd and env variable using sun jdi feat: support cwd and env variable using sun jdi Jul 3, 2020
@bkleiner
Copy link

any update on this? what needs to be done to get this merged?

Copy link
Owner

@fwcd fwcd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long delay here, thanks for looking into this! I have left a few comments (mostly concerning style) below, so we can get this moving.

@@ -29,6 +29,8 @@ 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")
// For CommandLineUtils.translateCommandLine
implementation 'org.codehaus.plexus:plexus-utils:3.3.0'
Copy link
Owner

@fwcd fwcd Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, I would like to avoid introducing a new dependency here. For splitting the command line args, I think just splitting by spaces would be fine for now.

/**
* A custom LaunchingConnector that supports cwd and env variables
*/
open class KDACommandLineLauncher : LaunchingConnector {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason to make this open, e.g. do we need to do so for interop with Java/JDI? If not, I would suggest removing the modifier.

Comment on lines +59 to +73
defaultArguments[ARG_HOME] = StringArgument(ARG_HOME, description = "Java home", value = System.getProperty("java.home"))

defaultArguments[ARG_OPTIONS] = StringArgument(ARG_OPTIONS, description = "Jvm arguments")

defaultArguments[ARG_MAIN] = StringArgument(ARG_MAIN, description = "Main class name and parameters", mustSpecify = true)

defaultArguments[ARG_SUSPEND] = StringArgument(ARG_SUSPEND, description = "Whether launch the debugee in suspend mode", value = "true")

defaultArguments[ARG_QUOTE] = StringArgument(ARG_QUOTE, description = "Quote char", value = "\"")

defaultArguments[ARG_VM_EXEC] = StringArgument(ARG_VM_EXEC, description = "The java exec", value = "java")

defaultArguments[ARG_CWD] = StringArgument(ARG_CWD, description = "Current working directory")

defaultArguments[ARG_ENVS] = StringArgument(ARG_ENVS, description = "Environment variables")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: Please remove the blank lines inbetween.

Suggested change
defaultArguments[ARG_HOME] = StringArgument(ARG_HOME, description = "Java home", value = System.getProperty("java.home"))
defaultArguments[ARG_OPTIONS] = StringArgument(ARG_OPTIONS, description = "Jvm arguments")
defaultArguments[ARG_MAIN] = StringArgument(ARG_MAIN, description = "Main class name and parameters", mustSpecify = true)
defaultArguments[ARG_SUSPEND] = StringArgument(ARG_SUSPEND, description = "Whether launch the debugee in suspend mode", value = "true")
defaultArguments[ARG_QUOTE] = StringArgument(ARG_QUOTE, description = "Quote char", value = "\"")
defaultArguments[ARG_VM_EXEC] = StringArgument(ARG_VM_EXEC, description = "The java exec", value = "java")
defaultArguments[ARG_CWD] = StringArgument(ARG_CWD, description = "Current working directory")
defaultArguments[ARG_ENVS] = StringArgument(ARG_ENVS, description = "Environment variables")
defaultArguments[ARG_HOME] = StringArgument(ARG_HOME, description = "Java home", value = System.getProperty("java.home"))
defaultArguments[ARG_OPTIONS] = StringArgument(ARG_OPTIONS, description = "Jvm arguments")
defaultArguments[ARG_MAIN] = StringArgument(ARG_MAIN, description = "Main class name and parameters", mustSpecify = true)
defaultArguments[ARG_SUSPEND] = StringArgument(ARG_SUSPEND, description = "Whether launch the debugee in suspend mode", value = "true")
defaultArguments[ARG_QUOTE] = StringArgument(ARG_QUOTE, description = "Quote char", value = "\"")
defaultArguments[ARG_VM_EXEC] = StringArgument(ARG_VM_EXEC, description = "The java exec", value = "java")
defaultArguments[ARG_CWD] = StringArgument(ARG_CWD, description = "Current working directory")
defaultArguments[ARG_ENVS] = StringArgument(ARG_ENVS, description = "Environment variables")

Comment on lines +135 to +142
command.append(wrapWhitespace(exe))

command.append(" $options")

//debug options
command.append(" -agentlib:jdwp=transport=${transport.name()},address=$address,server=n,suspend=${if (suspend) 'y' else 'n'}")

command.append(" $main")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I think it looks a bit nicer with less whitespace.

Suggested change
command.append(wrapWhitespace(exe))
command.append(" $options")
//debug options
command.append(" -agentlib:jdwp=transport=${transport.name()},address=$address,server=n,suspend=${if (suspend) 'y' else 'n'}")
command.append(" $main")
command.append(wrapWhitespace(exe))
command.append(" $options")
// debug options
command.append(" -agentlib:jdwp=transport=${transport.name()},address=$address,server=n,suspend=${if (suspend) 'y' else 'n'}")
command.append(" $main")

Comment on lines +161 to +163
fun launch(commandArray: Array<String>,
listenKey: TransportService.ListenKey,
ts: TransportService, cwd: String, envs: Array<String>? = null): VirtualMachine {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would read better if we moved the parameters to individual lines:

Suggested change
fun launch(commandArray: Array<String>,
listenKey: TransportService.ListenKey,
ts: TransportService, cwd: String, envs: Array<String>? = null): VirtualMachine {
fun launch(
commandArray: Array<String>,
listenKey: TransportService.ListenKey,
ts: TransportService,
cwd: String,
envs: Array<String>? = null
): VirtualMachine {

Comment on lines +167 to +168
return Bootstrap.virtualMachineManager().createVirtualMachine(connection,
process)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we already have a bunch of longer lines, I think not breaking would be fine here.

Suggested change
return Bootstrap.virtualMachineManager().createVirtualMachine(connection,
process)
return Bootstrap.virtualMachineManager().createVirtualMachine(connection, process)

Comment on lines +175 to +176
protected fun launchAndConnect(commandArray: Array<String>, listenKey: TransportService.ListenKey,
ts: TransportService, cwd: String = "", envs: Array<String>? = null): Pair<Connection, Process>{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected fun launchAndConnect(commandArray: Array<String>, listenKey: TransportService.ListenKey,
ts: TransportService, cwd: String = "", envs: Array<String>? = null): Pair<Connection, Process>{
protected fun launchAndConnect(
commandArray: Array<String>,
listenKey: TransportService.ListenKey,
ts: TransportService,
cwd: String = "",
envs: Array<String>? = null
): Pair<Connection, Process> {

Comment on lines +8 to +9
class StringArgument constructor(private val name: String, private val description: String = "", private val label: String = name,
private var value:String = "", private val mustSpecify: Boolean = false) : Connector.Argument {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we omit the constructor here, since it is the primary constructor?

Suggested change
class StringArgument constructor(private val name: String, private val description: String = "", private val label: String = name,
private var value:String = "", private val mustSpecify: Boolean = false) : Connector.Argument {
class StringArgument(
private val name: String,
private val description: String = "",
private val label: String = name,
private var value:String = "",
private val mustSpecify: Boolean = false
) : Connector.Argument {

}


}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if you could make sure that all edited files have trailing newlines (by Unix convention). Most editors should be configurable to do so, e.g. in VSCode you can set

"files.insertFinalNewline": true

in your settings.

@thunderz99
Copy link
Contributor Author

@fwcd Thank you for the advices, I will update this PR

@aharoldk
Copy link

aharoldk commented Mar 9, 2025

@thunderz99 please update the MR.

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

Successfully merging this pull request may close these issues.

4 participants