Skip to content

Commit cd4ca47

Browse files
committed
Allow for --list-main-classes to work in watch mode
1 parent 726d3e2 commit cd4ca47

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

modules/cli/src/main/scala/scala/cli/commands/Run.scala

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,43 @@ object Run extends ScalaCommand[RunOptions] {
6666
def maybeRun(
6767
build: Build.Successful,
6868
allowTerminate: Boolean
69-
): Either[BuildException, (Process, CompletableFuture[_])] = either {
69+
): Either[BuildException, Option[(Process, CompletableFuture[_])]] = either {
7070
val potentialMainClasses = build.foundMainClasses()
71-
value(options.mainClass.maybePrintMainClasses(potentialMainClasses))
72-
val process = value(maybeRunOnce(
73-
build,
74-
programArgs,
75-
logger,
76-
allowExecve = allowTerminate,
77-
jvmRunner = build.artifacts.hasJvmRunner,
78-
potentialMainClasses
79-
))
71+
if (options.mainClass.mainClassLs.contains(true))
72+
value {
73+
options.mainClass
74+
.maybePrintMainClasses(potentialMainClasses, shouldExit = allowTerminate)
75+
.map(_ => None)
76+
}
77+
else {
78+
val process = value {
79+
maybeRunOnce(
80+
build,
81+
programArgs,
82+
logger,
83+
allowExecve = allowTerminate,
84+
jvmRunner = build.artifacts.hasJvmRunner,
85+
potentialMainClasses
86+
)
87+
}
8088

81-
val onExitProcess = process.onExit().thenApply { p1 =>
82-
val retCode = p1.exitValue()
83-
if (retCode != 0)
84-
if (allowTerminate)
85-
sys.exit(retCode)
86-
else {
87-
val red = Console.RED
88-
val lightRed = "\u001b[91m"
89-
val reset = Console.RESET
90-
System.err.println(
91-
s"${red}Program exited with return code $lightRed$retCode$red.$reset"
92-
)
93-
}
94-
}
89+
val onExitProcess = process.onExit().thenApply { p1 =>
90+
val retCode = p1.exitValue()
91+
if (retCode != 0)
92+
if (allowTerminate)
93+
sys.exit(retCode)
94+
else {
95+
val red = Console.RED
96+
val lightRed = "\u001b[91m"
97+
val reset = Console.RESET
98+
System.err.println(
99+
s"${red}Program exited with return code $lightRed$retCode$red.$reset"
100+
)
101+
}
102+
}
95103

96-
(process, onExitProcess)
104+
Some((process, onExitProcess))
105+
}
97106
}
98107

99108
val cross = options.compileCross.cross.getOrElse(false)
@@ -130,6 +139,7 @@ object Run extends ScalaCommand[RunOptions] {
130139
if (proc.isAlive) ProcUtil.forceKillProcess(proc, logger)
131140
val maybeProcess = maybeRun(s, allowTerminate = false)
132141
.orReport(logger)
142+
.flatten
133143
if (options.watch.restart)
134144
processOpt = maybeProcess
135145
else
@@ -158,7 +168,7 @@ object Run extends ScalaCommand[RunOptions] {
158168
builds.main match {
159169
case s: Build.Successful =>
160170
val (process, onExit) = maybeRun(s, allowTerminate = true)
161-
.orExit(logger)
171+
.orExit(logger).getOrElse(sys.exit(1))
162172
ProcUtil.waitForProcess(process, onExit)
163173
case _: Build.Failed =>
164174
System.err.println("Compilation failed")

modules/cli/src/main/scala/scala/cli/commands/util/MainClassOptionsUtil.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import scala.cli.commands.MainClassOptions
55

66
object MainClassOptionsUtil {
77
implicit class MainClassOptionsOps(v: MainClassOptions) {
8-
def maybePrintMainClasses(mainClasses: Seq[String]): Either[MainClassError, Unit] =
8+
def maybePrintMainClasses(
9+
mainClasses: Seq[String],
10+
shouldExit: Boolean = true
11+
): Either[MainClassError, Unit] =
912
v.mainClassLs match {
1013
case Some(true) if mainClasses.nonEmpty =>
1114
println(mainClasses.mkString(" "))
12-
sys.exit(0)
15+
if (shouldExit) sys.exit(0)
16+
else Right(())
1317
case Some(true) => Left(new NoMainClassFoundError)
1418
case _ => Right(())
1519
}

0 commit comments

Comments
 (0)