google / ksp

Kotlin Symbol Processing API
https://github.com/google/ksp
Apache License 2.0
2.84k stars 266 forks source link

e: java.lang.AssertionError: Rewrite at slice LEXICAL_SCOPE key: ANNOTATION_ENTRY #1716

Open jschneider opened 8 months ago

jschneider commented 8 months ago

Hey guys, this exception still happens for me with:

last version without this bug:

This is the same(?) issue: https://github.com/google/ksp/issues/1591

But it is not fixed for me.

The exception can be provoked by marking methods with @JsExport

Full Stacktrace:

e: java.lang.AssertionError: Rewrite at slice LEXICAL_SCOPE key: ANNOTATION_ENTRY old value: org.jetbrains.kotlin.resolve.scopes.LexicalScope$Base@5d232184@1562583428 new value: org.jetbrains.kotlin.resolve.scopes.LexicalScope$Base@165ecc78@375311480
<File name: MeisterCharts.kt, Physical: true>
/**
 * Creates the [BarChartStacked] as a child of [element].
 * @see [createBarChartStacked]h
 */
<ELEMENT>@JsExport</ELEMENT>
@JsName("createBarChartStackedFromElement")
@Suppress("unused")
fun createBarChartStackedFromElement(
  element: Element,
): BarChartStacked {
  val barChartStacked = createBarChartStacked()
  element.appendChild(barChartStacked.holder)
  return barChartStacked
}
        at org.jetbrains.kotlin.com.intellij.openapi.diagnostic.DefaultLogger.error(DefaultLogger.java:54)
        at org.jetbrains.kotlin.com.intellij.openapi.diagnostic.Logger.error(Logger.java:182)
        at org.jetbrains.kotlin.util.slicedMap.Slices.logErrorAboutRewritingNonEqualObjects(Slices.java:158)
        at org.jetbrains.kotlin.util.slicedMap.Slices.access$000(Slices.java:30)
        at org.jetbrains.kotlin.util.slicedMap.Slices$1.processRewrite(Slices.java:42)
        at org.jetbrains.kotlin.util.slicedMap.SlicedMapImpl.put(SlicedMapImpl.java:65)
        at org.jetbrains.kotlin.resolve.BindingTraceContext.record(BindingTraceContext.java:150)
        at org.jetbrains.kotlin.storage.LockBasedLazyResolveStorageManager$LockProtectedTrace$record$1.invoke(LockBasedLazyResolveStorageManager.kt:70)
        at org.jetbrains.kotlin.storage.LockBasedLazyResolveStorageManager$LockProtectedTrace$record$1.invoke(LockBasedLazyResolveStorageManager.kt:70)
        at org.jetbrains.kotlin.storage.LockBasedStorageManager.compute(LockBasedStorageManager.java:290)
        at org.jetbrains.kotlin.storage.LockBasedLazyResolveStorageManager$LockProtectedTrace.record(LockBasedLazyResolveStorageManager.kt:70)
        at org.jetbrains.kotlin.resolve.calls.CallResolver.resolveCallForConstructor(CallResolver.java:380)
        at org.jetbrains.kotlin.resolve.calls.CallResolver.resolveFunctionCall(CallResolver.java:334)
        at org.jetbrains.kotlin.resolve.calls.CallResolver.resolveFunctionCall(CallResolver.java:303)
        at org.jetbrains.kotlin.resolve.AnnotationResolverImpl.resolveAnnotationCall(AnnotationResolverImpl.java:167)
        at org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor$valueArgumentsWithSourceInfo$2.invoke(LazyAnnotations.kt:107)
        at org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor$valueArgumentsWithSourceInfo$2.invoke(LazyAnnotations.kt:106)
        at org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedLazyValue.invoke(LockBasedStorageManager.java:408)
        at org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedNotNullLazyValue.invoke(LockBasedStorageManager.java:527)
        at org.jetbrains.kotlin.storage.StorageKt.getValue(storage.kt:42)
        at org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor.getValueArgumentsWithSourceInfo(LazyAnnotations.kt:106)
        at org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor.access$getValueArgumentsWithSourceInfo(LazyAnnotations.kt:76)
        at org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor$allValueArguments$2.invoke(LazyAnnotations.kt:121)
        at org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor$allValueArguments$2.invoke(LazyAnnotations.kt:120)
        at org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedLazyValue.invoke(LockBasedStorageManager.java:408)
        at org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedNotNullLazyValue.invoke(LockBasedStorageManager.java:527)
        at org.jetbrains.kotlin.storage.StorageKt.getValue(storage.kt:42)
        at org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor.getAllValueArguments(LazyAnnotations.kt:120)
        at org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor.forceResolveAllContents(LazyAnnotations.kt:138)
        at org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil.doForceResolveAllContents(ForceResolveUtil.java:78)
        at org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil.forceResolveAllContents(ForceResolveUtil.java:69)
        at org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil.doForceResolveAllContents(ForceResolveUtil.java:97)
        at org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil.forceResolveAllContents(ForceResolveUtil.java:42)
        at com.google.devtools.ksp.processing.impl.ResolverImpl.resolveDeclarationForLocal(ResolverImpl.kt:838)
        at com.google.devtools.ksp.processing.impl.ResolverImpl.resolveDeclaration(ResolverImpl.kt:497)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSPropertyDeclarationImpl$propertyDescriptor$2.invoke(KSPropertyDeclarationImpl.kt:45)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSPropertyDeclarationImpl$propertyDescriptor$2.invoke(KSPropertyDeclarationImpl.kt:44)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSPropertyDeclarationImpl.getPropertyDescriptor(KSPropertyDeclarationImpl.kt:44)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSPropertyDeclarationImpl.access$getPropertyDescriptor(KSPropertyDeclarationImpl.kt:36)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSPropertyDeclarationImpl$type$2$1.invoke(KSPropertyDeclarationImpl.kt:98)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSPropertyDeclarationImpl$type$2$1.invoke(KSPropertyDeclarationImpl.kt:97)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSTypeReferenceDeferredImpl$resolved$2.invoke(KSTypeReferenceDeferredImpl.kt:57)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSTypeReferenceDeferredImpl$resolved$2.invoke(KSTypeReferenceDeferredImpl.kt:56)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSTypeReferenceDeferredImpl.getResolved(KSTypeReferenceDeferredImpl.kt:56)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSTypeReferenceDeferredImpl.toString(KSTypeReferenceDeferredImpl.kt:67)
        at java.base/java.lang.String.valueOf(String.java:4225)
        at java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
        at it.neckar.ksp.ts.LoggerVisitor.visitTypeReference(LoggerVisitor.kt:156)
        at it.neckar.ksp.ts.LoggerVisitor.visitTypeReference(LoggerVisitor.kt:34)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSTypeReferenceDeferredImpl.accept(KSTypeReferenceDeferredImpl.kt:63)
        at com.google.devtools.ksp.visitor.KSTopDownVisitor.accept(KSTopDownVisitor.kt:34)
        at com.google.devtools.ksp.visitor.KSTopDownVisitor.visitPropertyDeclaration(KSTopDownVisitor.kt:37)
        at it.neckar.ksp.ts.LoggerVisitor.visitPropertyDeclaration(LoggerVisitor.kt:52)
        at it.neckar.ksp.ts.LoggerVisitor.visitPropertyDeclaration(LoggerVisitor.kt:34)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSPropertyDeclarationImpl.accept(KSPropertyDeclarationImpl.kt:115)
        at com.google.devtools.ksp.visitor.KSTopDownVisitor.accept(KSTopDownVisitor.kt:28)
        at com.google.devtools.ksp.visitor.KSTopDownVisitor.visitDeclarationContainer(KSTopDownVisitor.kt:60)
        at it.neckar.ksp.ts.LoggerVisitor.visitDeclarationContainer(LoggerVisitor.kt:92)
        at it.neckar.ksp.ts.LoggerVisitor.visitDeclarationContainer(LoggerVisitor.kt:34)
        at com.google.devtools.ksp.visitor.KSDefaultVisitor.visitFunctionDeclaration(KSDefaultVisitor.kt:38)
        at com.google.devtools.ksp.visitor.KSTopDownVisitor.visitFunctionDeclaration(KSTopDownVisitor.kt:74)
        at it.neckar.ksp.ts.LoggerVisitor.visitFunctionDeclaration(LoggerVisitor.kt:107)
        at it.neckar.ksp.ts.LoggerVisitor.visitFunctionDeclaration(LoggerVisitor.kt:34)
        at com.google.devtools.ksp.symbol.impl.kotlin.KSFunctionDeclarationImpl.accept(KSFunctionDeclarationImpl.kt:118)
        at it.neckar.ksp.ts.TypeScriptGeneratorProcessor.process(TypeScriptGeneratorProcessor.kt:66)
        at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$8$1.invoke(KotlinSymbolProcessingExtension.kt:306)
        at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$8$1.invoke(KotlinSymbolProcessingExtension.kt:304)
        at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.handleException(KotlinSymbolProcessingExtension.kt:410)
        at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.doAnalysis(KotlinSymbolProcessingExtension.kt:304)
        at org.jetbrains.kotlin.js.analyze.AbstractTopDownAnalyzerFacadeForWeb.analyzeFilesWithGivenTrace(TopDownAnalyzerFacadeForJS.kt:142)
        at org.jetbrains.kotlin.js.analyze.AbstractTopDownAnalyzerFacadeForWeb.analyzeFiles(TopDownAnalyzerFacadeForJS.kt:94)
        at org.jetbrains.kotlin.ir.backend.js.ModulesStructure$runAnalysis$1.invoke(klib.kt:534)
        at org.jetbrains.kotlin.ir.backend.js.ModulesStructure$runAnalysis$1.invoke(klib.kt:533)
        at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:115)
        at org.jetbrains.kotlin.ir.backend.js.ModulesStructure.runAnalysis(klib.kt:533)
        at org.jetbrains.kotlin.ir.backend.js.PrepareAnalyzedSourceModuleKt.prepareAnalyzedSourceModule(prepareAnalyzedSourceModule.kt:30)
        at org.jetbrains.kotlin.ir.backend.js.PrepareAnalyzedSourceModuleKt.prepareAnalyzedSourceModule$default(prepareAnalyzedSourceModule.kt:17)
        at org.jetbrains.kotlin.cli.js.K2JsIrCompiler.processSourceModule(K2JsIrCompiler.kt:427)
        at org.jetbrains.kotlin.cli.js.K2JsIrCompiler.doExecute(K2JsIrCompiler.kt:273)
        at org.jetbrains.kotlin.cli.js.K2JSCompiler.doExecute(K2JSCompiler.java:181)
        at org.jetbrains.kotlin.cli.js.K2JSCompiler.doExecute(K2JSCompiler.java:72)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:104)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:48)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1523)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:712)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:598)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:844)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:721)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:720)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.lang.Throwable: Rewrite at slice LEXICAL_SCOPE key: ANNOTATION_ENTRY old value: org.jetbrains.kotlin.resolve.scopes.LexicalScope$Base@5d232184@1562583428 new value: org.jetbrains.kotlin.resolve.scopes.LexicalScope$Base@165ecc78@375311480
