Skip to content

Commit 84fe0cc

Browse files
committed
Regression test for Metro bug
Reproduces a bug where `@ContributesBinding` contributions in the same module as a `@DependencyGraph` generated by an external FIR extension (`@DevelopmentAppComponent`) are not discovered during the SUPERTYPES phase. The `@MetroContribution` annotation's scope argument can't be resolved at the SUPERTYPES phase for same-module classes in real Gradle compilations, causing the contribution to be silently dropped.
1 parent c4e843e commit 84fe0cc

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.squareup.test.demo
2+
3+
import com.squareup.dagger.AppScope
4+
import dev.zacsweers.metro.ContributesBinding
5+
import dev.zacsweers.metro.ContributesTo
6+
import dev.zacsweers.metro.Inject
7+
import dev.zacsweers.metro.binding
8+
9+
/**
10+
* Reproduces a bug where @ContributesBinding contributions in the same module as a
11+
*
12+
* @DependencyGraph generated by an external FIR extension (@DevelopmentAppComponent) are not
13+
* discovered during the SUPERTYPES phase.
14+
*
15+
* The @MetroContribution annotation's scope argument can't be resolved at the SUPERTYPES phase for
16+
* same-module classes in real Gradle compilations, causing the contribution to be silently dropped.
17+
*/
18+
interface DemoService {
19+
val name: String
20+
}
21+
22+
@Inject
23+
@ContributesBinding(AppScope::class, binding = binding<DemoService>())
24+
class DemoServiceImpl : DemoService {
25+
override val name: String = "contributed-binding"
26+
}
27+
28+
/** Exposes [DemoService] on the generated graph. */
29+
@ContributesTo(AppScope::class)
30+
interface DemoServiceComponent {
31+
val demoService: DemoService
32+
}

integration-tests/demo/src/test/kotlin/com/squareup/test/demo/DemoAppTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.app.Application
44
import com.squareup.development.shell.DevelopmentAppComponent
55
import dev.zacsweers.metro.createGraphFactory
66
import kotlin.test.Test
7+
import kotlin.test.assertEquals
78
import kotlin.test.assertIs
89
import kotlin.test.assertNotNull
910

@@ -29,4 +30,16 @@ class DemoAppTest {
2930
val component = factory.create(Application())
3031
assertNotNull(component)
3132
}
33+
34+
@Test
35+
fun `same-module ContributesBinding is discovered for generated DependencyGraph`() {
36+
val factory = createGraphFactory<DemoApp.MetroComponent.Factory>()
37+
val component = factory.create(Application())
38+
// This verifies that DemoServiceImpl's @ContributesBinding(AppScope::class) is properly
39+
// merged into the MetroComponent generated by @DevelopmentAppComponent. Without the
40+
// name-based fallback in ContributedInterfaceSupertypeGenerator, the scope resolution
41+
// fails during the SUPERTYPES phase and DemoService cannot be provided.
42+
val service = (component as DemoServiceComponent).demoService
43+
assertEquals("contributed-binding", service.name)
44+
}
3245
}

0 commit comments

Comments
 (0)