Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Support Dagger KSP #713

Closed
wants to merge 65 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
b6c2859
Add dependencies
ZacSweers Feb 8, 2023
faa02d4
Update dagger to latest for KSP support
ZacSweers Feb 8, 2023
a124fe5
Extract modulemerger code to make it more reusable
ZacSweers May 29, 2023
93dbb57
Add new KSP option to Anvil plugin
ZacSweers May 29, 2023
1949ec2
Add necessary deps
ZacSweers May 29, 2023
c51fca5
Implement most of module merging
ZacSweers May 29, 2023
3daeaa5
Refactor InterfaceMerger for reuse
ZacSweers May 29, 2023
6060c79
Implement interface merging in KSP
ZacSweers Jun 8, 2023
762623b
Merge branch 'main' into z/kspSupport
ZacSweers Jun 8, 2023
e79b69b
Merge conflicts
ZacSweers Jun 8, 2023
ff1e452
Update KSP to match kotlin
ZacSweers Jun 8, 2023
e0b053a
Clean up module merger
ZacSweers Jun 8, 2023
6b922a9
Make ktlint happy
ZacSweers Jun 8, 2023
72e746d
Merge branch 'main' into z/kspSupport
ZacSweers Jun 8, 2023
bec3077
Introduce DaggerAnnotationProcessingMode to better control dagger pro…
ZacSweers Jun 8, 2023
f443f6f
Add KSP impls to testImplementation
ZacSweers Jun 8, 2023
2a05a90
WIP try making KCT compile KSP-generated code
ZacSweers Jun 20, 2023
9dd02f3
Merge branch 'main' into z/kspSupport
ZacSweers Jul 30, 2023
4e0e7de
Finish implementing module merging
ZacSweers Jul 30, 2023
0009120
Merge branch 'main' into z/kspSupport
ZacSweers Aug 2, 2023
ed92f73
Merge cleanups
ZacSweers Aug 2, 2023
45ffc64
Remove commented code
ZacSweers Aug 2, 2023
a4c2a61
More merge cleanup
ZacSweers Aug 2, 2023
3a90532
Simplify test stuff again
ZacSweers Aug 2, 2023
bfdc4b4
Additive
ZacSweers Aug 2, 2023
3b95dad
Another merge conflict
ZacSweers Aug 2, 2023
c6b475d
Update deps
ZacSweers Aug 2, 2023
790b65a
Restore
ZacSweers Aug 2, 2023
6a9068f
Exclude the dagger processor from service loading
ZacSweers Aug 2, 2023
46d2f73
Get tests running across all three modes
ZacSweers Aug 3, 2023
4e7ef73
Link issue
ZacSweers Aug 3, 2023
d9ee416
Tidy up some naming and visibility
ZacSweers Aug 3, 2023
00fd7f0
Fix invisible access
ZacSweers Aug 3, 2023
e50ab45
Align name of resolver
ZacSweers Aug 3, 2023
e2af1e7
Fix test with illegal parameter name
ZacSweers Aug 3, 2023
e67fbd1
Formatting
ZacSweers Aug 3, 2023
99658b3
Update gradle extension setup
ZacSweers Aug 3, 2023
7faeb72
Small fixes
ZacSweers Aug 3, 2023
32857de
Merge branch 'main' into z/kspSupport
ZacSweers Sep 9, 2023
489b5a8
Update Dagger
ZacSweers Sep 9, 2023
7b262f5
Extract MergeAnnotationMapper abstraction
ZacSweers Sep 9, 2023
cea1873
Merge branch 'main' into z/kspSupport
ZacSweers Oct 15, 2023
e00cf6e
Implement v2 of KSP impl
ZacSweers Oct 18, 2023
e31a161
Delete old impl
ZacSweers Oct 18, 2023
c5a4ec6
Delete old module merger
ZacSweers Oct 18, 2023
1a10c0c
Fix ref to new one
ZacSweers Oct 18, 2023
2b31908
Fix hint package handling
ZacSweers Oct 18, 2023
6546ac7
Creators are optional, also exclude ANY supertypes
ZacSweers Oct 18, 2023
4eb8f4a
Split behavior of merge modules and components
ZacSweers Oct 18, 2023
bc3b6c4
Handle different includes names
ZacSweers Oct 18, 2023
9122169
Better error message for missing fq names
ZacSweers Oct 18, 2023
0cc2c93
Merge branch 'main' into z/kspSupport
ZacSweers Dec 25, 2023
8b48aab
Update gradle files
ZacSweers Dec 25, 2023
0fcd0a7
Merge fixes
ZacSweers Dec 25, 2023
d0cfb46
Formatting
ZacSweers Dec 25, 2023
af31f8b
Misc fixes
ZacSweers Dec 25, 2023
64411d3
Add dagger processor to compilation when processing with dagger
ZacSweers Dec 25, 2023
e0fd618
Allow dagger ksp and anvil ksp
ZacSweers Dec 25, 2023
931da56
More fixes
ZacSweers Dec 25, 2023
181084d
Add compilation helper
ZacSweers Dec 25, 2023
543b0b4
Update to latest dagger with improved KSP support
ZacSweers Dec 25, 2023
8a36de4
More fixes
ZacSweers Dec 25, 2023
daf2e9e
Formatting
ZacSweers Dec 25, 2023
dab8e67
Force later guava version in tests
ZacSweers Dec 25, 2023
9dd25fe
Lower dagger version for now
ZacSweers Dec 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public sealed class AnnotationReference {
public class Psi internal constructor(
public val annotation: KtAnnotationEntry,
override val classReference: ClassReference,
override val declaringClass: ClassReference.Psi?,
override val declaringClass: ClassReference?,
) : AnnotationReference() {

override val arguments: List<AnnotationArgumentReference.Psi> by lazy(NONE) {
Expand All @@ -198,7 +198,7 @@ public sealed class AnnotationReference {
public class Descriptor internal constructor(
public val annotation: AnnotationDescriptor,
override val classReference: ClassReference,
override val declaringClass: ClassReference.Descriptor?,
override val declaringClass: ClassReference?,
) : AnnotationReference() {

override val arguments: List<AnnotationArgumentReference.Descriptor> by lazy(NONE) {
Expand All @@ -221,10 +221,21 @@ public sealed class AnnotationReference {
public fun KtAnnotationEntry.toAnnotationReference(
declaringClass: ClassReference.Psi?,
module: ModuleDescriptor,
): Psi {
return toAnnotationReference(
classReference = requireFqName(module).toClassReference(module),
declaringClass = declaringClass,
)
}

@ExperimentalAnvilApi
public fun KtAnnotationEntry.toAnnotationReference(
declaringClass: ClassReference?,
classReference: ClassReference,
): Psi {
return Psi(
annotation = this,
classReference = requireFqName(module).toClassReference(module),
classReference = classReference,
declaringClass = declaringClass,
)
}
Expand All @@ -237,10 +248,17 @@ public fun AnnotationDescriptor.toAnnotationReference(
val annotationClass = annotationClass ?: throw AnvilCompilationException(
message = "Couldn't find the annotation class for $fqName",
)
return toAnnotationReference(declaringClass, annotationClass.toClassReference(module))
}

@ExperimentalAnvilApi
public fun AnnotationDescriptor.toAnnotationReference(
declaringClass: ClassReference?,
classReference: ClassReference,
): Descriptor {
return Descriptor(
annotation = this,
classReference = annotationClass.toClassReference(module),
classReference = classReference,
declaringClass = declaringClass,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.tschuchort.compiletesting.kspArgs
import com.tschuchort.compiletesting.kspWithCompilation
import com.tschuchort.compiletesting.symbolProcessorProviders
import dagger.internal.codegen.ComponentProcessor
import dagger.internal.codegen.KspComponentProcessor
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.config.JvmTarget
import java.io.File
Expand All @@ -39,7 +40,7 @@ public class AnvilCompilation internal constructor(
@Suppress("SuspiciousCollectionReassignment")
@ExperimentalAnvilApi
public fun configureAnvil(
enableDaggerAnnotationProcessor: Boolean = false,
daggerAnnotationProcessingMode: DaggerAnnotationProcessingMode = DaggerAnnotationProcessingMode.NONE,
generateDaggerFactories: Boolean = false,
generateDaggerFactoriesOnly: Boolean = false,
disableComponentMerging: Boolean = false,
Expand All @@ -59,8 +60,25 @@ public class AnvilCompilation internal constructor(
// Deprecation tracked in https://github.com/square/anvil/issues/672
@Suppress("DEPRECATION")
componentRegistrars = listOf(anvilComponentRegistrar)
if (enableDaggerAnnotationProcessor) {
annotationProcessors = listOf(ComponentProcessor(), AutoAnnotationProcessor())

when (daggerAnnotationProcessingMode) {
DaggerAnnotationProcessingMode.KAPT -> {
annotationProcessors = listOf(ComponentProcessor(), AutoAnnotationProcessor())
}
DaggerAnnotationProcessingMode.KSP -> {
@Suppress("invisible_reference", "invisible_member")
symbolProcessorProviders +=
listOf(
com.squareup.anvil.compiler.ksp.MergeComponentSymbolProcessor.Provider(),
KspComponentProcessor.Provider(),
)
// Run KSP in a single-pass
// https://kotlinlang.slack.com/archives/C013BA8EQSE/p1639462548225400?thread_ts=1639433474.224900&cid=C013BA8EQSE
kspWithCompilation = true
}
DaggerAnnotationProcessingMode.NONE -> {
// Do nothing
}
}

val anvilCommandLineProcessor = AnvilCommandLineProcessor()
Expand Down Expand Up @@ -104,10 +122,13 @@ public class AnvilCompilation internal constructor(
is Ksp -> {
symbolProcessorProviders += buildList {
addAll(
ServiceLoader.load(
SymbolProcessorProvider::class.java,
SymbolProcessorProvider::class.java.classLoader,
),
ServiceLoader
.load(
SymbolProcessorProvider::class.java,
SymbolProcessorProvider::class.java.classLoader,
)
// Exclude the Dagger KSP processor, we have special handling for that as we decorate it
.filterNot { it is KspComponentProcessor.Provider },
)
addAll(mode.symbolProcessorProviders)
}
Expand Down Expand Up @@ -219,6 +240,13 @@ public class AnvilCompilation internal constructor(
}
}

/** Available Dagger annotation processing modes. */
enum class DaggerAnnotationProcessingMode {
KAPT,
KSP,
NONE,
}

/**
* Helpful for testing code generators in unit tests end to end.
*
Expand All @@ -229,7 +257,7 @@ public class AnvilCompilation internal constructor(
@ExperimentalAnvilApi
public fun compileAnvil(
@Language("kotlin") vararg sources: String,
enableDaggerAnnotationProcessor: Boolean = false,
daggerAnnotationProcessingMode: DaggerAnnotationProcessingMode = DaggerAnnotationProcessingMode.NONE,
generateDaggerFactories: Boolean = false,
generateDaggerFactoriesOnly: Boolean = false,
disableComponentMerging: Boolean = false,
Expand All @@ -243,7 +271,8 @@ public fun compileAnvil(
jvmTarget: JvmTarget? = null,
block: JvmCompilationResult.() -> Unit = { },
): JvmCompilationResult {
return AnvilCompilation()
val compilation = AnvilCompilation()
return compilation
.apply {
kotlinCompilation.apply {
this.allWarningsAsErrors = allWarningsAsErrors
Expand All @@ -265,13 +294,15 @@ public fun compileAnvil(
}
}
.configureAnvil(
enableDaggerAnnotationProcessor = enableDaggerAnnotationProcessor,
daggerAnnotationProcessingMode = daggerAnnotationProcessingMode,
generateDaggerFactories = generateDaggerFactories,
generateDaggerFactoriesOnly = generateDaggerFactoriesOnly,
disableComponentMerging = disableComponentMerging,
enableExperimentalAnvilApis = enableExperimentalAnvilApis,
mode = mode,
)
.compile(*sources)
.also(block)
.also { result ->
result.block()
}
}
8 changes: 7 additions & 1 deletion compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation(project(":compiler-api"))
implementation(project(":compiler-utils"))
implementation(libs.dagger2)
implementation(libs.dagger2.compiler)
implementation(libs.jsr250)
implementation(libs.kotlinpoet)
implementation(libs.kotlinpoet.ksp)
Expand All @@ -47,16 +48,21 @@ dependencies {
compileOnly(libs.kotlin.compiler)
compileOnly(libs.ksp.compilerPlugin)
compileOnly(libs.ksp.api)
// just for reference to get underlying APIs
compileOnly(libs.ksp.compilerPlugin)

kapt(libs.auto.service.processor)

testImplementation(testFixtures(project(":compiler-utils")))
testImplementation(libs.dagger2.compiler)
testImplementation(libs.kotlin.annotationProcessingEmbeddable)
testImplementation(libs.kotlin.compileTesting)
testImplementation(libs.kotlin.compileTesting.ksp)
testImplementation(libs.ksp.compilerPlugin)
testImplementation(libs.kotlin.compiler)
testImplementation(libs.kotlin.test)
testImplementation(libs.ksp.api)
testImplementation(libs.ksp.compilerPlugin)
testImplementation(libs.truth)
// Force latest guava version for alignment with dagger's versions
testImplementation(libs.guava)
}
Loading
Loading