Skip to content

Commit

Permalink
[2.x] Fix dependency-management/force-update-period
Browse files Browse the repository at this point in the history
  • Loading branch information
adpi2 committed May 21, 2024
1 parent b27c725 commit 5deb6e6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion main/src/main/scala/sbt/Defaults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3797,7 +3797,7 @@ object Classpaths {
forceUpdatePeriod.value match {
case None => false
case Some(period) =>
val fullUpdateOutput = cacheDirectory / "out"
val fullUpdateOutput = cacheDirectory / "output"
val now = System.currentTimeMillis
val diff = now - IO.getModifiedTimeOrZero(fullUpdateOutput)
val elapsedDuration = new FiniteDuration(diff, TimeUnit.MILLISECONDS)
Expand Down
44 changes: 26 additions & 18 deletions main/src/main/scala/sbt/internal/LibraryManagement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ private[sbt] object LibraryManagement {
}

/* Skip resolve if last output exists, otherwise error. */
def skipResolve(cache: CacheStore): UpdateInputs => UpdateReport = {
def skipResolve(cache: CacheStore)(inputs: UpdateInputs): UpdateReport = {
import sbt.librarymanagement.LibraryManagementCodec._
Tracked.lastOutput[UpdateInputs, UpdateReport](cache) {
case (_, Some(out)) => markAsCached(out)
case _ =>
sys.error("Skipping update requested, but update has not previously run successfully.")
}
val cachedReport = Tracked
.lastOutput[UpdateInputs, UpdateReport](cache) {
case (_, Some(out)) => out
case _ =>
sys.error("Skipping update requested, but update has not previously run successfully.")
}
.apply(inputs)
markAsCached(cachedReport)
}

// Mark UpdateReport#stats as "cached." This is used by the dependers later
Expand All @@ -127,24 +130,29 @@ private[sbt] object LibraryManagement {
def doResolve(cache: CacheStore): UpdateInputs => UpdateReport = {
val doCachedResolve = { (inChanged: Boolean, updateInputs: UpdateInputs) =>
import sbt.librarymanagement.LibraryManagementCodec._
val cachedResolve = Tracked.lastOutput[UpdateInputs, UpdateReport](cache) {
case (_, Some(out)) if upToDate(inChanged, out) => markAsCached(out)
case pair =>
log.debug(s"""not up to date. inChanged = $inChanged, force = $force""")
resolve
}
import scala.util.control.Exception.catching
catching(classOf[NullPointerException], classOf[OutOfMemoryError])
.withApply { t =>
try {
var isCached = false
val report = Tracked
.lastOutput[UpdateInputs, UpdateReport](cache) {
case (_, Some(out)) if upToDate(inChanged, out) =>
isCached = true
out
case pair =>
log.debug(s"""not up to date. inChanged = $inChanged, force = $force""")
resolve
}
.apply(updateInputs)
if (isCached) markAsCached(report) else report
} catch {
case t @ (_: NullPointerException | _: OutOfMemoryError) =>
val resolvedAgain = resolve
val culprit = t.getClass.getSimpleName
log.warn(s"Update task caching failed due to $culprit.")
log.warn("Report the following output to sbt:")
resolvedAgain.toString.linesIterator.foreach(log.warn(_))
log.trace(t)
resolvedAgain
}
.apply(cachedResolve(updateInputs))
}
}
import LibraryManagementCodec._
Tracked.inputChanged(cacheStoreFactory.make("inputs"))(doCachedResolve)
Expand Down Expand Up @@ -257,7 +265,7 @@ private[sbt] object LibraryManagement {
forceUpdatePeriod.value match {
case None => false
case Some(period) =>
val fullUpdateOutput = cacheDirectory / "out"
val fullUpdateOutput = cacheDirectory / "output"
val now = System.currentTimeMillis
val diff = now - fullUpdateOutput.lastModified()
val elapsedDuration = new scala.concurrent.duration.FiniteDuration(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
ThisBuild / useCoursier := false

name := "force-update-period"
scalaVersion := "2.12.18"
libraryDependencies += "log4j" % "log4j" % "1.2.16" % "compile"

autoScalaLibrary := false

crossPaths := false

TaskKey[Unit]("check-last-update-time") := (streams map { (s) =>
val fullUpdateOutput = s.cacheDirectory / "out"
val timeDiff = System.currentTimeMillis()-fullUpdateOutput.lastModified()
val exists = fullUpdateOutput.exists()
TaskKey[Unit]("check-last-update-time") := {
val s = streams.value
val updateOutput = target.value / "update" / updateCacheName.value / "output"
if (!updateOutput.exists()) {
sys.error("Update cache does not exist")
}
val timeDiff = System.currentTimeMillis() - updateOutput.lastModified()
s.log.info(s"Amount of time since last full update: $timeDiff")
if (exists && timeDiff > 5000) {
sys.error("Full update not performed")
if (timeDiff > 5000) {
sys.error("Update not performed")
}
}).value
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ $ absent target/resolution-cache
> compile
$ exists target/resolution-cache
> checkLastUpdateTime
$ sleep 10000
$ sleep 5000
> compile
# This is expected to fail
-> checkLastUpdateTime
> set forceUpdatePeriod := Some(new scala.concurrent.duration.FiniteDuration(5000, java.util.concurrent.TimeUnit.MILLISECONDS))
> compile
> checkLastUpdateTime
> checkLastUpdateTime

0 comments on commit 5deb6e6

Please sign in to comment.