Skip to content

Commit

Permalink
Switch zinc from fork to original one.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Dec 20, 2021
1 parent 9d673f9 commit 8803d87
Show file tree
Hide file tree
Showing 43 changed files with 140 additions and 1,270 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[submodule "zinc"]
path = zinc
url = https://github.com/scalacenter/zinc.git
branch = loop
[submodule "nailgun"]
path = nailgun
url = https://github.com/scalacenter/nailgun.git
Expand Down
43 changes: 42 additions & 1 deletion backend/src/main/scala/bloop/BloopClassFileManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import java.io.IOException
import scala.util.Try
import scala.util.Failure
import scala.util.Success
import java.nio.file.CopyOption
import java.nio.file.StandardCopyOption

final class BloopClassFileManager(
backupDir0: Path,
inputs: CompileInputs,
outPaths: CompileOutPaths,
allGeneratedRelativeClassFilePaths: mutable.HashMap[String, File],
Expand All @@ -41,6 +44,12 @@ final class BloopClassFileManager(

// Supported compile products by the class file manager
private[this] val supportedCompileProducts = List(".sjsir", ".nir", ".tasty")
// Files backed up during compilation
private[this] val movedFiles = new mutable.HashMap[File, File]

private val backupDir = backupDir0.normalize
backupDir.toFile.delete()
Files.createDirectories(backupDir)

/**
* Returns the set of all invalidated class files.
Expand Down Expand Up @@ -129,9 +138,22 @@ final class BloopClassFileManager(
}
}
}

allInvalidatedClassFilesForProject.++=(classes)

// Idea taken from the default TransactionalClassFileManager in zinc
// TODO see if we can be more efficient
val toBeBackedUp =
allInvalidatedClassFilesForProject.filter(c =>
!movedFiles.contains(c) /*&& !generatedFiles(c) TODO */
)
for { c <- toBeBackedUp } if (c.exists)
movedFiles
.put(c, move(c))
.foreach(move)
allInvalidatedClassFilesForProject.foreach { f =>
if (f.exists()) f.delete()
}

val invalidatedExtraCompileProducts = classes.flatMap { classFile =>
val prefixClassName = classFile.getName().stripSuffix(".class")
supportedCompileProducts.flatMap { supportedProductSuffix =>
Expand Down Expand Up @@ -167,6 +189,7 @@ final class BloopClassFileManager(
allInvalidatedExtraCompileProducts.-=(productAssociatedToClassFile)
}
}

}

def complete(success: Boolean): Unit = {
Expand Down Expand Up @@ -200,6 +223,18 @@ final class BloopClassFileManager(
}
)
} else {
// Restore all files from backup
for {
(orig, tmp) <- movedFiles
} {
if (tmp.exists) {
if (!orig.getParentFile.exists) {
Files.createDirectory(orig.getParentFile.toPath())
}
Files.move(tmp.toPath(), orig.toPath())
}
}
backupDir.toFile().delete()
// Delete all compilation products generated in the new classes directory
val deleteNewDir = Task { BloopPaths.delete(AbsolutePath(newClassesDir)); () }.memoize
backgroundTasksForFailedCompilation.+=(
Expand Down Expand Up @@ -245,6 +280,12 @@ final class BloopClassFileManager(
)
}
}

private def move(c: File): File = {
val target = Files.createTempFile(backupDir, "bloop", ".class").toFile
Files.move(c.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING)
target
}
}

