Skip to content

Commit

Permalink
Fix the -jvm-debug and -h flags in the ash template (#1630)
Browse files Browse the repository at this point in the history
* Add bash and ash tests for the -jvm-debug and -h flags

* Add missing functions to the ash template

* replace bash syntax with ash compatible one

* Revert "Fixes remote debug connections closing (#1546)"

This reverts commit 665e4e2.

* fix sbt 2.0 cross-build for new tests

---------

Co-authored-by: Muki Seiler <muuki88@users.noreply.github.com>
  • Loading branch information
qwe2 and muuki88 authored Feb 4, 2025
1 parent 5a12bdb commit eecea01
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/bin/sh

die() {
echo "$@" 1>&2
exit 1
}

realpath () {
(
TARGET_FILE="$1"
Expand Down Expand Up @@ -51,6 +56,19 @@ addResidual () {
residual_args="$residual_args $(shellEscape "$1")"
}

addDebugger () {
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1"
}

require_arg () {
local type="$1"
local opt="$2"
local arg="$3"
if [ -z "$arg" ] || [ "${arg#-}" != "$arg" ]; then
die "$opt requires <$type> argument"
fi
}

# Allow user to specify java options. These get listed first per bash-template.
if [ -n "$JAVA_OPTS" ]
then
Expand Down Expand Up @@ -81,10 +99,6 @@ process_args () {
case "$1" in
--) shift && no_more_snp_opts=1 && break ;;
-h|-help) usage; exit 1 ;;
-v|-verbose) verbose=1 && shift ;;
-d|-debug) debug=1 && shift ;;

-no-version-check) no_version_check=1 && shift ;;

-mem) echo "!! WARNING !! -mem option is ignored. Please use -J-Xmx and -J-Xms" && shift 2 ;;
-jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;;
Expand All @@ -99,13 +113,41 @@ process_args () {
esac
done

if [ $no_more_snp_opts ]; then
if [ $no_more_snp_opts -ne 0 ]; then
while [ $# -gt 0 ]; do
addResidual "$1" && shift
done
fi
}

usage() {
cat <<EOM
Usage: $script_name [options]
-h | -help print this message
-main <classname> Define a custom main class
-jvm-debug <port> Turn on JVM debugging, open at the given port.
# java version (default: java from PATH, currently $(java -version 2>&1 | grep version))
-java-home <path> alternate JAVA_HOME
# jvm options and output control
JAVA_OPTS environment variable, if unset uses "$java_opts"
-Dkey=val pass -Dkey=val directly to the java runtime
-J-X pass option -X directly to the java runtime
(-J is stripped)
# special option
-- To stop parsing built-in commands from the rest of the command-line.
e.g.) enabling debug and sending -d as app argument
\$ ./start-script -d -- -d
In the case of duplicated or conflicting options, basically the order above
shows precedence: JAVA_OPTS lowest, command line options highest except "--".
${{available_main_classes}}
EOM
}

app_commands=""
residual_args=""
real_script_path="$(realpath "$0")"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ addResidual () {
residual_args+=( "$1" )
}
addDebugger () {
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:$1"
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1"
}

require_arg () {
Expand Down
15 changes: 15 additions & 0 deletions src/sbt-test/ash/script-debug/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enablePlugins(JavaAppPackaging, AshScriptPlugin)

name := "script-debug"

version := "0.1.0"

TaskKey[Unit]("runCheck") := {
val cwd = (Universal / stagingDirectory).value
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, "-jvm-debug", "0")
val output = (sys.process.Process(cmd, cwd).!!).replaceAll("\n", "")

assert(output.contains("Listening for transport dt_socket at address:"),
"Application did not start in debug mode: \n" + output)
assert(output.contains("SUCCESS!"), "Application did not run successfully: \n" + output)
}
1 change: 1 addition & 0 deletions src/sbt-test/ash/script-debug/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
3 changes: 3 additions & 0 deletions src/sbt-test/ash/script-debug/src/main/scala/MainApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object MainApp extends App {
println("SUCCESS!")
}
4 changes: 4 additions & 0 deletions src/sbt-test/ash/script-debug/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Run the staging and check the script.
> stage
$ exists target/**/universal/stage/bin/script-debug
> runCheck
26 changes: 26 additions & 0 deletions src/sbt-test/ash/script-help/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
enablePlugins(JavaAppPackaging, AshScriptPlugin)

