@@ -2,6 +2,7 @@ package dotty.tools.sbtplugin
2
2
3
3
import sbt ._
4
4
import sbt .Keys ._
5
+ import sbt .inc .{ ClassfileManager , IncOptions }
5
6
6
7
object DottyPlugin extends AutoPlugin {
7
8
object autoImport {
@@ -78,6 +79,37 @@ object DottyPlugin extends AutoPlugin {
78
79
}
79
80
}
80
81
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
+ }
81
113
82
114
override def projectSettings : Seq [Setting [_]] = {
83
115
Seq (
@@ -99,6 +131,13 @@ object DottyPlugin extends AutoPlugin {
99
131
scalaOrganization.value
100
132
},
101
133
134
+ incOptions in Compile := {
135
+ if (isDotty.value)
136
+ dottyPatchIncOptions((incOptions in Compile ).value)
137
+ else
138
+ (incOptions in Compile ).value
139
+ },
140
+
102
141
scalaBinaryVersion := {
103
142
if (isDotty.value)
104
143
scalaVersion.value.split(" \\ ." ).take(2 ).mkString(" ." ) // Not needed with sbt >= 0.13.16
0 commit comments