object BloopClassFileManager {
Expand Down
27 changes: 0 additions & 27 deletions backend/src/main/scala/bloop/CompileMode.scala

This file was deleted.

3 changes: 1 addition & 2 deletions backend/src/main/scala/bloop/CompileProducts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ case class CompileProducts(
resultForDependentCompilationsInSameRun: PreviousResult,
resultForFutureCompilationRuns: PreviousResult,
invalidatedCompileProducts: Set[File],
generatedRelativeClassFilePaths: Map[String, File],
definedMacroSymbols: Array[String]
generatedRelativeClassFilePaths: Map[String, File]
)
36 changes: 15 additions & 21 deletions backend/src/main/scala/bloop/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import bloop.tracing.BraveTracer
import bloop.logging.{ObservedLogger, Logger}
import bloop.reporter.{ProblemPerPhase, ZincReporter}
import bloop.util.{AnalysisUtils, UUIDUtil, CacheHashCode}
import bloop.CompileMode.Pipelined
import bloop.CompileMode.Sequential

import xsbti.compile._
import xsbti.T2
Expand All @@ -23,7 +21,6 @@ import sbt.util.InterfaceUtil
import sbt.internal.inc.Analysis
import sbt.internal.inc.bloop.BloopZincCompiler
import sbt.internal.inc.{FreshCompilerCache, InitialChanges, Locate}
import sbt.internal.inc.bloop.internal.StopPipelining
import sbt.internal.inc.{ConcreteAnalysisContents, FileAnalysisStore}

import scala.concurrent.Promise
Expand Down Expand Up @@ -55,7 +52,6 @@ case class CompileInputs(
previousCompilerResult: Compiler.Result,
reporter: ZincReporter,
logger: ObservedLogger[Logger],
mode: CompileMode,
dependentResults: Map[File, PreviousResult],
cancelPromise: Promise[Unit],
tracer: BraveTracer,
Expand Down Expand Up @@ -260,6 +256,7 @@ object Compiler {

def newFileManager: ClassFileManager = {
new BloopClassFileManager(
Files.createTempDirectory("bloop"),
compileInputs,
compileOut,
allGeneratedRelativeClassFilePaths,
Expand Down Expand Up @@ -300,7 +297,6 @@ object Compiler {
.withClasspath(classpath)
.withScalacOptions(optionsWithoutFatalWarnings)
.withJavacOptions(inputs.javacOptions)
.withClasspathOptions(inputs.classpathOptions)
.withOrder(inputs.compileOrder)
}

Expand Down Expand Up @@ -344,19 +340,12 @@ object Compiler {

import ch.epfl.scala.bsp
import scala.util.{Success, Failure}
val mode = compileInputs.mode
val reporter = compileInputs.reporter

def cancel(): Unit = {
// Complete all pending promises when compilation is cancelled
logger.debug(s"Cancelling compilation from ${readOnlyClassesDirPath} to ${newClassesDirPath}")
compileInputs.cancelPromise.trySuccess(())
mode match {
case _: Sequential => ()
case Pipelined(completeJava, finishedCompilation, _, _, _) =>
completeJava.trySuccess(())
finishedCompilation.tryFailure(CompileExceptions.FailedOrCancelledPromise)
}

// Always report the compilation of a project no matter if it's completed
reporter.reportCancelledCompilation()
Expand All @@ -379,7 +368,16 @@ object Compiler {
val uniqueInputs = compileInputs.uniqueInputs
reporter.reportStartCompilation(previousProblems)
BloopZincCompiler
.compile(inputs, mode, reporter, logger, uniqueInputs, newFileManager, cancelPromise, tracer)
.compile(
inputs,
reporter,
logger,
uniqueInputs,
newFileManager,
cancelPromise,
tracer,
classpathOptions
)
.materialize
.doOnCancel(Task(cancel()))
.map {
Expand Down Expand Up @@ -417,9 +415,9 @@ object Compiler {
val invalidatedExtraProducts =
allInvalidatedExtraCompileProducts.iterator.map(_.toPath).toSet
val invalidatedInThisProject = invalidatedClassFiles ++ invalidatedExtraProducts
val blacklist = invalidatedInThisProject ++ readOnlyCopyBlacklist.iterator
val denyList = invalidatedInThisProject ++ readOnlyCopyBlacklist.iterator
val config =
ParallelOps.CopyConfiguration(5, CopyMode.ReplaceIfMetadataMismatch, blacklist)
ParallelOps.CopyConfiguration(5, CopyMode.ReplaceIfMetadataMismatch, denyList)
val lastCopy = ParallelOps.copyDirectories(config)(
readOnlyClassesDir,
clientClassesDir.underlying,
Expand All @@ -444,7 +442,6 @@ object Compiler {
}

val isNoOp = previousAnalysis.contains(analysis)
val definedMacroSymbols = mode.oracle.collectDefinedMacroSymbols
if (isNoOp) {
// If no-op, return previous result with updated classpath hashes
val noOpPreviousResult = {
Expand All @@ -460,8 +457,7 @@ object Compiler {
noOpPreviousResult,
noOpPreviousResult,
Set(),
Map.empty,
definedMacroSymbols
Map.empty
)

val backgroundTasks = new CompileBackgroundTasks {
Expand Down Expand Up @@ -578,8 +574,7 @@ object Compiler {
resultForDependentCompilationsInSameRun,
resultForFutureCompilationRuns,
allInvalidated.toSet,
allGeneratedProducts,
definedMacroSymbols
allGeneratedProducts
)

Result.Success(
Expand All @@ -599,7 +594,6 @@ object Compiler {
reporter.reportEndCompilation()

cause match {
case f: StopPipelining => Result.Blocked(f.failedProjectNames)
case f: xsbti.CompileFailed =>
// We cannot guarantee reporter.problems == f.problems, so we aggregate them together
val reportedProblems = reporter.allProblemsPerPhase.toList
Expand Down
7 changes: 5 additions & 2 deletions backend/src/main/scala/bloop/CompilerCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import sbt.internal.util.LoggerWriter
import java.io.IOException
import scala.concurrent.ExecutionContext
import xsbti.compile.ScalaCompiler
import xsbti.compile.ClasspathOptions
import scala.collection.mutable.HashSet

final class CompilerCache(
componentProvider: ComponentProvider,
Expand Down Expand Up @@ -162,7 +164,7 @@ final class CompilerCache(
import sbt.util.InterfaceUtil
InterfaceUtil.toOption(topts.classFileManager()) match {
case None => logger.error("Missing class file manager for forked Java compiler"); false
case Some(classFileManager) =>
case Some(classFileManager: BloopClassFileManager) =>
import java.nio.file.Files
val newInvalidatedEntry = AbsolutePath(
Files.createTempDirectory("invalidated-forked-javac")
Expand Down Expand Up @@ -309,7 +311,8 @@ final class CompilerCache(
val invalidated = {
zincManager match {
case m: bloop.BloopClassFileManager => m.invalidatedClassFilesSet
case _ => zincManager.invalidatedClassFiles().toSet
case _ => new HashSet[File]()

}
}

Expand Down
70 changes: 0 additions & 70 deletions backend/src/main/scala/bloop/CompilerOracle.scala

This file was deleted.

3 changes: 1 addition & 2 deletions backend/src/main/scala/bloop/PartialCompileProducts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ import bloop.io.AbsolutePath
*/
case class PartialCompileProducts(
readOnlyClassesDir: AbsolutePath,
newClassesDir: AbsolutePath,
definedMacroSymbols: Array[String]
newClassesDir: AbsolutePath
)
Loading

0 comments on commit 8803d87

Please sign in to comment.