Skip to content

Fix inline scripted #4857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object B {
transparent def getInline: Double =
A.get
transparent def getInline: String =
A.get.toString
}
2 changes: 1 addition & 1 deletion sbt-dotty/sbt-test/source-dependencies/inline/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $ copy-file changes/B1.scala B.scala
> compile

$ copy-file changes/B2.scala B.scala
# Compilation of C.scala should fail because B.getInline now has type Double instead of Int
# Compilation of C.scala should fail because B.getInline now has type String instead of Int
-> compile

$ copy-file changes/B1.scala B.scala
Expand Down
37 changes: 12 additions & 25 deletions sbt-dotty/src/dotty/tools/sbtplugin/DottyPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,20 @@ object DottyPlugin extends AutoPlugin {
* corresponding .tasty or .hasTasty file is also deleted.
*/
def dottyPatchIncOptions(incOptions: IncOptions): IncOptions = {
val inheritedNewClassFileManager = ClassFileManagerUtil.getDefaultClassFileManager(incOptions)
val tastyFileManager = new ClassFileManager {
private[this] val inherited = inheritedNewClassFileManager

def delete(classes: Array[File]): Unit = {
val tastySuffixes = List(".tasty", ".hasTasty")
inherited.delete(classes flatMap { classFile =>
if (classFile.getPath endsWith ".class") {
val prefix = classFile.getAbsolutePath.stripSuffix(".class")
tastySuffixes.map(suffix => new File(prefix + suffix)).filter(_.exists)
} else Nil
})
}
val tastyFileManager = new TastyFileManager

def generated(classes: Array[File]): Unit = {}
def complete(success: Boolean): Unit = {}
}
// Once sbt/zinc#562 is fixed, can be:
// val newExternalHooks =
// incOptions.externalHooks.withExternalClassFileManager(tastyFileManager)
val inheritedHooks = incOptions.externalHooks
val externalClassFileManager: Optional[ClassFileManager] = Option(inheritedHooks.getExternalClassFileManager.orElse(null)) match {
case Some(prevManager) =>
Optional.of(WrappedClassFileManager.of(prevManager, Optional.of(tastyFileManager)))
case None =>
Optional.of(tastyFileManager)
}

val hooks = new DefaultExternalHooks(inheritedHooks.getExternalLookup, externalClassFileManager)
incOptions.withExternalHooks(hooks)
val external = Optional.of(tastyFileManager: ClassFileManager)
val prevManager = inheritedHooks.getExternalClassFileManager
val fileManager: Optional[ClassFileManager] =
if (prevManager.isPresent) Optional.of(WrappedClassFileManager.of(prevManager.get, external))
else external
val newExternalHooks = new DefaultExternalHooks(inheritedHooks.getExternalLookup, fileManager)

incOptions.withExternalHooks(newExternalHooks)
}

override val globalSettings: Seq[Def.Setting[_]] = Seq(
Expand Down
62 changes: 62 additions & 0 deletions sbt-dotty/src/dotty/tools/sbtplugin/TastyFileManager.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dotty.tools.sbtplugin

import java.io.File
import java.nio.file.Files

import sbt.io.IO
import xsbti.compile.ClassFileManager

import scala.collection.mutable


/** A class file manger that prunes .tasty and .hasTasty as needed.
*
* This makes sure that, when a .class file must be deleted, the
* corresponding .tasty or .hasTasty file is also deleted.
*
* This code is adapted from Zinc `TransactionalClassFileManager`.
* We need to duplicate the logic since forwarding to the default class
* file manager doesn't work: we need to backup tasty files in a different
* temporary directory as class files.
*/
final class TastyFileManager extends ClassFileManager {
private[this] val tempDir = Files.createTempDirectory("backup").toFile

private[this] val generatedTastyFiles = new mutable.HashSet[File]
private[this] val movedTastyFiles = new mutable.HashMap[File, File]

override def delete(classes: Array[File]): Unit = {
val toBeBackedUp = tastyFiles(classes)
.filter(t => t.exists && !movedTastyFiles.contains(t) && !generatedTastyFiles(t))
for (c <- toBeBackedUp)
movedTastyFiles.put(c, move(c))
IO.deleteFilesEmptyDirs(classes)
}

override def generated(classes: Array[File]): Unit =
generatedTastyFiles ++= tastyFiles(classes)

override def complete(success: Boolean): Unit = {
if (!success) {
IO.deleteFilesEmptyDirs(generatedTastyFiles)
for ((orig, tmp) <- movedTastyFiles) IO.move(tmp, orig)
}
IO.delete(tempDir)
}

private def tastyFiles(classes: Array[File]): Array[File] = {
val tastySuffixes = List(".tasty", ".hasTasty")
classes.flatMap { classFile =>
if (classFile.getPath.endsWith(".class")) {
val prefix = classFile.getAbsolutePath.stripSuffix(".class")
tastySuffixes.map(suffix => new File(prefix + suffix)).filter(_.exists)
} else Nil
}
}

private def move(c: File): File = {
val target = File.createTempFile("sbt", ".tasty", tempDir)
IO.move(c, target)
target
}
}