square / anvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.
Apache License 2.0
1.29k stars 76 forks source link

Compiling annotation that does not exist throws `NoSuchElementException` #881

Open agrosner opened 5 months ago

agrosner commented 5 months ago

in 2.5.0-beta1, kotlin 1.9.22

I have an annotation in code that was mistakenly using old package name from a merge conflict. I tried compiling the app and anvil crushes the compilation error:

import com.foo.wrong.Bar // does not actually exist anymore

@Bar
class MyClass

Anvil fails with:

e: java.util.NoSuchElementException: Collection contains no element matching the predicate. at com.squareup.anvil.compiler.internal.PsiUtilsKt.requireFqName(PsiUtils.kt:379) at com.squareup.anvil.compiler.internal.PsiUtilsKt.requireFqName(PsiUtils.kt:136) at com.squareup.anvil.compiler.internal.PsiUtilsKt.requireFqName(PsiUtils.kt:154) at com.squareup.anvil.compiler.internal.reference.AnnotationReferenceKt.toAnnotationReference(AnnotationReference.kt:227) at com.squareup.anvil.compiler.internal.reference.ClassReference$Psi$annotations$2.invoke(ClassReference.kt:156) at com.squareup.anvil.compiler.internal.reference.ClassReference$Psi$annotations$2.invoke(ClassReference.kt:155) at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81) at com.squareup.anvil.compiler.internal.reference.ClassReference$Psi.getAnnotations(ClassReference.kt:155) at com.squareup.anvil.compiler.internal.reference.AnnotatedReference$DefaultImpls.isAnnotatedWith(AnnotatedReference.kt:14) at com.squareup.anvil.compiler.internal.reference.ClassReference.isAnnotatedWith(ClassReference.kt:54) at com.squareup.anvil.compiler.codegen.BindingModuleGenerator.generateCode(BindingModuleGenerator.kt:74) at com.squareup.anvil.compiler.codegen.CodeGenerationExtension.generateCode$generateCode(CodeGenerationExtension.kt:193) at com.squareup.anvil.compiler.codegen.CodeGenerationExtension.generateCode(CodeGenerationExtension.kt:219)

RBusarow commented 5 months ago

As I understand it, you fixed the issue in your code but Anvil is still trying to reference the old type name?

This sounds like a symptom of the incremental compilation issues that were addressed in the current beta release.

In order to enable the new behavior, you need to enable the trackSourceFiles flag in the Gradle extension or in gradle.properties via com.squareup.anvil.trackSourceFiles=true.

agrosner commented 5 months ago

Sorry to clarify, we have that already turned on. This is code from a stale branch merged from the main branch. The merged code referenced an annotation that moved packages but the code was not updated to use it. So hitting compile resulted in the error above.