Skip to content

Commit 8d442c8

Browse files
committed
Support System.exit in worksheet
1 parent 2b45c01 commit 8d442c8

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

language-server/src/dotty/tools/languageserver/Worksheet.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object Worksheet {
3939
case statement: DefTree if statement.symbol.is(Synthetic) =>
4040
()
4141

42-
case statement if executed.add(bounds(statement.pos)) =>
42+
case statement if evaluator.isAlive() && executed.add(bounds(statement.pos)) =>
4343
try {
4444
cancelChecker.checkCanceled()
4545
val (line, result) = execute(evaluator, statement, tree.source)
@@ -100,7 +100,7 @@ private object Evaluator {
100100
def get(cancelChecker: CancelChecker)(implicit ctx: Context): Option[Evaluator] = {
101101
val classpath = ctx.settings.classpath.value
102102
previousEvaluator match {
103-
case Some(cp, evaluator) if cp == classpath =>
103+
case Some(cp, evaluator) if evaluator.isAlive() && cp == classpath =>
104104
evaluator.reset(cancelChecker)
105105
Some(evaluator)
106106
case _ =>
@@ -146,6 +146,9 @@ private class Evaluator private (javaExec: String,
146146
// Wait for the REPL to be ready
147147
processOutput.next()
148148

149+
/** Is the process that runs the REPL still alive? */
150+
def isAlive(): Boolean = process.isAlive()
151+
149152
/**
150153
* Submit `command` to the REPL, wait for the result.
151154
*
@@ -185,7 +188,7 @@ private class CancellationThread(private[this] var cancelChecker: CancelChecker,
185188

186189
override def run(): Unit = {
187190
try {
188-
while (!Thread.interrupted()) {
191+
while (evaluator.isAlive() && !Thread.interrupted()) {
189192
cancelChecker.checkCanceled()
190193
Thread.sleep(checkCancelledDelayMs)
191194
}

language-server/test/dotty/tools/languageserver/WorksheetTest.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,11 @@ class WorksheetTest {
199199
.cancelEvaluation(m1, afterMs = 5000)
200200
}
201201

202+
@Test def systemExit(): Unit = {
203+
ws"""${m1}println("Hello, world!")
204+
System.exit(0)
205+
println("Goodbye!")""".withSource
206+
.evaluate(m1, "1:Hello, world!")
207+
}
208+
202209
}

0 commit comments

Comments
 (0)