Repository navigation
Stack overflow with Dagger 2.56.2 #4702
Description
Activity
This sounds similar to square/kotlinpoet#1737.
It would be nice to have a repro, but we'd first need to know what type is causing this issue and for that we'd need more info in the stack trace to understand where this was called from in Dagger/Hilt. I'm not sure why the stacktrace isn't giving you more info about where the recursion originated from, but if you can somehow get that information we could do more to help.
- O remember having a similar problem with recursive errors. In my case I had a mix of javax with Jakarta annotations. Filipe Sousa…On Wed, Apr 23, 2025 at 22:53 Brad Corso ***@***.***> wrote: *bcorso* left a comment (google/dagger#4702) <#4702 (comment)> This sounds similar to square/kotlinpoet#1737 <square/kotlinpoet#1737>. It would be nice to have a repro, but we'd first need to know what type is causing this issue and for that we'd need more info in the stack trace to understand where this was called from in Dagger/Hilt. I'm not sure why the stacktrace isn't giving you more info about where the recursion originated from, but if you can somehow get that information we could do more to help. — Reply to this email directly, view it on GitHub <#4702 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABGDZQOCJZESNAF5IXGKLA323ADXHAVCNFSM6AAAAAB3W23T4SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDQMRVGU3TQNRTG4> . You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
Sorry, but I have not yet been able to dig much into this due to some personal problems. If I get some free time and if I can create a reproducible example I will update later.
From the comments above and the kotlinpoet issue my current guess is that it may be from types with self referencing generics for emulating self types like:
interface PaginatedElement { val identifier: String } interface PaginatedSection<Element : PaginatedElement, Section : PaginatedSection<Element, Section>> { val identifier: String val elements: List<Element> fun withNewElements(elements: List<Element>): Section }and/or the actual classes implementing
PaginatedSectionand similar classes / interfaces.I have been able to make a bit more progress here. I have not yet been able to identify which code causes the failure exactly, but there seems to be some incompatibility or odd interaction with the hilt extensions on AndroidX (JetPack). If I remove the dependency and the ksp compiler from that there is no longer an stack overflow happening. Upgrading from version 1.2.0 to version 1.3.0-alpha01 of AndroidX hilt does not help either. Only if I remove their hilt-compiler the build goes through with Dagger 2.56.2. But on Dagger 2.56.1 the build succeeded without issues with both ksp compilers included.
Reacted by Timothy### Workaround: Use KAPT instead of KSP I encountered the same `StackOverflowError` when upgrading to Dagger/Hilt 2.56.2+. **Root Cause:** The issue is in KotlinPoet's handling of recursively bound generics (e.g., `T : Comparable<T>`), which causes infinite recursion in `hashCode()` methods during annotation processing with KSP. **Workaround:** Switching from KSP to KAPT for Hilt annotation processing resolves this issue. // Before (KSP - causes StackOverflowError) plugins { id("com.google.devtools.ksp") } dependencies { ksp("com.google.dagger:hilt-compiler:2.59") ksp("com.google.dagger:hilt-android-compiler:2.59") } // After (KAPT - works fine) plugins { id("com.android.legacy-kapt") } dependencies { kapt("com.google.dagger:hilt-compiler:2.59") kapt("com.google.dagger:hilt-android-compiler:2.59") }Using this workaround, I was able to successfully upgrade to the latest version Dagger/Hilt 2.59.
⚠️ However, I'm not entirely comfortable with this solution since KAPT is an older annotation processing method and KSP is the recommended modern approach with better performance. It would be great to see a proper fix so we can use KSP with the latest Dagger/Hilt versions.Reacted by A.Sina Birdevrim and Juan Ramón González@emreozerdev, I looked into this issue recently and found that it was introduced by square/kotlinpoet#1477 and first released in KotlinPoet 1.13.0.
Thus, another solution here is to try to downgrade your projects version of KotlinPoet to 1.12.0.
Note that Dagger itself depends on KotlinPoet 1.12.0, but it's likely that something else in your build deps is pulling in a higher version.
Reacted by Emre ÖzerThanks for the findings!. Unfortunately Dagger Hilt for Android transitively depends on kotlinpoet 1.14.2 so it does have the issue. But that probably explains why I could use the non-Hilt variants without getting the same issue.
Unfortunately Dagger Hilt for Android transitively depends on kotlinpoet 1.14.2 so it does have the issue
@jrgonzalezg, while not ideal, you can force Gradle to use a specific version of transitive dependencies (assuming the newer version of the transitive dependency isn't strictly necessary)
I think there's a lot of ways to do it in Gradle, but something like this should work:
configurations.all { resolutionStrategy.dependencySubstitution { substitute(module("com.squareup:kotlinpoet")) .using(module("com.squareup:kotlinpoet:1.12.0")) .because("Kotlinpoet 1.13+ introduces a bug (see https://github.com/square/kotlinpoet/issues/1737).") } }Note: The
"com.squareup:kotlinpoet"artifact was changed to"com.squareup:kotlinpoet-jvm"in KotlinPoet 1.15.0, so you may also need to add a substitution for that as well.Reacted by Juan Ramón González
My app has been compiling fine until Dagger 2.56.1 (included). But if I try to update to Dagger/Hilt 2.56.2 I get this exception on every build:
The stacktrace keeps repeating those lines which suggest some infinite recursion. And I have no information provided of which of my code leads to that.