Skip to content

Commit

Permalink
runBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Sep 21, 2024
1 parent c37b7a1 commit 1f71332
Show file tree
Hide file tree
Showing 20 changed files with 46 additions and 49 deletions.
2 changes: 2 additions & 0 deletions main/src/main/scala/sbt/BackgroundJobService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ abstract class JobHandle {
def humanReadableName: String
def spawningTask: ScopedKey[_]
}

case class RunInfo(handle: JobHandle)
14 changes: 10 additions & 4 deletions main/src/main/scala/sbt/Defaults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,12 @@ object Defaults extends BuildCommon {
},
runMain := foregroundRunMainTask.evaluated,
run := foregroundRunTask.evaluated,
runBlock := {
val r = run.evaluated
val service = bgJobService.value
service.waitForTry(r.handle).get
r
},
fgRun := runTask(fullClasspath, (run / mainClass), (run / runner)).evaluated,
fgRunMain := runMainTask(fullClasspath, (run / runner)).evaluated,
copyResources := copyResourcesTask.value,
Expand Down Expand Up @@ -2146,27 +2152,27 @@ object Defaults extends BuildCommon {
}

// `runMain` calls bgRunMain in the background and pauses the current channel
def foregroundRunMainTask: Initialize[InputTask[RunVoid]] =
def foregroundRunMainTask: Initialize[InputTask[RunInfo]] =
Def.inputTask {
val handle = bgRunMain.evaluated
val service = bgJobService.value
val st = state.value
st.remainingCommands match
case Nil => service.waitForTry(handle).get
case _ => service.pauseChannelDuringJob(st, handle)
RunVoid
RunInfo(handle)
}

// `run` task calls bgRun in the background and pauses the current channel
def foregroundRunTask: Initialize[InputTask[RunVoid]] =
def foregroundRunTask: Initialize[InputTask[RunInfo]] =
Def.inputTask {
val handle = bgRun.evaluated
val service = bgJobService.value
val st = state.value
st.remainingCommands match
case Nil => service.waitForTry(handle).get
case _ => service.pauseChannelDuringJob(st, handle)
RunVoid
RunInfo(handle)
}