<File name: MeisterCharts.kt, Physical: true>
/**
 * Creates the [BarChartStacked] as a child of [element].
 * @see [createBarChartStacked]h
 */
<ELEMENT>@JsExport</ELEMENT>
@JsName("createBarChartStackedFromElement")
@Suppress("unused")
fun createBarChartStackedFromElement(
  element: Element,
): BarChartStacked {
  val barChartStacked = createBarChartStacked()
  element.appendChild(barChartStacked.holder)
  return barChartStacked
}
        ... 101 more
neetopia commented 8 months ago

Is it possible to provide a reproduce repo for this issue? That would greatly help with investigation.

jschneider commented 7 months ago

The bug still exists with 2.0.0-Beta4-1.0.18

Last working version: 1.9.20-1.0.13

I have a monorepo here, which I can not share. But I am more than willing to help debugging this issue? (Remote Session / Screen share / ...)

jschneider commented 7 months ago

This might be the same issue. Contains a link to a reproducer repo:

https://github.com/google/ksp/issues/1630#issuecomment-1837303260

jschneider commented 6 months ago

I investigated a bit further. I can reproduce the exception with a very simple visitor:

Overwriting all visit methods and calling the super method.

class LoggerVisitor(val codeGenerator: CodeGenerator) : KSTopDownVisitor<Int, Unit>() {
...

