Skip to content

Commit 452bc5e

Browse files
committed
sbt-dotty: Delete .tasty files
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
1 parent b8e56b7 commit 452bc5e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

sbt-dotty/src/dotty/tools/sbtplugin/DottyPlugin.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty.tools.sbtplugin
22

33
import sbt._
44
import sbt.Keys._
5+
import sbt.inc.{ ClassfileManager, IncOptions }
56

67
object DottyPlugin extends AutoPlugin {
78
object autoImport {
@@ -78,6 +79,37 @@ object DottyPlugin extends AutoPlugin {
7879
}
7980
}
8081

82+
/** Patches the IncOptions so that .tasty files are pruned as needed.
83+
*
84+
* This code is adapted from `scalaJSPatchIncOptions` in Scala.js, which needs
85+
* to do the exact same thing but for classfiles.
86+
*
87+
* This complicated logic patches the ClassfileManager factory of the given
88+
* IncOptions with one that is aware of .tasty files emitted by the Dotty
89+
* compiler. This makes sure that, when a .class file must be deleted, the
90+
* corresponding .tasty file is also deleted.
91+
*/
92+
def dottyPatchIncOptions(incOptions: IncOptions): IncOptions = {
93+
val inheritedNewClassfileManager = incOptions.newClassfileManager
94+
val newClassfileManager = () => new ClassfileManager {
95+
private[this] val inherited = inheritedNewClassfileManager()
96+
97+
def delete(classes: Iterable[File]): Unit = {
98+
inherited.delete(classes flatMap { classFile =>
99+
val dottyFiles = if (classFile.getPath endsWith ".class") {
100+
val f = new File(classFile.getAbsolutePath.stripSuffix(".class") + ".tasty")
101+
if (f.exists) List(f)
102+
else Nil
103+
} else Nil
104+
classFile :: dottyFiles
105+
})
106+
}
107+
108+
def generated(classes: Iterable[File]): Unit = inherited.generated(classes)
109+
def complete(success: Boolean): Unit = inherited.complete(success)
110+
}
111+
incOptions.withNewClassfileManager(newClassfileManager)
112+
}
81113

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

134+
incOptions in Compile := {
135+
if (isDotty.value)
136+
dottyPatchIncOptions((incOptions in Compile).value)
137+
else
138+
(incOptions in Compile).value
139+
},
140+
102141
scalaBinaryVersion := {
103142
if (isDotty.value)
104143
scalaVersion.value.split("\\.").take(2).mkString(".") // Not needed with sbt >= 0.13.16

0 commit comments

Comments
 (0)