def runMainTask(
Expand Down
5 changes: 3 additions & 2 deletions main/src/main/scala/sbt/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,9 @@ object Keys {
// Run Keys
val selectMainClass = taskKey[Option[String]]("Selects the main class to run.").withRank(BMinusTask)
val mainClass = taskKey[Option[String]]("Defines the main class for packaging or running.").withRank(BPlusTask)
val run = inputKey[RunVoid]("Runs a main class, passing along arguments provided on the command line.").withRank(APlusTask)
val runMain = inputKey[RunVoid]("Runs the main class selected by the first argument, passing the remaining arguments to the main method.").withRank(ATask)
val run = inputKey[RunInfo]("Runs a main class, passing along arguments provided on the command line.").withRank(APlusTask)
val runBlock = inputKey[RunInfo]("Runs a main class, and blocks until it's done.").withRank(DTask)
val runMain = inputKey[RunInfo]("Runs the main class selected by the first argument, passing the remaining arguments to the main method.").withRank(ATask)
val discoveredMainClasses = taskKey[Seq[String]]("Auto-detects main classes.").withRank(BMinusTask)
val runner = taskKey[ScalaRun]("Implementation used to run a main class.").withRank(DTask)
val trapExit = settingKey[Boolean]("If true, enables exit trapping and thread management for 'run'-like tasks. This was removed in sbt 1.6.0 due to JDK 17 deprecating Security Manager.").withRank(CSetting)
Expand Down
8 changes: 4 additions & 4 deletions main/src/main/scala/sbt/internal/Aggregation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ object Aggregation {
case Result.Value(_) => true
case Result.Inc(_) => false
// run task ends earlier than the program run
val isRunVoid = results match
case Result.Value(Seq(KeyValue(_, RunVoid))) => true
case _ => false
val isRunInfo = results match
case Result.Value(Seq(KeyValue(_, RunInfo(_)))) => true
case _ => false
results.toEither.foreach { r =>
if show.taskValues then printSettings(r, show.print) else ()
}
if show.success && !isRunVoid && !state.get(suppressShow).getOrElse(false) then
if show.success && !isRunInfo && !state.get(suppressShow).getOrElse(false) then
printSuccess(start, stop, extracted, success, cacheSummary, log)
else ()

Expand Down
11 changes: 0 additions & 11 deletions run/src/main/scala/sbt/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,3 @@ object Run:
case str => str
}).mkString(" ")
end Run

/**
* RunVoid is a special `Unit` type used to indicate that it's a `run` task.
* When a task returns RunVoid, [success] log is omitted.
*/
sealed trait RunVoid

/**
* The only resident of RunVoid type.
*/
case object RunVoid extends RunVoid
4 changes: 2 additions & 2 deletions sbt-app/src/sbt-test/actions/add-alias/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addCommandAlias("demo-success", "run true")
addCommandAlias("demo-failure", "run false")
addCommandAlias("demo-success", "runBlock true")
addCommandAlias("demo-failure", "runBlock false")
addCommandAlias("z", "scalaVersion")
4 changes: 2 additions & 2 deletions sbt-app/src/sbt-test/classloader-cache/jni/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ val dropLibraryPath = taskKey[Unit]("Drop the last path from the java.library.pa
val wrappedRun = taskKey[Unit]("Run with modified java.library.path")
val wrappedTest = taskKey[Unit]("Test with modified java.library.path")

def wrap(task: InputKey[Unit]): Def.Initialize[Task[Unit]] =
def wrap[A1](task: InputKey[A1]): Def.Initialize[Task[Unit]] =
Def.sequential(appendToLibraryPath, task.toTask(""), dropLibraryPath)

// ThisBuild / turbo := true
Expand Down Expand Up @@ -35,6 +35,6 @@ val root = (project in file(".")).settings(
val cp = System.getProperty("java.library.path", "").split(":").dropRight(1)
System.setProperty("java.library.path", cp.mkString(":"))
},
wrappedRun := wrap(Runtime / run).value,
wrappedRun := wrap(Runtime / runBlock).value,
wrappedTest := wrap(Test / testOnly).value
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
$ delete output
> run
> runBlock
$ exists output
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
$ delete output
> run
> runBlock
$ exists output
2 changes: 1 addition & 1 deletion sbt-app/src/sbt-test/dependency-management/profiles/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
$ delete output
> run
> runBlock
$ exists output
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
> a/checkLibs
> b/checkLibs

> b/run
> b/runBlock
$ exists s2.13.8.txt
$ delete s2.13.8.txt

# don't crash when expanding the macro
> b3/run
> b3/runBlock
$ exists s2.13.10.txt
$ delete s2.13.10.txt

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
$ delete output
> run
> runBlock
$ exists output
4 changes: 2 additions & 2 deletions sbt-app/src/sbt-test/package/resources/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# This should fail because the Main object is in package jartest and the resource is directly
# in src/main/resources
-> run
-> runBlock

> package

Expand All @@ -18,7 +18,7 @@ $ copy-file src/main/resources/main_resource_test src/main/resources/jartest/mai
$ delete src/main/resources/main_resource_test

# This should succeed because sbt should put the resource on the runClasspath
> run
> runBlock

# This is necessary because package bases whether or not to run on last modified times, which don't have
# high enough resolution to notice the above move of main_resource_test
Expand Down
8 changes: 4 additions & 4 deletions sbt-app/src/sbt-test/source-dependencies/constants/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
$ copy-file changes/B.scala B.scala

$ copy-file changes/A1.scala A.scala
> run 1
> runBlock 1
$ copy-file changes/A2.scala A.scala
> run 2
> runBlock 2

> clean
> ++2.13.12!

$ copy-file changes/A1.scala A.scala
> run 1
> runBlock 1
$ copy-file changes/A2.scala A.scala
> run 2
> runBlock 2
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
ThisBuild / scalaVersion := "2.12.17"

scalaVersion := "2.12.19"
4 changes: 2 additions & 2 deletions sbt-app/src/sbt-test/source-dependencies/implicit-search/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ $ copy-file changes/A1.scala A.scala
$ copy-file changes/B.scala B.scala
$ copy-file changes/C.scala C.scala
> compile
-> run
-> runBlock

$ copy-file changes/A2.scala A.scala
$ sleep 1000

> compile
> run
> runBlock
4 changes: 2 additions & 2 deletions sbt-app/src/sbt-test/source-dependencies/java-basic/test
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ $ delete src/main/java/a/A.java
# It shouldn't run though, because it doesn't have a main method
$ copy-file changes/B1.java src/main/java/a/b/B.java
> compile
-> run
-> runBlock


# Replace B with a new B that has a main method and should therefore run
# if the main method was properly detected
$ copy-file changes/B3.java src/main/java/a/b/B.java
> run
> runBlock
4 changes: 2 additions & 2 deletions sbt-app/src/sbt-test/source-dependencies/linearization/test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
> compile

# the value of F.x should be 16
> run 16
> runBlock 16

# modify D.scala so that the linearization changes
$ copy-file changes/D.scala D.scala
Expand All @@ -12,4 +12,4 @@ $ sleep 1000

# if F is recompiled, the value of x should be 11, otherwise it will still be 16
# and this will fail
> run 11
> runBlock 11
4 changes: 2 additions & 2 deletions sbt-app/src/sbt-test/source-dependencies/named/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
> compile

# result should be 1
> run 1
> runBlock 1

# change order of arguments in A.x
$ copy-file changes/A.scala A.scala
Expand All @@ -13,4 +13,4 @@ $ copy-file changes/A.scala A.scala
> compile

# Should still get 1 and not -1
> run 1
> runBlock 1
4 changes: 2 additions & 2 deletions sbt-app/src/sbt-test/source-dependencies/specialized/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
> compile

# verify that erased A.x can be called normally and reflectively
> run false
> runBlock false

# make A.x specialized
$ copy-file changes/A.scala A.scala
Expand All @@ -12,4 +12,4 @@ $ copy-file changes/A.scala A.scala

# verify that specialized A.x can be called normally and reflectively
# NOTE: this test doesn't actually work correctly: have to check the output to see that B.scala was recompiled
> run true
> runBlock true

0 comments on commit 1f71332

Please sign in to comment.