Only trigger reload if sources have changed

Running a `~` command in a local build off the latest develop branch
will cause the build to reload even if the build sources were only
touched and not actually modified.
This commit is contained in:
Ethan Atkins 2020-07-09 09:07:10 -07:00
parent de1423d662
commit d826e93ddf
5 changed files with 22 additions and 9 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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,7 +160,8 @@ 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 Some(cbs) if (cbs.needsReload(st, st.globalLogging.full, "")) =>
StateTransform("reload" :: (_: State))
case _ => StateTransform(identity)
}
}

View File

@ -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

View File

@ -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
}