  override fun visitTypeReference(typeReference: KSTypeReference, data: Int) {
    log("${indent(data)}visitTypeReference: $typeReference $data") //this line is the problem!
    super.visitTypeReference(typeReference, data + 1)
  }
}

Removing "valueArgument" from the log statement solves the exception. Therefore, I suspect the toString() method might be the problem.

joreilly commented 4 weeks ago

Seeing this issue here when using room (only on iOS). Can be reproduced using this PR https://github.com/joreilly/BikeShare/pull/225

Uinsg kotlin = "2.0.20" ksp = "2.0.20-1.0.25"

ERROR: Rewrite at slice LEXICAL_SCOPE key: ANNOTATION_ENTRY old value: org.jetbrains.kotlin.resolve.scopes.LexicalScope$Base@7bd01c44@2077236292 new value: org.jetbrains.kotlin.resolve.scopes.LexicalScope$Base@334b6715@860579605
<File name: AppDatabase.kt, Physical: true>
<ELEMENT>@Database(entities = [Network::class], version = 1)</ELEMENT>
@ConstructedBy(AppDatabaseCtor::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun bikeShareDao(): BikeShareDao
}

I'm also using ksp for kotlin-inject in this project so that's perhaps a factor

joreilly commented 4 weeks ago

So, changing to following order seems to work around the issue

dependencies {
    ksp(libs.androidx.room.compiler)
    ksp(libs.kotlininject.compiler)
}