diff --git a/main/src/main/scala/sbt/MainLoop.scala b/main/src/main/scala/sbt/MainLoop.scala index 0ba3099cc..e50a1465f 100644 --- a/main/src/main/scala/sbt/MainLoop.scala +++ b/main/src/main/scala/sbt/MainLoop.scala @@ -218,7 +218,7 @@ object MainLoop { } state.get(CheckBuildSourcesKey) match { case Some(cbs) => - if (!cbs.needsReload(state, exec.commandLine)) process() + if (!cbs.needsReload(state, state.globalLogging.full, exec.commandLine)) process() else { if (exec.commandLine.startsWith(SetTerminal)) exec +: Exec("reload", None, None) +: state.remove(CheckBuildSourcesKey) diff --git a/main/src/main/scala/sbt/internal/Continuous.scala b/main/src/main/scala/sbt/internal/Continuous.scala index c9ea072c7..274ca692f 100644 --- a/main/src/main/scala/sbt/internal/Continuous.scala +++ b/main/src/main/scala/sbt/internal/Continuous.scala @@ -526,7 +526,14 @@ private[sbt] object Continuous extends DeprecatedContinuous { } if (buildGlobs.exists(_.matches(path))) { - getWatchEvent(forceTrigger = false).map(e => e -> Watch.Reload).toSeq + getWatchEvent(forceTrigger = false).flatMap { e => + state.get(CheckBuildSources.CheckBuildSourcesKey) match { + case Some(cbs) => + if (cbs.needsReload(state, logger, "")) Some(e -> Watch.Reload) else None + case None => + Some(e -> Watch.Reload) + } + }.toSeq } else { val acceptedConfigParameters = configs.flatMap { config => config.inputs().flatMap { diff --git a/main/src/main/scala/sbt/internal/nio/CheckBuildSources.scala b/main/src/main/scala/sbt/internal/nio/CheckBuildSources.scala index 326e08f15..53c29cf43 100644 --- a/main/src/main/scala/sbt/internal/nio/CheckBuildSources.scala +++ b/main/src/main/scala/sbt/internal/nio/CheckBuildSources.scala @@ -24,6 +24,7 @@ import sbt.nio.Keys._ import sbt.nio.file.{ FileAttributes, FileTreeView, Glob, ** } import sbt.nio.file.syntax._ import sbt.nio.Settings +import sbt.util.Logger import scala.annotation.tailrec import scala.concurrent.duration.{ Deadline => SDeadline, _ } @@ -102,11 +103,10 @@ private[sbt] class CheckBuildSources extends AutoCloseable { !resetState } @inline private def forceCheck = fileTreeRepository.isEmpty - private[sbt] def needsReload(state: State, cmd: String) = { + private[sbt] def needsReload(state: State, logger: Logger, cmd: String) = { (needCheck(state, cmd) && (forceCheck || needUpdate.compareAndSet(true, false))) && { val extracted = Project.extract(state) val onChanges = extracted.get(Global / onChangedBuildSource) - val logger = state.globalLogging.full val current = getStamps(force = false) val previous = previousStamps.getAndSet(current) Settings.changedFiles(previous, current) match { @@ -160,8 +160,9 @@ private[sbt] object CheckBuildSources { private[sbt] def needReloadImpl: Def.Initialize[Task[StateTransform]] = Def.task { val st = state.value st.get(CheckBuildSourcesKey) match { - case Some(cbs) if (cbs.needsReload(st, "")) => StateTransform("reload" :: (_: State)) - case _ => StateTransform(identity) + case Some(cbs) if (cbs.needsReload(st, st.globalLogging.full, "")) => + StateTransform("reload" :: (_: State)) + case _ => StateTransform(identity) } } private[sbt] def buildSourceFileInputs: Def.Initialize[Seq[Glob]] = Def.setting { diff --git a/sbt/src/sbt-test/watch/on-start-watch/build.sbt b/sbt/src/sbt-test/watch/on-start-watch/build.sbt index 3d50e58b4..808416d2a 100644 --- a/sbt/src/sbt-test/watch/on-start-watch/build.sbt +++ b/sbt/src/sbt-test/watch/on-start-watch/build.sbt @@ -16,4 +16,11 @@ failingTask := { throw new IllegalStateException("failed") } -onChangedBuildSource := ReloadOnSourceChanges +watchOnIteration := { (count, project, commands) => + val extra = baseDirectory.value / "extra.sbt" + IO.copyFile(baseDirectory.value / "changes" / "extra.sbt", extra, CopyOptions().withOverwrite(true)) + Watch.defaultStartWatch(count, project, commands).foreach(_.linesIterator.foreach(l => println(s"[info] $l"))) + Watch.Ignore +} + +Global / onChangedBuildSource := ReloadOnSourceChanges diff --git a/sbt/src/sbt-test/watch/on-start-watch/extra.sbt b/sbt/src/sbt-test/watch/on-start-watch/extra.sbt index ce346f6b9..a419d1c94 100644 --- a/sbt/src/sbt-test/watch/on-start-watch/extra.sbt +++ b/sbt/src/sbt-test/watch/on-start-watch/extra.sbt @@ -1,7 +1,5 @@ Compile / compile := { Count.increment() // Trigger a new build by updating the last modified time - val extra = baseDirectory.value / "extra.sbt" - IO.copyFile(baseDirectory.value / "changes" / "extra.sbt", extra, CopyOptions().withOverwrite(true)) (Compile / compile).value }