Skip to content

Commit

Permalink
Workaround libraryClassName non-determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
James Judd committed Jul 5, 2024
1 parent eb2991a commit fbe7b5c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sbt.internal.inc

/**
* Zinc's libraryClassName is a map from a library jar to class names. Problem is
* it's not deterministic. You only get a single class in the map and it's not always
* the same class. Until that bug in Zinc is fixed, we're setting the libraryClassName
* to an empty libraryClassName because empty is better than non-deterministic.
*
* This class is in the sbt.internal.inc package to get access to the MRelationsNameHashing
* class, which is private to that package. Super duper hacky, but I wasn't able to find a
* better way to change the libraryClassName for the relation.
*/
object FilteredRelations {
def getFilteredRelations(relations: Relations): Relations = {
val emptyRelations = Relations.empty

relations.asInstanceOf[MRelationsNameHashing].copy(
libraryClassName = emptyRelations.libraryClassName
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import net.sourceforge.argparse4j.impl.{Arguments => Arg}
import net.sourceforge.argparse4j.inf.Namespace
import sbt.internal.inc.classpath.ClassLoaderCache
import sbt.internal.inc.caching.ClasspathCache
import sbt.internal.inc.{Analysis, CompileFailed, IncrementalCompilerImpl, Locate, PlainVirtualFile, PlainVirtualFileConverter, ZincUtil}
import sbt.internal.inc.{Analysis, CompileFailed, FilteredRelations, IncrementalCompilerImpl, Locate, PlainVirtualFile, PlainVirtualFileConverter, Relations, ZincUtil}
import scala.jdk.CollectionConverters._
import scala.util.Try
import scala.util.control.NonFatal
Expand Down Expand Up @@ -279,12 +279,19 @@ object ZincRunner extends WorkerMain[Namespace] {
true,
usePersistence,
)
analysisStoreText.set(AnalysisContents.create(compileResult.analysis, compileResult.setup))
analysisStore.set(AnalysisContents.create(compileResult.analysis, compileResult.setup))

// Filter out libraryClassNames from the analysis because it is non-deterministic.
// Can stop doing this once the bug in Zinc is fixed. Check the comment on FilteredRelations
// for more info.
val resultAnalysis = {
val originalResultAnalysis = compileResult.analysis.asInstanceOf[Analysis]
originalResultAnalysis.copy(
relations = FilteredRelations.getFilteredRelations(originalResultAnalysis.relations)
)
}
analysisStoreText.set(AnalysisContents.create(resultAnalysis, compileResult.setup))
analysisStore.set(AnalysisContents.create(resultAnalysis, compileResult.setup))

// create used deps
val resultAnalysis = compileResult.analysis.asInstanceOf[Analysis]
val usedDeps =
// Filter out the Scala standard library as that should just always be
// implicitly available and not something we should be book keeping.
Expand Down

0 comments on commit fbe7b5c

Please sign in to comment.