ZacSweers / anvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.
https://www.zacsweers.dev/introducing-anvil-ksp/
Apache License 2.0
17 stars 4 forks source link

Improve handling of error types in dagger factory generation #69

Closed ZacSweers closed 1 month ago

ZacSweers commented 1 month ago

Discussed in https://github.com/ZacSweers/anvil/discussions/62

Originally posted by **bddckr** August 26, 2024 I have a class `A` that depends on class `B`: ```kotlin class A @Inject constructor(private val b: B) ``` `B` is generated by my own code generator, which is also using KSP. I'm getting the following error from the `kspDebugKotlin` task (this is an Android library module) when using KSP2: > [ksp] Error type '<ERROR TYPE: B>' is not resolvable in the current round of processing. Check your imports or, if this is a generated type, ensure the tool that generates it has its outputs appropriately sources as inputs to the KSP task. > > A failure occurred while executing com.google.devtools.ksp.gradle.KspAAWorkerAction > > KSP failed with exit code: PROCESSING_ERROR When using KSP1, the error message is pretty much the same - it just mentions the file name as well, which helps when debugging: > [ksp] /Users/bddckr/Projects/ksp-testing/some-module/src/main/kotlin/A.kt:5: Error type '<ERROR TYPE: B>' is not resolvable in the current round of processing. Check your imports or, if this is a generated type, ensure the tool that generates it has its outputs appropriately sources as inputs to the KSP task. I disabled all the KSP processors/dependencies one-by-one to identify whether this is from dagger-ksp or Anvil. It's Anvil as far as I can see - this error isn't raised when I run with just my generator and dagger-ksp. I've tried using `AnvilKspExtension` while adding a custom annotation to the class definition of `A`, so I can return the fully qualified name of that annotation via `AnvilKspExtension.supportedAnnotationTypes`, but it doesn't help: The error is raised _after_ my custom code generator. Without usage of `AnvilKspExtension`, the error is raised _before_ my code generator executes. (I've learned that by logging via the environment's `KSPLogger` in my generator.) --- How do I get Anvil's symbol processor to "ignore" the missing type until my generator had a chance to create it in the current round? Is the usage of `AnvilKspExtension` required? If so: * What am I doing wrong? * Is there a way to use `AnvilKspExtension` without requiring the use of an annotation? As you can see, in my use case there's no annotation. (My code generator looks up files and class declarations based on some naming convention - no annotation-based lookup is done at all.) Side-note: This is all working fine with a custom generator and upstream Anvil v2.5.0-beta11. It's only now that I'm moving everything to KSP that I'm having an issue with my use case. It looks like KSP expects the Anvil processor to not fail, but return the erroring symbols to defer to the next round. At least that's my understanding from [this](https://github.com/google/ksp/issues/1043#issuecomment-1187768871) and the [KSP docs on multi-round processing](https://kotlinlang.org/docs/ksp-multi-round.html).
ZacSweers commented 1 month ago

Do you want to try the latest snapshot to see if that works for you?

bddckr commented 1 month ago

This completely resolves my issue in #62! Thanks a lot for this as well as the upstream work you've done on Anvil, and fork in the first place! ❤️