@@ -194,7 +194,9 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
194
194
)
195
195
196
196
if (options.sharedRun.watch.watchMode) {
197
- var processOpt = Option .empty[(Process , CompletableFuture [_])]
197
+ var processOpt = Option .empty[(Process , CompletableFuture [_])]
198
+ var shouldReadInput = false
199
+ var mainThreadOpt = Option .empty[Thread ]
198
200
val watcher = Build .watch(
199
201
inputs,
200
202
initialBuildOptions,
@@ -216,6 +218,8 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
216
218
for ((proc, _) <- processOpt if proc.isAlive)
217
219
// If the process doesn't exit, send SIGKILL
218
220
ProcUtil .forceKillProcess(proc, logger)
221
+ shouldReadInput = false
222
+ mainThreadOpt.foreach(_.interrupt())
219
223
val maybeProcess = maybeRun(
220
224
s,
221
225
allowTerminate = false ,
@@ -225,18 +229,34 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
225
229
)
226
230
.orReport(logger)
227
231
.flatten
232
+ .map {
233
+ case (proc, onExit) =>
234
+ if (options.sharedRun.watch.restart)
235
+ onExit.thenApply { _ =>
236
+ shouldReadInput = true
237
+ mainThreadOpt.foreach(_.interrupt())
238
+ }
239
+ (proc, onExit)
240
+ }
228
241
s.copyOutput(options.shared)
229
242
if (options.sharedRun.watch.restart)
230
243
processOpt = maybeProcess
231
- else
244
+ else {
232
245
for ((proc, onExit) <- maybeProcess)
233
246
ProcUtil .waitForProcess(proc, onExit)
247
+ shouldReadInput = true
248
+ mainThreadOpt.foreach(_.interrupt())
249
+ }
234
250
case _ : Build .Failed =>
235
251
System .err.println(" Compilation failed" )
236
252
}
237
253
}
238
- try WatchUtil .waitForCtrlC(() => watcher.schedule())
239
- finally watcher.dispose()
254
+ mainThreadOpt = Some (Thread .currentThread())
255
+ try WatchUtil .waitForCtrlC(() => watcher.schedule(), () => shouldReadInput)
256
+ finally {
257
+ mainThreadOpt = None
258
+ watcher.dispose()
259
+ }
240
260
}
241
261
else {
242
262
val builds =
0 commit comments