From 376b84fca92e0d435e65a4943db0be7c539cefa4 Mon Sep 17 00:00:00 2001 From: James Judd Date: Thu, 27 Jul 2023 21:30:56 -0600 Subject: [PATCH 1/2] Make Stamps deterministic --- .../workers/zinc/compile/AnalysisStore.scala | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/scala/higherkindness/rules_scala/workers/zinc/compile/AnalysisStore.scala b/src/main/scala/higherkindness/rules_scala/workers/zinc/compile/AnalysisStore.scala index b5fc447d..f20a6ad8 100644 --- a/src/main/scala/higherkindness/rules_scala/workers/zinc/compile/AnalysisStore.scala +++ b/src/main/scala/higherkindness/rules_scala/workers/zinc/compile/AnalysisStore.scala @@ -6,7 +6,9 @@ import java.io.{File, InputStream, OutputStream, OutputStreamWriter} import java.nio.charset.StandardCharsets import java.nio.file.{Files, NoSuchFileException, Path, Paths} import java.nio.file.attribute.FileTime +import java.util import java.util.concurrent.ConcurrentHashMap +import java.util.LinkedHashMap import java.util.zip.{GZIPInputStream, GZIPOutputStream} import java.util.Optional import sbt.internal.inc.binary.converters.{ProtobufReaders, ProtobufWriters} @@ -15,12 +17,10 @@ import sbt.internal.inc.{APIs, Analysis, PlainVirtualFile, PlainVirtualFileConve import sbt.internal.inc.Schema.{Access, AnalyzedClass, Annotation, AnnotationArgument, ClassDefinition, ClassDependencies, ClassLike, Companions, MethodParameter, NameHash, ParameterList, Path => SchemaPath, Qualifier, Type, TypeParameter, UsedName, UsedNames, Values} import sbt.internal.shaded.com.google.protobuf.GeneratedMessageV3 import sbt.io.IO -import scala.math.Ordering -import scala.collection.mutable.StringBuilder import scala.collection.immutable.TreeMap import xsbti.compile.analysis.{GenericMapper, ReadMapper, ReadWriteMappers, Stamp, WriteMapper} import xsbti.compile.{AnalysisContents, AnalysisStore, MiniSetup} -import scala.collection.JavaConverters._ +import scala.jdk.CollectionConverters._ import xsbti.VirtualFileRef import java.util.Objects @@ -97,7 +97,7 @@ class AnxAnalysisStore(files: AnalysisFiles, analyses: AnxAnalyses) extends Anal apis = analyses.apis().read(files.apis), relations = analyses.relations.read(files.relations), infos = analyses.sourceInfos.read(files.sourceInfos), - stamps = analyses.stamps.read(files.stamps) + stamps = analyses.stamps().read(files.stamps) ) val miniSetup = analyses.miniSetup().read(files.miniSetup) Optional.of(AnalysisContents.create(analysis, miniSetup)) @@ -115,7 +115,7 @@ class AnxAnalysisStore(files: AnalysisFiles, analyses: AnxAnalyses) extends Anal analyses.apis().write(files.apis, analysis.apis) analyses.relations.write(files.relations, analysis.relations) analyses.sourceInfos.write(files.sourceInfos, analysis.infos) - analyses.stamps.write(files.stamps, analysis.stamps) + analyses.stamps().write(files.stamps, analysis.stamps) val miniSetup = analysisContents.getMiniSetup analyses.miniSetup().write(files.miniSetup, miniSetup) } @@ -690,10 +690,38 @@ class AnxAnalyses(format: AnxAnalysisStore.Format) { (stream, value) => format.write(writer.toSourceInfos(value), stream) ) - def stamps = new Store[Stamps]( - stream => reader.fromStamps(format.read(Schema.Stamps.getDefaultInstance, stream)), - (stream, value) => format.write(writer.toStamps(value), stream) - ) + def stamps(): Store[Stamps] = { + new Store[Stamps]( + stream => reader.fromStamps(format.read(Schema.Stamps.getDefaultInstance, stream)), + (stream, value) => + format.write( + sortStamps(writer.toStamps(value)), + stream + ) + ) + } + + def sortStamps(stamps: Schema.Stamps): Schema.Stamps = { + val sortedProductStamps = sortStampTypeMap(stamps.getProductStampsMap).asJava + val sortedBinaryStamps = sortStampTypeMap(stamps.getBinaryStampsMap).asJava + val sortedSourceStamps = sortStampTypeMap(stamps.getSourceStampsMap).asJava + + Schema.Stamps + .newBuilder(stamps) + .clearProductStamps() + .putAllProductStamps(sortedProductStamps) + .clearBinaryStamps() + .putAllBinaryStamps(sortedBinaryStamps) + .clearSourceStamps() + .putAllSourceStamps(sortedSourceStamps) + .build() + } + + def sortStampTypeMap( + stampTypeMap: util.Map[String, Schema.Stamps.StampType] + ): TreeMap[String, Schema.Stamps.StampType] = { + TreeMap[String, Schema.Stamps.StampType]() ++ stampTypeMap.asScala + } } object AnxMapper { From a32c2ac4051c05b7089513162066df8cc2130675 Mon Sep 17 00:00:00 2001 From: James Judd Date: Thu, 28 Mar 2024 19:34:57 -0600 Subject: [PATCH 2/2] Use Java's TreeMap directly --- .../workers/zinc/compile/AnalysisStore.scala | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/scala/higherkindness/rules_scala/workers/zinc/compile/AnalysisStore.scala b/src/main/scala/higherkindness/rules_scala/workers/zinc/compile/AnalysisStore.scala index f20a6ad8..e7f10280 100644 --- a/src/main/scala/higherkindness/rules_scala/workers/zinc/compile/AnalysisStore.scala +++ b/src/main/scala/higherkindness/rules_scala/workers/zinc/compile/AnalysisStore.scala @@ -702,9 +702,9 @@ class AnxAnalyses(format: AnxAnalysisStore.Format) { } def sortStamps(stamps: Schema.Stamps): Schema.Stamps = { - val sortedProductStamps = sortStampTypeMap(stamps.getProductStampsMap).asJava - val sortedBinaryStamps = sortStampTypeMap(stamps.getBinaryStampsMap).asJava - val sortedSourceStamps = sortStampTypeMap(stamps.getSourceStampsMap).asJava + val sortedProductStamps = sortStampTypeMap(stamps.getProductStampsMap) + val sortedBinaryStamps = sortStampTypeMap(stamps.getBinaryStampsMap) + val sortedSourceStamps = sortStampTypeMap(stamps.getSourceStampsMap) Schema.Stamps .newBuilder(stamps) @@ -719,8 +719,8 @@ class AnxAnalyses(format: AnxAnalysisStore.Format) { def sortStampTypeMap( stampTypeMap: util.Map[String, Schema.Stamps.StampType] - ): TreeMap[String, Schema.Stamps.StampType] = { - TreeMap[String, Schema.Stamps.StampType]() ++ stampTypeMap.asScala + ): java.util.TreeMap[String, Schema.Stamps.StampType] = { + new java.util.TreeMap(stampTypeMap) } }