Skip to content

Commit

Permalink
invalidate cache on -cli/-core/scalameta bump via scalafix-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
github-brice-jaglin committed May 16, 2020
1 parent d497fc2 commit 9828f94
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package scalafix.sbt

import java.net.URLClassLoader
import java.nio.file.Path

import com.geirsson.coursiersmall.Repository
Expand All @@ -12,6 +13,7 @@ import sbt.internal.sbtscalafix.Caching._
import scalafix.interfaces.ScalafixError
import scalafix.internal.sbt._

import scala.annotation.tailrec
import scala.util.Try
import scala.util.control.NoStackTrace

Expand Down Expand Up @@ -253,12 +255,27 @@ object ScalafixPlugin extends AutoPlugin {

implicit val stamper = new CacheKeysStamper {
override protected def stamp: Arg.CacheKey => Unit = {
case Arg.ToolClasspath(classLoader) =>
val urls = classLoader.getURLs
// to keep it simple, don't support caching when dealing with directories
if (urls.exists(_.toString.endsWith("/"))) throw StampingImpossible
// and assume JARs are stable (a new version would have a different URL)
write(classLoader.getURLs)
case Arg.ToolClasspath(toolClassLoader) =>
@tailrec def withParentRec(classLoader: URLClassLoader): Unit = {
val urls = classLoader.getURLs
// to keep it simple, don't support caching when dealing with directories
if (urls.exists(_.toString.endsWith("/")))
throw StampingImpossible
// and assume JARs are stable (a new version would have a different URL)
write(urls)
classLoader.getParent match {
case parent: URLClassLoader =>
// stamp also the JARs in the parent
withParentRec(parent)
case _: ScalafixInterfacesClassloader =>
// expected root of sbt-scalafix-managed tool classpaths
()
case _ =>
// the class loader layering is unexpected, don't cache to be safe
throw StampingImpossible
}
}
withParentRec(toolClassLoader)
case Arg.Rules(rules) =>
rules.foreach {
case source
Expand Down
28 changes: 28 additions & 0 deletions src/sbt-test/skip-windows/caching/test
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,31 @@ $ copy-file files/Valid.scala src/main/scala/Valid.scala
$ exec chmod 000 src/main/scala/Valid.scala
-> scalafix ProcedureSyntax
$ delete src/main/scala

# make sure cache is invalidated after changing scalafix version
> set scalafixConfig := None
$ mkdir src/main/scala
$ copy-file files/Valid.scala src/main/scala/Valid.scala
> scalafix --check ProcedureSyntax
> reload plugins
> 'set dependencyOverrides := Seq("ch.epfl.scala" % "scalafix-interfaces" % "0.9.14")' // anything else than above
> session save
> reload return
$ exec chmod 000 src/main/scala/Valid.scala
-> scalafix --check ProcedureSyntax
$ delete src/main/scala

# make sure cache is invalidated after changing scalafix version when no custom dependency is set
> set scalafixConfig := None
$ mkdir src/main/scala
$ copy-file files/Valid.scala src/main/scala/Valid.scala
> set scalafixDependencies in ThisBuild := Nil
> scalafix --check ProcedureSyntax
> reload plugins
> 'set dependencyOverrides := Seq("ch.epfl.scala" % "scalafix-interfaces" % "0.9.13")' // anything else than current
> session save
> reload return
> set scalafixDependencies in ThisBuild := Nil
$ exec chmod 000 src/main/scala/Valid.scala
-> scalafix --check ProcedureSyntax
$ delete src/main/scala

0 comments on commit 9828f94

Please sign in to comment.