ZacSweers / MoshiX

Extensions for Moshi including IR plugins, moshi-sealed, and more.
Apache License 2.0
514 stars 37 forks source link

kotlinx.metadata.InconsistentKotlinMetadataException: Exception occurred when reading Kotlin metadata #533

Closed snowe2010 closed 9 months ago

snowe2010 commented 9 months ago

I'm not sure where to ask for help on this. It seems related to this library, but I'm not entirely sure.

I'm trying to deploy an http4k application using graalvm on AWS Lambda. I swear this was working before new years 😂, but now I'm getting this error in AWS, but not locally.

Stack Trace
ERROR [io.qua.ama.lam.run.AbstractLambdaPollLoop] (Lambda Thread (NORMAL)) Failed to run lambda (NORMAL): kotlinx.metadata.InconsistentKotlinMetadataException: Exception occurred when reading Kotlin metadata
at kotlinx.metadata.jvm.KotlinClassMetadata$Companion.read(KotlinClassMetadata.kt:721)
at dev.zacsweers.moshix.reflect.MetadataKotlinJsonAdapterFactoryKt.toKmClass(MetadataKotlinJsonAdapterFactory.kt:408)
at dev.zacsweers.moshix.reflect.MetadataKotlinJsonAdapterFactoryKt.access$toKmClass(MetadataKotlinJsonAdapterFactory.kt:1)
at dev.zacsweers.moshix.reflect.MetadataKotlinJsonAdapterFactory.create(MetadataKotlinJsonAdapterFactory.kt:229)
at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
at com.squareup.moshi.Moshi.adapter(Moshi.java:106)
at com.squareup.moshi.Moshi.adapter(Moshi.java:80)
at org.http4k.format.ConfigurableMoshi.asFormatString(ConfigurableMoshi.kt:86)
at com.sunrun.pricing.tax.login.UpdateServiceAuthTokenLambda$handleRequest$1.invoke(UpdateServiceAuthTokenLambda.kt:42)
at com.sunrun.pricing.tax.login.UpdateServiceAuthTokenLambda$handleRequest$1.invoke(UpdateServiceAuthTokenLambda.kt:30)
at com.sunrun.pricing.aws_common.serialization.RequestStreamWrapperKt.requestStreamWrapper(RequestStreamWrapper.kt:35)
at com.sunrun.pricing.tax.login.UpdateServiceAuthTokenLambda.handleRequest(UpdateServiceAuthTokenLambda.kt:30)
at io.quarkus.amazon.lambda.runtime.AmazonLambdaRecorder$1.processRequest(AmazonLambdaRecorder.java:189)
at io.quarkus.amazon.lambda.runtime.AbstractLambdaPollLoop$1.run(AbstractLambdaPollLoop.java:129)
at java.base@21.0.1/java.lang.Thread.runWith(Thread.java:1596)
at java.base@21.0.1/java.lang.Thread.run(Thread.java:1583)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:832)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:211)
Caused by: java.lang.IllegalStateException: No MetadataExtensions instances found in the classpath. Please ensure that the META-INF/services/ is not stripped from your application and that the Java virtual machine is not running under a security manager
at kotlinx.metadata.internal.extensions.MetadataExtensions$Companion$INSTANCES$2.invoke(MetadataExtensions.kt:88)
at kotlinx.metadata.internal.extensions.MetadataExtensions$Companion$INSTANCES$2.invoke(MetadataExtensions.kt:85)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at kotlinx.metadata.internal.extensions.MetadataExtensions$Companion.getINSTANCES(MetadataExtensions.kt:85)
at kotlinx.metadata.KmClass.(Nodes.kt:126)
at kotlinx.metadata.jvm.KotlinClassMetadata$Class.(KotlinClassMetadata.kt:62)
at kotlinx.metadata.jvm.KotlinClassMetadata$Companion.read(KotlinClassMetadata.kt:583)
... 17 more 

My adapter looks like this:

object MoshiXLite : ConfigurableMoshi(
    Moshi.Builder()
        .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
        .add(
            PolymorphicJsonAdapterFactory.of(LineResponseTaxesInner::class.java, "responseSummary")
                .withSubtype(Tax::class.java, "FullDetails")
                .withSubtype(TaxSummary::class.java, "SummaryByErpCode")
                .withSubtype(TaxSummary::class.java, "SummaryByAuthorityAndZone")
        )
        .addLast(BigDecimalAdapter())
        .addLast(MapAdapter)
        .addLast(ThrowableAdapter)
        .addLast(ListAdapter)
        .addLast(EventAdapter)
        .add(ApplicationJsonAdapterFactory)
        .asConfigurable(MetadataKotlinJsonAdapterFactory())
        .withStandardMappings()
        .done()
)

I'm using kotshi as well to get rid of reflection.

The failure spot is in this code:

is Success -> {
                    val body = MoshiXLite.asFormatString(result.value)
                    val lambdaProxyResponse = LambdaProxyResponse(204, body)
                    output.write(MoshiXLite.asFormatString(lambdaProxyResponse).toByteArray())
                }

at the MoshiXLite line.

If there's any more information I need to provide let me know.

edit: oh i forgot to mention, I was following this guide to get it working in the first place. https://www.http4k.org/guide/howto/moshi_lite/

ZacSweers commented 9 months ago

The issue tracker is not a place for usage questions. You can use discussions' Q&A, which I'll convert this to.