Skip to content

Commit df9afba

Browse files
Use FileInfo.hash as caching style
* Switch from lastModified to hash caching style in order to produce a machine-independent file stamping once sbt/sbt#6298 is solved. * Add a system property "sbt-scalafix.uselastmodified" to force the utilisation of the lastModified caching style. (Used in scripted tests)
1 parent 96d729f commit df9afba

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ lib_managed/
1111
src_managed/
1212
project/boot/
1313
project/plugins/project/
14+
.bsp
1415

1516
# Scala-IDE specific
1617
.scala_dependencies

build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,6 @@ sbtPlugin := true
9292
scriptedBufferLog := false
9393
scriptedLaunchOpts ++= Seq(
9494
"-Xmx2048M",
95-
s"-Dplugin.version=${version.value}"
95+
s"-Dplugin.version=${version.value}",
96+
"-Dsbt-scalafix.uselastmodified=true" // the caching scripted relies on sbt-scalafix only checking file attributes, not content
9697
)

src/main/scala-sbt-0.13/sbt/internal/sbtscalafix/Caching.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import scala.util.DynamicVariable
1111
object Caching {
1212

1313
val lastModifiedStyle = FilesInfo.lastModified
14+
val hashStyle = FilesInfo.hash
1415

1516
trait CacheKeysStamper
1617
extends InputCache[Seq[Arg.CacheKey]]

src/main/scala-sbt-1.0/sbt/internal/sbtscalafix/Caching.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scala.util.DynamicVariable
99
object Caching {
1010

1111
val lastModifiedStyle = FileInfo.lastModified
12+
val hashStyle = FileInfo.hash
1213

1314
trait CacheKeysStamper
1415
extends JsonFormat[Seq[Arg.CacheKey]]

src/main/scala/scalafix/sbt/ScalafixPlugin.scala

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ object ScalafixPlugin extends AutoPlugin {
170170
Invisible
171171
)
172172

173+
private lazy val cachingStyle = {
174+
val useLastModifiedCachingStyle =
175+
sys.props.get("sbt-scalafix.uselastmodified") == Some("true")
176+
if (useLastModifiedCachingStyle) lastModifiedStyle
177+
else hashStyle
178+
}
179+
173180
override lazy val projectConfigurations: Seq[Configuration] =
174181
Seq(ScalafixConfig)
175182

@@ -485,7 +492,8 @@ object ScalafixPlugin extends AutoPlugin {
485492
case jar =>
486493
Seq(jar)
487494
}
488-
write(files.map(FileInfo.lastModified.apply))
495+
.filter(_.isFile)
496+
write(files.map(stampFile))
489497
write(customDependencies.map(_.toString))
490498
case Arg.Rules(rules) =>
491499
rules.foreach {
@@ -502,10 +510,10 @@ object ScalafixPlugin extends AutoPlugin {
502510
case Arg.Config(maybeFile) =>
503511
maybeFile match {
504512
case Some(path) =>
505-
write(FileInfo.lastModified(path.toFile))
513+
write(stampFile(path.toFile))
506514
case None =>
507515
val defaultConfigFile = file(".scalafix.conf")
508-
write(FileInfo.lastModified(defaultConfigFile))
516+
write(stampFile(defaultConfigFile))
509517
}
510518
case Arg.ParsedArgs(args) =>
511519
val cacheKeys = args.filter {
@@ -528,6 +536,13 @@ object ScalafixPlugin extends AutoPlugin {
528536
case Arg.NoCache =>
529537
throw StampingImpossible
530538
}
539+
def stampFile(file: File): Array[Byte] = {
540+
// ensure the file exists and is not a directory
541+
if (file.isFile)
542+
Hash(file)
543+
else
544+
Array.empty[Byte]
545+
}
531546
}
532547

533548
def diffWithPreviousRuns[T](f: (Boolean, Set[File]) => T): T = {
@@ -542,7 +557,7 @@ object ScalafixPlugin extends AutoPlugin {
542557
)(doForStaleTargets: Set[File] => T): T =
543558
Tracked.diffInputs(
544559
streams.cacheDirectory / "targets-by-rule" / rule,
545-
lastModifiedStyle
560+
cachingStyle
546561
)(targets) { changeReport: ChangeReport[File] =>
547562
doForStaleTargets(changeReport.modified -- changeReport.removed)
548563
}
@@ -576,7 +591,9 @@ object ScalafixPlugin extends AutoPlugin {
576591
// in sbt 1.x, this is not necessary as any exception thrown during stamping is already silently ignored,
577592
// but having this here helps keeping code as common as possible
578593
// https://github.com/sbt/util/blob/v1.0.0/util-tracking/src/main/scala/sbt/util/Tracked.scala#L180
579-
case _ @StampingImpossible => f(true, Set.empty)
594+
case _ @StampingImpossible =>
595+
println("StampingImpossible")
596+
f(true, Set.empty)
580597
}.get
581598
}
582599

0 commit comments

Comments
 (0)