Skip to content

Commit

Permalink
sbt-dotty: Delete .tasty files
Browse files Browse the repository at this point in the history
Since 1293e2f we emit empty .tasty
files by default (and already before that, non-empty ones if
-YemitTasty is used). But these files were never deleted by sbt when the
corresponding classfiles got deleted. This commit fixes that by stealing
some code from Scala.js
  • Loading branch information
smarter committed May 26, 2017
1 parent b8e56b7 commit 452bc5e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions sbt-dotty/src/dotty/tools/sbtplugin/DottyPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dotty.tools.sbtplugin

import sbt._
import sbt.Keys._
import sbt.inc.{ ClassfileManager, IncOptions }

object DottyPlugin extends AutoPlugin {
object autoImport {
Expand Down Expand Up @@ -78,6 +79,37 @@ object DottyPlugin extends AutoPlugin {
}
}

/** Patches the IncOptions so that .tasty files are pruned as needed.
*
* This code is adapted from `scalaJSPatchIncOptions` in Scala.js, which needs
* to do the exact same thing but for classfiles.
*
* This complicated logic patches the ClassfileManager factory of the given
* IncOptions with one that is aware of .tasty files emitted by the Dotty
* compiler. This makes sure that, when a .class file must be deleted, the
* corresponding .tasty file is also deleted.
*/
def dottyPatchIncOptions(incOptions: IncOptions): IncOptions = {
val inheritedNewClassfileManager = incOptions.newClassfileManager
val newClassfileManager = () => new ClassfileManager {
private[this] val inherited = inheritedNewClassfileManager()

def delete(classes: Iterable[File]): Unit = {
inherited.delete(classes flatMap { classFile =>
val dottyFiles = if (classFile.getPath endsWith ".class") {
val f = new File(classFile.getAbsolutePath.stripSuffix(".class") + ".tasty")
if (f.exists) List(f)
else Nil
} else Nil
classFile :: dottyFiles
})
}

def generated(classes: Iterable[File]): Unit = inherited.generated(classes)
def complete(success: Boolean): Unit = inherited.complete(success)
}
incOptions.withNewClassfileManager(newClassfileManager)
}

override def projectSettings: Seq[Setting[_]] = {
Seq(
Expand All @@ -99,6 +131,13 @@ object DottyPlugin extends AutoPlugin {
scalaOrganization.value
},

incOptions in Compile := {
if (isDotty.value)
dottyPatchIncOptions((incOptions in Compile).value)
else
(incOptions in Compile).value
},

scalaBinaryVersion := {
if (isDotty.value)
scalaVersion.value.split("\\.").take(2).mkString(".") // Not needed with sbt >= 0.13.16
Expand Down

0 comments on commit 452bc5e

Please sign in to comment.