Skip to content
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

Update Zinc to 1.6.0 and drop the fork #1662

Merged
merged 3 commits into from
Feb 21, 2022
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
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
75 changes: 60 additions & 15 deletions backend/src/main/scala/bloop/BloopClassFileManager.scala
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package bloop

import bloop.io.{Paths => BloopPaths}
import bloop.io.AbsolutePath
import bloop.tracing.BraveTracer
import bloop.io.ParallelOps
import bloop.io.ParallelOps.CopyMode
import bloop.io.{Paths => BloopPaths}
import bloop.reporter.Reporter
import bloop.tracing.BraveTracer
import monix.eval.Task
import xsbti.compile.ClassFileManager
import xsbti.compile.PreviousResult

import java.io.File
import java.io.IOException
import java.nio.file.CopyOption
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

import java.nio.file.StandardCopyOption
import scala.collection.mutable

import xsbti.compile.ClassFileManager
import monix.eval.Task
import bloop.reporter.Reporter
import xsbti.compile.PreviousResult
import java.nio.file.Files
import java.io.IOException
import scala.util.Try
import scala.util.Failure
import scala.util.Success
import scala.util.Try

final class BloopClassFileManager(
backupDir0: Path,
inputs: CompileInputs,
outPaths: CompileOutPaths,
allGeneratedRelativeClassFilePaths: mutable.HashMap[String, File],
Expand All @@ -38,9 +39,16 @@ final class BloopClassFileManager(
private[this] val newClassesDirPath = newClassesDir.toString
private[this] val dependentClassFilesLinks = new mutable.HashSet[Path]()
private[this] val weakClassFileInvalidations = new mutable.HashSet[Path]()
private[this] val generatedFiles = new mutable.HashSet[File]

// 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 @@ -114,22 +122,21 @@ final class BloopClassFileManager(
inputs.dependentResults
) match {
case None => ()
case Some(foundClassFile) =>
case Some(foundClassFilePath) =>
weakClassFileInvalidations.+=(classFilePath)
val newLink = newClassesDir.resolve(relativeFilePath)
BloopClassFileManager.link(newLink, foundClassFile.toPath) match {
BloopClassFileManager.link(newLink, foundClassFilePath) match {
case Success(_) => dependentClassFilesLinks.+=(newLink)
case Failure(exception) =>
inputs.logger.error(
s"Failed to create link for invalidated file $foundClassFile: ${exception.getMessage()}"
s"Failed to create link for invalidated file $foundClassFilePath: ${exception.getMessage()}"
)
inputs.logger.trace(exception)
}
()
}
}
}

allInvalidatedClassFilesForProject.++=(classes)

val invalidatedExtraCompileProducts = classes.flatMap { classFile =>
Expand All @@ -142,12 +149,28 @@ final class BloopClassFileManager(
}
}

// Idea taken from the default TransactionalClassFileManager in zinc
// https://github.com/sbt/zinc/blob/c18637c1b30f8ab7d1f702bb98301689ec75854b/internal/zinc-core/src/main/scala/sbt/internal/inc/ClassFileManager.scala#L183
val toBeBackedUp = (classes ++ invalidatedExtraCompileProducts).filter(c =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, Bloop was using forked Zinc, that allowed to invalidate all symbols from invalidated files from the compiler. That API is not available for the mainline Zinc though, so we need to move them explicitely.

!movedFiles.contains(c) && !generatedFiles(c)
)
for {
c <- toBeBackedUp
if c.exists()
} movedFiles.put(c, move(c)).foreach(move)

for {
f <- classes
if f.exists()
} f.delete()

allInvalidatedExtraCompileProducts.++=(invalidatedExtraCompileProducts)
}

def generated(generatedClassFiles: Array[File]): Unit = {
memoizedInvalidatedClassFiles = null
generatedClassFiles.foreach { generatedClassFile =>
generatedFiles += generatedClassFile
val newClassFile = generatedClassFile.getAbsolutePath
val relativeClassFilePath = newClassFile.replace(newClassesDirPath, "")
allGeneratedRelativeClassFilePaths.put(relativeClassFilePath, generatedClassFile)
Expand All @@ -167,6 +190,7 @@ final class BloopClassFileManager(
allInvalidatedExtraCompileProducts.-=(productAssociatedToClassFile)
}
}

}

