Skip to content

Commit

Permalink
Enable debugging for run and test commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wleczny committed Sep 27, 2022
1 parent 19b8f0c commit c523fe1
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package scala.cli.commands

import caseapp.*
import caseapp.core.help.Help

import scala.cli.commands.BloopExitOptions.parser

// format: off
final case class SharedDebugOptions(
@Group("Debug")
@HelpMessage("Turn debugging on")
debug: Boolean = false,
@Group("Debug")
@HelpMessage("Debug port (5005 by default)")
debugPort: Option[String] = None,
@Group("Debug")
@HelpMessage("Debug mode (attach by default)")
@ValueDescription("attach|a|listen|l")
debugMode: Option[String] = None
)
// format: on

object SharedDebugOptions {
implicit lazy val parser: Parser[SharedDebugOptions] = Parser.derive
implicit lazy val help: Help[SharedDebugOptions] = Help.derive
// Parser.Aux for using SharedDebugOptions with @Recurse in other options
implicit lazy val parserAux: Parser.Aux[SharedDebugOptions, parser.D] = parser
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.github.plokhotnyuk.jsoniter_scala.macros._

// format: off
final case class SharedJvmOptions(
@Recurse
sharedDebug: SharedDebugOptions = SharedDebugOptions(),

@Group("Java")
@HelpMessage("Set the Java home directory")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object BloopStart extends ScalaCommand[BloopStartOptions] {
private def mkBloopRifleConfig(opts: BloopStartOptions): BloopRifleConfig = {
import opts._
val buildOptions = BuildOptions(
javaOptions = JvmUtils.javaOptions(jvm),
javaOptions = JvmUtils.javaOptions(jvm).orExit(logging.logger),
internal = InternalOptions(
cache = Some(coursier.coursierCache(logging.logger.coursierLogger("")))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object Config extends ScalaCommand[ConfigOptions] {
logger,
coursierCache,
() =>
JvmUtils.javaOptions(options.jvm).javaHome(
JvmUtils.javaOptions(options.jvm).orExit(logger).javaHome(
ArchiveCache().withCache(coursierCache),
coursierCache,
logger.verbosity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ abstract class PgpExternalCommand extends ExternalCommand {
logger,
allowExecve = true,
() =>
JvmUtils.javaOptions(options.jvm).javaHome(
JvmUtils.javaOptions(options.jvm).orExit(logger).javaHome(
ArchiveCache().withCache(cache),
cache,
logger.verbosity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object PgpPush extends ScalaCommand[PgpPushOptions] {
val keyContent = os.read(path)

val javaCommand = () =>
JvmUtils.javaOptions(options.jvm).javaHome(
JvmUtils.javaOptions(options.jvm).orExit(logger).javaHome(
ArchiveCache().withCache(coursierCache),
coursierCache,
logger.verbosity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ final case class PgpSecretKeyCheck(
else
Base64.getEncoder().encodeToString(input)

def javaCommand: () => String =
def javaCommand: Either[BuildException, () => String] = either {
() =>
JvmUtils.javaOptions(options.sharedJvm).javaHome(
value(JvmUtils.javaOptions(options.sharedJvm)).javaHome(
ArchiveCache().withCache(coursierCache),
coursierCache,
logger.verbosity
).value.javaCommand
}

def defaultValue(): Either[BuildException, OptionCheck.DefaultValue] =
either {
Expand Down Expand Up @@ -114,7 +115,7 @@ final case class PgpSecretKeyCheck(
password,
logger,
coursierCache,
javaCommand
value(javaCommand)
)
}
val pgpSecretBase64 = pgpSecret0.map(Base64.getEncoder.encodeToString)
Expand Down Expand Up @@ -150,7 +151,7 @@ final case class PgpSecretKeyCheck(
"[generated key]",
coursierCache,
logger,
javaCommand
value(javaCommand)
)
}
val keyServers = value {
Expand Down
29 changes: 27 additions & 2 deletions modules/cli/src/main/scala/scala/cli/commands/util/JvmUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package util

import java.io.File

import scala.build.options.JavaOptions
import scala.build.EitherCps.{either, value}
import scala.build.errors.{BuildException, UnrecognizedDebugModeError}
import scala.build.options.{JavaOpt, JavaOptions, ShadowingSeq}
import scala.build.{Os, Position, Positioned}
import scala.util.Properties

object JvmUtils {
def javaOptions(opts: SharedJvmOptions) = {
def javaOptions(opts: SharedJvmOptions): Either[BuildException, JavaOptions] = either {
import opts._

val (javacFilePlugins, javacPluginDeps) =
Expand All @@ -20,6 +22,28 @@ object JvmUtils {
input.count(_ == ':') < 2
}

val javaOptsSeq = {
val isDebug =
opts.sharedDebug.debug ||
opts.sharedDebug.debugMode.nonEmpty ||
opts.sharedDebug.debugPort.nonEmpty
if (isDebug) {
val server = value {
opts.sharedDebug.debugMode match {
case Some("attach") | Some("a") | None => Right("y")
case Some("listen") | Some("l") => Right("n")
case Some(m) => Left(new UnrecognizedDebugModeError(m))
}
}
val port = opts.sharedDebug.debugPort.getOrElse("5005")
Seq(Positioned.none(
JavaOpt(s"-agentlib:jdwp=transport=dt_socket,server=$server,suspend=y,address=$port")
))
}
else
Seq.empty
}

JavaOptions(
javaHomeOpt = javaHome.filter(_.nonEmpty).map(v =>
Positioned(Seq(Position.CommandLine("--java-home")), os.Path(v, Os.pwd))
Expand All @@ -28,6 +52,7 @@ object JvmUtils {
jvmIndexOpt = jvmIndex.filter(_.nonEmpty),
jvmIndexOs = jvmIndexOs.map(_.trim).filter(_.nonEmpty),
jvmIndexArch = jvmIndexArch.map(_.trim).filter(_.nonEmpty),
javaOpts = ShadowingSeq.from(javaOptsSeq),
javacPluginDependencies = SharedOptionsUtil.parseDependencies(
javacPluginDeps.map(Positioned.none(_)),
ignoreErrors = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ object SharedOptionsUtil extends CommandHelpers {
),
scalaJsOptions = scalaJsOptions(js),
scalaNativeOptions = scalaNativeOptions(native),
javaOptions = scala.cli.commands.util.JvmUtils.javaOptions(jvm),
javaOptions = value(scala.cli.commands.util.JvmUtils.javaOptions(jvm)),
internalDependencies = bo.InternalDependenciesOptions(
addStubsDependencyOpt = addStubs,
addRunnerDependencyOpt = runner
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package scala.build.errors

final class UnrecognizedDebugModeError(mode: String) extends BuildException(
s"Unrecognized debug mode: $mode."
)
18 changes: 18 additions & 0 deletions website/docs/commands/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,21 @@ object HelloWorld extends App {
docker run -v $(pwd)/HelloWorld.scala:/HelloWorld.scala virtuslab/scala-cli /HelloWorld.scala
# Hello world
```

## Debugging

It is possible to debug code by passing `--debug` flag.

Additional debug options:
* `--debug-mode` (attach by default)
* `--debug-port` (5005 by default)

Available debug modes:
* Attach (`attach` | `att` | `a`)
* Listen (`listen` | `lis` | `l`)

Example debugging with scala-cli:

```bash ignore
scala-cli Foo.scala --debug --debug-mode l --debug-port 5006
```
20 changes: 20 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ Available in commands:

[experimental] Run given command against all provided Scala versions and/or platforms

## Debug options

Available in commands:

[`bloop`](./commands.md#bloop), [`bloop start`](./commands.md#bloop-start), [`bsp`](./commands.md#bsp), [`compile`](./commands.md#compile), [`config`](./commands.md#config), [`dependency-update`](./commands.md#dependency-update), [`doc`](./commands.md#doc), [`export`](./commands.md#export), [`fmt` , `format` , `scalafmt`](./commands.md#fmt), [`browse` , `metabrowse`](./commands.md#browse), [`package`](./commands.md#package), [`pgp push`](./commands.md#pgp-push), [`publish`](./commands.md#publish), [`publish local`](./commands.md#publish-local), [`publish setup`](./commands.md#publish-setup), [`repl` , `console`](./commands.md#repl), [`run`](./commands.md#run), [`setup-ide`](./commands.md#setup-ide), [`shebang`](./commands.md#shebang), [`test`](./commands.md#test)

<!-- Automatically generated, DO NOT EDIT MANUALLY -->

### `--debug`

Turn debugging on

### `--debug-port`

Debug port (5005 by default)

### `--debug-mode`

Debug mode (attach by default)

## Dependency options

Available in commands:
Expand Down
Loading

0 comments on commit c523fe1

Please sign in to comment.