raamcosta / compose-destinations

Annotation processing library for type-safe Jetpack Compose navigation with no boilerplate.
https://composedestinations.rafaelcosta.xyz
Apache License 2.0
3.14k stars 130 forks source link

Support K2 #545

Open Nek-12 opened 6 months ago

Nek-12 commented 6 months ago

Currently, trying to build with KSP2 / K2 fails with:

Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.google.devtools.ksp.gradle.KspAAWorkerAction
    <junk>
Caused by: java.lang.NoClassDefFoundError: javaslang/collection/HashMap
    at org.jetbrains.kotlin.load.java.structure.impl.classFiles.ClassifierResolutionContext.<init>(ClassifierResolutionContext.kt:40)
    at com.google.devtools.ksp.impl.symbol.kotlin.UtilKt$getDefaultValue$1$2$visitMethod$1.visitAnnotationDefault(util.kt:435)
    at org.jetbrains.org.objectweb.asm.ClassReader.readMethod(ClassReader.java:1364)
    at org.jetbrains.org.objectweb.asm.ClassReader.accept(ClassReader.java:712)
    at org.jetbrains.org.objectweb.asm.ClassReader.accept(ClassReader.java:395)
    at com.google.devtools.ksp.impl.symbol.kotlin.UtilKt.getDefaultValue(util.kt:422)
    at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl$defaultArguments$2.invoke(KSAnnotationImpl.kt:77)
    at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl$defaultArguments$2.invoke(KSAnnotationImpl.kt:60)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl.getDefaultArguments(KSAnnotationImpl.kt:60)
    at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl$arguments$2.invoke(KSAnnotationImpl.kt:54)
    at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl$arguments$2.invoke(KSAnnotationImpl.kt:51)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at com.google.devtools.ksp.impl.symbol.kotlin.KSAnnotationImpl.getArguments(KSAnnotationImpl.kt:51)
    at com.ramcosta.composedestinations.ksp.processors.KspToCodeGenDestinationsMapper.toDestination(KspToCodeGenDestinationsMapper.kt:557)
    at com.ramcosta.composedestinations.ksp.processors.KspToCodeGenDestinationsMapper.access$toDestination(KspToCodeGenDestinationsMapper.kt:67)
    at com.ramcosta.composedestinations.ksp.processors.KspToCodeGenDestinationsMapper$map$1.invoke(KspToCodeGenDestinationsMapper.kt:78)
    at com.ramcosta.composedestinations.ksp.processors.KspToCodeGenDestinationsMapper$map$1.invoke(KspToCodeGenDestinationsMapper.kt:78)
    at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:210)
    at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:811)
    at com.ramcosta.composedestinations.ksp.processors.KspToCodeGenDestinationsMapper.map(KspToCodeGenDestinationsMapper.kt:78)
    at com.ramcosta.composedestinations.ksp.processors.Processor.process(Processor.kt:55)
    at com.google.devtools.ksp.impl.KotlinSymbolProcessing.execute(KotlinSymbolProcessing.kt:464)
    <junk>
    ... 2 more
Caused by: java.lang.ClassNotFoundException: javaslang.collection.HashMap
    ... 56 more

gradle.properties:

kotlin.experimental.tryK2=true
android.lint.useK2Uast=true
ksp.useKSP2=true
Nek-12 commented 1 month ago

Looks like K2 is enabled by default in Kotlin 2.0. Did anyone get the library working with k2 enabled?

FooIbar commented 1 month ago

You can use it with K2, we've been doing that for a while, just don't use KSP2.

FooIbar commented 1 month ago

Looks like https://github.com/FooIbar/compose-destinations/commit/7875839d4d6729cf93b3a98c91d09e1c7fa1a993 is the only change needed to support KSP2. Note KSP 1.0.20 does not have https://github.com/google/ksp/pull/1871, so those arguments that have class literals as default values need to be explicitly specified, e.g.

@Destination<RootGraph>(navArgs = Nothing::class, style = DestinationStyle.Default::class)
raamcosta commented 1 month ago

Ok thank you @FooIbar

But just so we're clear. Using 1.0.20 KSP means we'd have to specify all field defaults for annotations that have class types, but only if using K2 as well, right?

FooIbar commented 1 month ago

Ok thank you @FooIbar

But just so we're clear. Using 1.0.20 KSP means we'd have to specify all field defaults for annotations that have class types, but only if using K2 as well, right?

Yes, only if using KSP2. That fix has been cherry-picked to the 1.0.21 branch https://github.com/google/ksp/commit/fc0cdbf52448e3423d0c381ff3c42e5fed95c1c1, which should be released soon?

raamcosta commented 1 month ago

You scared me for a moment.. I thought for a moment that they would be dropping that completely 😅 made no sense but we never know..

FooIbar commented 1 month ago

You scared me for a moment.. I thought for a moment that they would be dropping that completely 😅 made no sense but we never know..

I should have made it clearer. That is only for testing KSP2 compatibility with 1.0.20 https://github.com/FooIbar/compose-destinations/commit/d7e79dd31ebed6c6812d28e88ab63ec704c3e97b.

Update: Tested with 1.0.21, it's only partially fixed. Filed a bug on KSP https://github.com/google/ksp/issues/1910.