def complete(success: Boolean): Unit = {
Expand Down Expand Up @@ -200,6 +224,21 @@ final class BloopClassFileManager(
}
)
} else {
/* Restore all files from backuped last successful compilation to make sure
* that they are still available.
*/
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 +284,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
62 changes: 36 additions & 26 deletions backend/src/main/scala/bloop/BloopClasspathEntryLookup.scala
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
package bloop

import java.io.File
import java.{util => ju}

import sbt.util.InterfaceUtil
import bloop.util.AnalysisUtils
import sbt.internal.inc.PlainVirtualFileConverter
import sbt.internal.inc.bloop.internal.BloopNameHashing
import sbt.internal.inc.bloop.internal.BloopStamps
import sbt.internal.inc.classpath.ClasspathUtil
import sbt.internal.inc.classpath.ClasspathUtilities

import xsbti.compile.PreviousResult
import xsbti.compile.PerClasspathEntryLookup
import sbt.util.InterfaceUtil
import xsbti.FileConverter
import xsbti.VirtualFile
import xsbti.compile.CompileAnalysis
import xsbti.compile.DefinesClass
import java.util.zip.ZipFile
import java.util.zip.ZipException
import java.util.concurrent.ConcurrentHashMap
import xsbti.compile.FileHash
import sbt.internal.inc.bloop.internal.BloopNameHashing
import sbt.internal.inc.bloop.internal.BloopStamps
import xsbti.compile.PerClasspathEntryLookup
import xsbti.compile.PreviousResult

import java.io.File
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap
import java.util.zip.ZipException
import java.util.zip.ZipFile
import java.{util => ju}

final class BloopClasspathEntryLookup(
results: Map[File, PreviousResult],
classpathHashes: Vector[FileHash]
classpathHashes: Vector[FileHash],
converter: FileConverter
) extends PerClasspathEntryLookup {
override def analysis(classpathEntry: File): ju.Optional[CompileAnalysis] = {
InterfaceUtil.toOptional(results.get(classpathEntry)).flatMap(_.analysis())
override def analysis(classpathEntry: VirtualFile): ju.Optional[CompileAnalysis] = {
val file = converter.toPath(classpathEntry).toFile()
InterfaceUtil.toOptional(results.get(file)).flatMap(_.analysis())
}

override def definesClass(entry: File): DefinesClass = {
if (!entry.exists) FalseDefinesClass
override def definesClass(entry: VirtualFile): DefinesClass = {
val path = converter.toPath(entry)
val file = path.toFile()
if (!file.exists) FalseDefinesClass
else {
classpathHashes.find(fh => fh.file() == entry) match {
classpathHashes.find(fh => fh.file() == file) match {
case None => FalseDefinesClass
case Some(entryHash) =>
def computeDefinesClassForJar = {
if (!ClasspathUtilities.isArchive(entry, contentFallback = true)) FalseDefinesClass
else new JarDefinesClass(entry)
if (!ClasspathUtil.isArchive(path, contentFallback = true)) FalseDefinesClass
else new JarDefinesClass(file)
}

if (BloopStamps.isDirectoryHash(entryHash)) new DirectoryDefinesClass(entry)
if (BloopStamps.isDirectoryHash(entryHash)) new DirectoryDefinesClass(file)
else {
val (_, cachedDefinesClass) = BloopClasspathEntryLookup.definedClasses.compute(
entry,
file,
(entry, definesClass) => {
definesClass match {
case null =>
Expand Down Expand Up @@ -116,14 +125,15 @@ object BloopClasspathEntryLookup {
def definedClassFileInDependencies(
relativeClassFile: String,
results: Map[File, PreviousResult]
): Option[File] = {
def findClassFile(t: (File, PreviousResult)): Option[File] = {
): Option[Path] = {
def findClassFile(t: (File, PreviousResult)): Option[Path] = {
val (classesDir, result) = t
val targetClassFile = new File(classesDir, relativeClassFile)
val targetFile = classesDir.toPath().resolve(relativeClassFile)
val targetClassFile = PlainVirtualFileConverter.converter.toVirtualFile(targetFile)
InterfaceUtil.toOption(result.analysis()).flatMap { analysis0 =>
val analysis = analysis0.asInstanceOf[sbt.internal.inc.Analysis]
val definedClass = analysis.relations.allProducts.contains(targetClassFile)
if (definedClass) Some(targetClassFile) else None
if (definedClass) Some(targetFile) else None
}
}

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]
)
Loading