google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.45k stars 2.02k forks source link

[KSP2] Assisted injection fails with `UnexpectedException` in the presence of `suspend` methods #4496

Open mcumings opened 3 weeks ago

mcumings commented 3 weeks ago

Dagger: 2.52 KSP: 2.0.21-1.0.26

The following results in an exception during processing:

class InjectedClass @AssistedInject constructor(
    @Assisted val assistedArg: String,
) {
    suspend fun method() { }
}

The result:

Execution failed for task ':lib:kspDebugKotlin'.
> A failure occurred while executing com.google.devtools.ksp.gradle.KspAAWorkerAction
   > 
       Validation trace:
         => element (CLASS): repro.ksp.issue5.InjectedClass

The stack trace is as follows:

    [ elided for brevity ]
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.google.devtools.ksp.gradle.KspAAWorkerAction
    [ elided for brevity ]
Caused by: dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException$UnexpectedException: 
  Validation trace:
    => element (CLASS): repro.ksp.issue5.InjectedClass
    at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.from(DaggerSuperficialValidation.java:527)
    at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.access$100(DaggerSuperficialValidation.java:491)
    at dagger.internal.codegen.base.DaggerSuperficialValidation.validateElement(DaggerSuperficialValidation.java:299)
    at dagger.internal.codegen.processingstep.SuperficialValidator.validationExceptionsUncached(SuperficialValidator.java:58)
    at dagger.internal.codegen.processingstep.SuperficialValidator.throwIfNearestEnclosingTypeNotValid(SuperficialValidator.java:47)
    at dagger.internal.codegen.processingstep.TypeCheckingProcessingStep.lambda$process$0(TypeCheckingProcessingStep.java:92)
    at com.google.common.collect.SingletonImmutableBiMap.forEach(SingletonImmutableBiMap.java:70)
    at dagger.internal.codegen.processingstep.TypeCheckingProcessingStep.process(TypeCheckingProcessingStep.java:72)
    at dagger.internal.codegen.processingstep.TypeCheckingProcessingStep.process(TypeCheckingProcessingStep.java:49)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.XProcessingStep.process(XProcessingStep.kt:57)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.CommonProcessorDelegate.processRound(XBasicAnnotationProcessor.kt:134)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspBasicAnnotationProcessor.process(KspBasicAnnotationProcessor.kt:62)
    at com.google.devtools.ksp.impl.KotlinSymbolProcessing$execute$1$1.invoke(KotlinSymbolProcessing.kt:553)
    at com.google.devtools.ksp.impl.KotlinSymbolProcessing$execute$1$1.invoke(KotlinSymbolProcessing.kt:551)
    at ksp.com.google.devtools.ksp.common.IncrementalContextBase.closeFilesOnException(IncrementalContextBase.kt:409)
    at com.google.devtools.ksp.impl.KotlinSymbolProcessing.execute(KotlinSymbolProcessing.kt:551)
    at com.google.devtools.ksp.impl.KSPLoader$Companion.loadAndRunKSP(KSPLoader.kt:37)
    at com.google.devtools.ksp.impl.KSPLoader.loadAndRunKSP(KSPLoader.kt)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)
    at com.google.devtools.ksp.gradle.KspAAWorkerAction.execute(KspAATask.kt:525)
    at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:66)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:62)
    at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:100)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:62)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:209)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:166)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
    at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:59)
    at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$0(DefaultWorkerExecutor.java:174)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:194)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.access$700(DefaultConditionalExecutionQueue.java:127)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner$1.run(DefaultConditionalExecutionQueue.java:169)
    at org.gradle.internal.Factories$1.create(Factories.java:31)
    at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:263)
    at org.gradle.internal.work.DefaultWorkerLeaseService.runAsWorkerThread(DefaultWorkerLeaseService.java:127)
    at org.gradle.internal.work.DefaultWorkerLeaseService.runAsWorkerThread(DefaultWorkerLeaseService.java:132)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:164)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:133)
    ... 2 more
Caused by: java.lang.IllegalStateException: unexpected jvm signature V
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.javac.kotlin.JvmDescriptorUtilsKt.typeNameFromJvmSignature(JvmDescriptorUtils.kt:106)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KSTypeJavaPoetExtKt.asJTypeName(KSTypeJavaPoetExt.kt:99)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KSTypeJavaPoetExtKt.asJTypeName(KSTypeJavaPoetExt.kt:182)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KSTypeJavaPoetExtKt.asJTypeName(KSTypeJavaPoetExt.kt:64)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KSTypeJavaPoetExtKt.asJTypeName$resolveTypeName(KSTypeJavaPoetExt.kt:143)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KSTypeJavaPoetExtKt.asJTypeName(KSTypeJavaPoetExt.kt:145)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KSTypeJavaPoetExtKt.asJTypeName(KSTypeJavaPoetExt.kt:172)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KSTypeJavaPoetExtKt.asJTypeName(KSTypeJavaPoetExt.kt:154)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.DefaultKspType.resolveJTypeName(DefaultKspType.kt:43)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType$xTypeName$2.invoke(KspType.kt:90)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType$xTypeName$2.invoke(KspType.kt:69)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType.getXTypeName(KspType.kt:69)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType.access$getXTypeName(KspType.kt:49)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType$typeName$2.invoke(KspType.kt:61)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType$typeName$2.invoke(KspType.kt:61)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType.getTypeName(KspType.kt:61)
    at dagger.internal.codegen.xprocessing.XTypes.toStableString(XTypes.java:496)
    at dagger.internal.codegen.xprocessing.XElements.toStableString(XElements.java:346)
    at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.getMessageForElement(DaggerSuperficialValidation.java:637)
    at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.append(DaggerSuperficialValidation.java:547)
    at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.access$200(DaggerSuperficialValidation.java:491)
    at dagger.internal.codegen.base.DaggerSuperficialValidation.validateElement(DaggerSuperficialValidation.java:299)
    at dagger.internal.codegen.base.DaggerSuperficialValidation.validateElements(DaggerSuperficialValidation.java:250)
    at dagger.internal.codegen.base.DaggerSuperficialValidation.validateElement(DaggerSuperficialValidation.java:285)
    ... 45 more
amal commented 2 weeks ago

We have lots of these errors (https://github.com/google/ksp/issues/2177) from Dagger when using ksp.UseKSP2=true

e: [ksp] dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException$UnexpectedException: 
  Validation trace:
    => element (CLASS): app.PostViewModel

> Task :kspDebugKotlin FAILED
e: [ksp] dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException$UnexpectedException: 
  Validation trace:
    => element (CLASS): app.NewViewModel

...
Chang-Eric commented 1 week ago

@amal

when using ksp.UseKSP2=true

Please see https://github.com/google/dagger/issues/2349#issuecomment-2329852274 but we actually don't support KSP2 right now so you shouldn't expect things to work when using it. It is a work in progress and our current top priority though.