Merge pull request #7538 from adpi2/sbt2-fix-force-update-period

[2.x] Fix `dependency-management/force-update-period`
This commit is contained in:
adpi2 2024-04-16 16:24:50 +02:00 committed by GitHub
commit 771b1d3e84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 34 deletions

View File

@ -3990,7 +3990,7 @@ object Classpaths {
fup 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)

View File

@ -115,13 +115,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
@ -132,15 +135,21 @@ 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 then 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.")
@ -148,8 +157,6 @@ private[sbt] object LibraryManagement {
resolvedAgain.toString.linesIterator.foreach(log.warn(_))
log.trace(t)
resolvedAgain
}
.apply(cachedResolve(updateInputs))
}
import LibraryManagementCodec._
Tracked.inputChanged(cacheStoreFactory.make("inputs"))(doCachedResolve)
@ -331,7 +338,7 @@ private[sbt] object LibraryManagement {
fup 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 FiniteDuration(

View File

@ -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()
s.log.info(s"Amount of time since last full update: $timeDiff")
if (exists && timeDiff > 5000) {
sys.error("Full update not performed")
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")
}
}).value
val timeDiff = System.currentTimeMillis() - updateOutput.lastModified()
s.log.info(s"Amount of time since last full update: $timeDiff")
if (timeDiff > 5000) {
sys.error("Update not performed")
}
}

View File

@ -1,11 +1,11 @@
$ absent target/resolution-cache
$ absent target/out/jvm/scala-2.12.18/force-update-period/resolution-cache
> compile
$ exists target/resolution-cache
$ exists target/out/jvm/scala-2.12.18/force-update-period/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