Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ResourceExtractorDbIndex update
package com.mediarithmics.schemaextractor
import org.scalameta.FileLine
import scalafix.internal.patch.CrashingSemanticdbIndex
import scalafix.internal.reflect.ClasspathOps
import scalafix.internal.v0.{LegacyInMemorySemanticdbIndex, LegacySemanticdbIndex}
import scalafix.util.SemanticdbIndex
import scalafix.v0.{Database, Denotation, Document, ResolvedName, Symbol}
import scala.meta.inputs.Position
import scala.meta.internal.io.{FileIO, PathIO}
import scala.meta.internal.symtab.SymbolTable
import scala.meta.io.Classpath
import scala.meta.{Defn, Input, Source, Tree}
import scala.util.control.NonFatal
import scala.meta.internal.{semanticdb => s}
import scalafix.v1
import scalafix.v1.SemDoc
case class ResourceExtractorDbIndex(legacyDB: LegacyInMemorySemanticdbIndex)
extends CrashingSemanticdbIndex {
def index = legacyDB.index.asInstanceOf[Map[String, LegacySemanticdbIndex]]
private[this] var cache: Map[Symbol, Option[Defn]] = Map.empty
/** Get all documents in this index */
override def documents: Seq[Document] =
index.toSeq.flatMap {
case (uri, doc) =>
try {
doc.documents
} catch {
case NonFatal(_) =>
println(s"Error in index of $uri")
//there WILL be errors, so long as they don't impact the
// resources for which we want the schemas, we will ignore them
Nil
}
}
def document(s: Symbol): Option[LegacySemanticdbIndex] =
index.values.find(d =>
d.symbols.exists(rs => rs.symbol == s)
)
def definition(s: Symbol): Option[Defn] = {
def collectDefnsInDocument(): Option[(LegacySemanticdbIndex, List[Defn])] =
for {
d <- document(s)
} yield
(d, d.doc.tree.collect {
case defn: Defn.Class => defn
case defn: Defn.Trait => defn
})
if (cache.contains(s)) cache(s)
else {
collectDefnsInDocument() foreach { case (doc, defns) =>
val symDefs =
defns.map {
case defn: Defn.Class =>
doc.symbol(defn.name).map(_ -> Some(defn))
case defn: Defn.Trait =>
doc.symbol(defn.name).map(_ -> Some(defn))
}
val newCache = symDefs.foldLeft(cache) {
case (c, sd) =>
if (sd.isEmpty) c
else c + sd.get
}
cache = newCache
}
cache.getOrElse(s, None)
}
}
def findSymbol(doc: LegacySemanticdbIndex, tree: Tree): Either[Error, Symbol] =
doc.symbol(tree).toRight(error(s"No symbol found for $tree"))
def findDefinition(s: Symbol): Either[Error, Defn] =
definition(s).toRight(error(s"No definition found for $s"))
def error(msg: String, cause: Throwable = null)
(implicit fileLine: FileLine): Error =
new Error(s"[$fileLine] $msg", cause)
// def namedDefinitions : Seq[Either[Error, (Symbol, Defn)]] =
def namedDefinitions: List[Either[Error, (Symbol, Defn)]] =
index.values.toList.flatMap { doc =>
doc.doc.tree.collect {
case defn: Defn.Class =>
findSymbol(doc, defn.name).map { s =>
cache += s -> Some(defn)
s -> defn
}
case defn: Defn.Trait =>
findSymbol(doc, defn.name).map { s =>
cache += s -> Some(defn)
s -> defn
}
}
}
override def database: Database = legacyDB.database
override def names: Seq[ResolvedName] = legacyDB.names
override def symbol(position: Position): Option[Symbol] = legacyDB.symbol(position)
override def symbol(tree: Tree): Option[Symbol] = legacyDB.symbol(tree)
def findSymbol(tree: Tree): Either[Error, Symbol] =
symbol(tree).toRight(new Error(s"No symbol found for $tree"))
override def denotation(symbol: Symbol): Option[Denotation] = legacyDB.denotation(symbol)
override def denotation(tree: Tree): Option[Denotation] = legacyDB.denotation(tree)
}
object ResourceExtractorDbIndex {
def load(classpath: Classpath): ResourceExtractorDbIndex = {
val symtab = ClasspathOps.newSymbolTable(classpath)
load(classpath, symtab)
}
def load(classpath: Classpath, symtab: SymbolTable): ResourceExtractorDbIndex = {
val buf = Map.newBuilder[String, SemanticdbIndex]
classpath.entries.foreach { entry =>
if (entry.isDirectory) {
val files = FileIO.listAllFilesRecursively(
entry.resolve("META-INF").resolve("semanticdb"))
files.foreach { file =>
if (PathIO.extension(file.toNIO) == "semanticdb") {
val textDocument = s.TextDocuments.parseFrom(file.readAllBytes)
textDocument.documents.foreach { textDocument =>
val input = Input.VirtualFile(textDocument.uri, textDocument.text)
val tree = input.parse[Source].get
val syndoc = v1.SyntacticDocument.fromTree(tree)
val semdoc = SemDoc(syndoc, textDocument, symtab)
buf += (textDocument.uri -> new LegacySemanticdbIndex(semdoc))
}
}
}
}
}
new ResourceExtractorDbIndex(LegacyInMemorySemanticdbIndex(buf.result(), symtab))
}
}
1) T1(com.mediarithmics.schemaextractor.SuperTypesTest)
scala.meta.internal.classpath.MissingSymbolException: missing symbol: scala
at scala.meta.internal.scalacp.SymbolOps$XtensionSymbolSSpec.descriptor(SymbolOps.scala:119)
at scala.meta.internal.scalacp.SymbolOps$XtensionSymbolSSymbol.uncached$1(SymbolOps.scala:18)
at scala.meta.internal.scalacp.SymbolOps$XtensionSymbolSSymbol.toSemantic(SymbolOps.scala:25)
at scala.meta.internal.scalacp.SymbolOps$XtensionSymbol.ssym(SymbolOps.scala:185)
at scala.meta.internal.scalacp.SymbolOps$XtensionSymbolSSpec.owner(SymbolOps.scala:58)
at scala.meta.internal.scalacp.SymbolOps$XtensionSymbolSSymbol.uncached$1(SymbolOps.scala:18)
at scala.meta.internal.scalacp.SymbolOps$XtensionSymbolSSymbol.toSemantic(SymbolOps.scala:25)
at scala.meta.internal.scalacp.SymbolOps$XtensionSymbol.ssym(SymbolOps.scala:185)
at scala.meta.internal.scalacp.TypeOps$XtensionTypeSType.loop$1(TypeOps.scala:22)
at scala.meta.internal.scalacp.TypeOps$XtensionTypeSType.toSemanticTpe(TypeOps.scala:88)
at scala.meta.internal.scalacp.TypeOps$XtensionTypeSType.$anonfun$toSemanticSig$1(TypeOps.scala:95)
at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:237)
at scala.collection.immutable.List.foreach(List.scala:392)
at scala.collection.TraversableLike.map(TraversableLike.scala:237)
at scala.collection.TraversableLike.map$(TraversableLike.scala:230)
at scala.collection.immutable.List.map(List.scala:298)
at scala.meta.internal.scalacp.TypeOps$XtensionTypeSType.loop$2(TypeOps.scala:95)
at scala.meta.internal.scalacp.TypeOps$XtensionTypeSType.toSemanticSig(TypeOps.scala:133)
at scala.meta.internal.scalacp.SymbolInformationOps$XtensionGSymbolSSymbolInformation.sig(SymbolInformationOps.scala:165)
at scala.meta.internal.scalacp.SymbolInformationOps$XtensionGSymbolSSymbolInformation.toSymbolInformation(SymbolInformationOps.scala:252)
at scala.meta.internal.scalacp.Scalacp.sinfos(Scalacp.scala:32)
at scala.meta.internal.scalacp.Scalacp.$anonfun$parse$1(Scalacp.scala:20)
at scala.collection.immutable.List.flatMap(List.scala:338)
at scala.meta.internal.scalacp.Scalacp.parse(Scalacp.scala:19)
at scala.meta.internal.scalacp.Scalacp$.parse(Scalacp.scala:50)
at scala.meta.internal.metacp.ClassfileInfos$.fromClassNode(ClassfileInfos.scala:46)
at scala.meta.internal.symtab.GlobalSymbolTable.loadSymbol(GlobalSymbolTable.scala:40)
at scala.meta.internal.symtab.GlobalSymbolTable.info(GlobalSymbolTable.scala:69)
at scalafix.internal.v1.InternalSemanticDoc.info(InternalSemanticDoc.scala:56)
at scalafix.internal.v0.LegacyCodePrinter.$anonfun$pprint$2(LegacyCodePrinter.scala:75)
at scalafix.internal.v0.LegacyCodePrinter.$anonfun$pprint$2$adapted(LegacyCodePrinter.scala:71)
at scalafix.internal.v0.LegacyCodePrinter.$anonfun$mkString$1(LegacyCodePrinter.scala:46)
at scalafix.internal.v0.LegacyCodePrinter.$anonfun$mkString$1$adapted(LegacyCodePrinter.scala:40)
at scala.collection.immutable.List.foreach(List.scala:392)
at scalafix.internal.v0.LegacyCodePrinter.mkString(LegacyCodePrinter.scala:40)
at scalafix.internal.v0.LegacyCodePrinter.$anonfun$pprint$1(LegacyCodePrinter.scala:71)
at scalafix.internal.v0.LegacyCodePrinter.$anonfun$pprint$1$adapted(LegacyCodePrinter.scala:70)
at scala.collection.Iterator.foreach(Iterator.scala:941)
at scala.collection.Iterator.foreach$(Iterator.scala:941)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
at scala.collection.IterableLike.foreach(IterableLike.scala:74)
at scala.collection.IterableLike.foreach$(IterableLike.scala:73)
at scala.collection.AbstractIterable.foreach(Iterable.scala:56)
at scalafix.internal.v0.LegacyCodePrinter.pprint(LegacyCodePrinter.scala:70)
at scalafix.internal.v0.LegacyCodePrinter.convertDenotation(LegacyCodePrinter.scala:224)
at scalafix.internal.v0.LegacySemanticdbIndex$.infoToDenotation(LegacySemanticdbIndex.scala:147)
at scalafix.internal.v0.LegacySemanticdbIndex.$anonfun$symbols$1(LegacySemanticdbIndex.scala:46)
at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:237)
at scala.collection.Iterator.foreach(Iterator.scala:941)
at scala.collection.Iterator.foreach$(Iterator.scala:941)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
at scala.collection.IterableLike.foreach(IterableLike.scala:74)
at scala.collection.IterableLike.foreach$(IterableLike.scala:73)
at scala.collection.AbstractIterable.foreach(Iterable.scala:56)
at scala.collection.TraversableLike.map(TraversableLike.scala:237)
at scala.collection.TraversableLike.map$(TraversableLike.scala:230)
at scala.collection.AbstractTraversable.map(Traversable.scala:108)
at scalafix.internal.v0.LegacySemanticdbIndex.symbols(LegacySemanticdbIndex.scala:43)
at com.mediarithmics.schemaextractor.ResourceExtractorDbIndex.$anonfun$document$1(ResourceExtractorDbIndex.scala:51)
at com.mediarithmics.schemaextractor.ResourceExtractorDbIndex.$anonfun$document$1$adapted(ResourceExtractorDbIndex.scala:50)
at scala.collection.Iterator.find(Iterator.scala:993)
at scala.collection.Iterator.find$(Iterator.scala:990)
at scala.collection.AbstractIterator.find(Iterator.scala:1429)
at scala.collection.IterableLike.find(IterableLike.scala:81)
at scala.collection.IterableLike.find$(IterableLike.scala:80)
at scala.collection.AbstractIterable.find(Iterable.scala:56)
at com.mediarithmics.schemaextractor.ResourceExtractorDbIndex.document(ResourceExtractorDbIndex.scala:50)
at com.mediarithmics.schemaextractor.ResourceExtractorDbIndex.collectDefnsInDocument$1(ResourceExtractorDbIndex.scala:59)
at com.mediarithmics.schemaextractor.ResourceExtractorDbIndex.definition(ResourceExtractorDbIndex.scala:68)
at com.mediarithmics.schemaextractor.PolymorphismInfosExtractor.$anonfun$superTypes$3(PolymorphismInfosExtractor.scala:161)
at com.mediarithmics.functional.data.recursion.package$.hyloM(package.scala:41)
at com.mediarithmics.schemaextractor.PolymorphismInfosExtractor.superTypes(PolymorphismInfosExtractor.scala:164)
at com.mediarithmics.schemaextractor.SuperTypesTest.$anonfun$new$1(SuperTypesTest.scala:22)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:186)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
at org.scalatest.FunSuite.withFixture(FunSuite.scala:1560)
at org.scalatest.FunSuiteLike.invokeWithFixture$1(FunSuiteLike.scala:184)
at org.scalatest.FunSuiteLike.$anonfun$runTest$1(FunSuiteLike.scala:196)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
at org.scalatest.FunSuiteLike.runTest(FunSuiteLike.scala:196)
at org.scalatest.FunSuiteLike.runTest$(FunSuiteLike.scala:178)
at org.scalatest.FunSuite.runTest(FunSuite.scala:1560)
at org.scalatest.FunSuiteLike.$anonfun$runTests$1(FunSuiteLike.scala:229)
at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:396)
at scala.collection.immutable.List.foreach(List.scala:392)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:379)
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
at org.scalatest.FunSuiteLike.runTests(FunSuiteLike.scala:229)
at org.scalatest.FunSuiteLike.runTests$(FunSuiteLike.scala:228)
at org.scalatest.FunSuite.runTests(FunSuite.scala:1560)
at org.scalatest.Suite.run(Suite.scala:1147)
at org.scalatest.Suite.run$(Suite.scala:1129)
at org.scalatest.FunSuite.org$scalatest$FunSuiteLike$$super$run(FunSuite.scala:1560)
at org.scalatest.FunSuiteLike.$anonfun$run$1(FunSuiteLike.scala:233)
at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
at org.scalatest.FunSuiteLike.run(FunSuiteLike.scala:233)
at org.scalatest.FunSuiteLike.run$(FunSuiteLike.scala:232)
at org.scalatest.FunSuite.run(FunSuite.scala:1560)
at org.scalatest.junit.JUnitRunner.run(JUnitRunner.scala:99)
at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.CompositeRequestRunner.runChild(CompositeRequestRunner.java:66)
at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConcurrentCompositeRequestRunner$1$1.run(ConcurrentCompositeRequestRunner.java:37)
at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConcurrentRunnerScheduler.finished(ConcurrentRunnerScheduler.java:89)
at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConcurrentCompositeRequestRunner$1.evaluate(ConcurrentCompositeRequestRunner.java:46)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConsoleRunnerImpl.runLegacy(ConsoleRunnerImpl.java:539)
at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConsoleRunnerImpl.run(ConsoleRunnerImpl.java:455)
at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConsoleRunnerImpl.main(ConsoleRunnerImpl.java:843)
at org.pantsbuild.tools.junit.ConsoleRunner.main(ConsoleRunner.java:12)
@jamesanto

This comment has been minimized.

Copy link

@jamesanto jamesanto commented May 20, 2020

Hi, did you manage to fix this? I am running into the same issue when I tried to upgrade scalafix-noinfer to scala 2.13

@lgirault

This comment has been minimized.

Copy link
Owner Author

@lgirault lgirault commented May 23, 2020

Hey ! Sorry for the delay. Not really sure to remember the exact problem. What is certain is that I've rewritten a custom version of the LegacySemanticdbIndex and I had to fiddle with the classpath.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.