name := "script-help"

version := "0.1.0"

TaskKey[Unit]("runCheck") := {
val cwd = (Universal / stagingDirectory).value
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, "-h")

val buffer = new StringBuffer
val code = sys.process.Process(cmd, cwd).run(scala.sys.process.BasicIO(false, buffer, None)).exitValue()
assert(code == 1, "Exit code for -h was not 1: " + code)

val output = buffer.toString.replaceAll("\n", "")

val expectedHelpSamples = Seq(
"-h | -help", "print this message",
"-jvm-debug",
"JAVA_OPTS",
"special option"
)

assert(expectedHelpSamples.forall(output.contains(_)),
s"Application did not print the correct help message: \n" + output)
}
1 change: 1 addition & 0 deletions src/sbt-test/ash/script-help/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
3 changes: 3 additions & 0 deletions src/sbt-test/ash/script-help/src/main/scala/MainApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object MainApp extends App {
println("SUCCESS!")
}
4 changes: 4 additions & 0 deletions src/sbt-test/ash/script-help/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Run the staging and check the script.
> stage
$ exists target/**/universal/stage/bin/script-help
> runCheck
17 changes: 17 additions & 0 deletions src/sbt-test/bash/script-debug/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.net.ServerSocket

enablePlugins(JavaAppPackaging)

name := "script-debug"

version := "0.1.0"

TaskKey[Unit]("runCheck") := {
val cwd = (Universal / stagingDirectory).value
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, "-jvm-debug", "0")
val output = (sys.process.Process(cmd, cwd).!!).replaceAll("\n", "")

assert(output.contains("Listening for transport dt_socket at address:"),
"Application did not start in debug mode: \n" + output)
assert(output.contains("SUCCESS!"), "Application did not run successfully: \n" + output)
}
1 change: 1 addition & 0 deletions src/sbt-test/bash/script-debug/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
3 changes: 3 additions & 0 deletions src/sbt-test/bash/script-debug/src/main/scala/MainApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object MainApp extends App {
println("SUCCESS!")
}
4 changes: 4 additions & 0 deletions src/sbt-test/bash/script-debug/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Run the staging and check the script.
> stage
$ exists target/**/universal/stage/bin/script-debug
> runCheck
26 changes: 26 additions & 0 deletions src/sbt-test/bash/script-help/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
enablePlugins(JavaAppPackaging)

name := "script-help"

version := "0.1.0"

TaskKey[Unit]("runCheck") := {
val cwd = (Universal / stagingDirectory).value
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, "-h")

val buffer = new StringBuffer
val code = sys.process.Process(cmd, cwd).run(scala.sys.process.BasicIO(false, buffer, None)).exitValue()
assert(code == 1, "Exit code for -h was not 1: " + code)

val output = buffer.toString.replaceAll("\n", "")

val expectedHelpSamples = Seq(
"-h | -help", "print this message",
"-jvm-debug",
"JAVA_OPTS",
"special option"
)

assert(expectedHelpSamples.forall(output.contains(_)),
s"Application did not print the correct help message: \n" + output)
}
1 change: 1 addition & 0 deletions src/sbt-test/bash/script-help/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("project.version"))
3 changes: 3 additions & 0 deletions src/sbt-test/bash/script-help/src/main/scala/MainApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object MainApp extends App {
println("SUCCESS!")
}
4 changes: 4 additions & 0 deletions src/sbt-test/bash/script-help/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Run the staging and check the script.
> stage
$ exists target/**/universal/stage/bin/script-help
> runCheck

0 comments on commit eecea01

Please sign in to comment.