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

removing all hidden KSP logic #1080

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
# Expressions in Github actions are limited. If there would be an if expression, then we
# wouldn't need to duplicate the next step and depending on the OS enable / disable them.
- name: Test on Windows
run: ./gradlew.bat assemble test --no-build-cache --no-daemon --stacktrace -Doverride_config-fullTestRun=false -Doverride_config-includeKspTests=false
run: ./gradlew.bat assemble test --no-build-cache --no-daemon --stacktrace -Doverride_config-fullTestRun=false

- name: Upload Test Results
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.squareup.anvil.conventions

import com.dropbox.gradle.plugins.dependencyguard.DependencyGuardPluginExtension
import com.rickbusarow.kgx.pluginId
import com.rickbusarow.kgx.withJavaTestFixturesPlugin
import com.squareup.anvil.conventions.utils.isInAnvilRootBuild
import com.squareup.anvil.conventions.utils.libs
import org.gradle.api.Project
Expand All @@ -23,8 +24,14 @@ open class LibraryPlugin : BasePlugin() {

private fun configureDependencyGuard(target: Project) {
target.plugins.apply(target.libs.plugins.dependencyGuard.pluginId)
target.extensions

val dependencyGuard = target.extensions
.getByType(DependencyGuardPluginExtension::class.java)
.configuration("runtimeClasspath")

dependencyGuard.configuration("runtimeClasspath")

target.plugins.withJavaTestFixturesPlugin {
dependencyGuard.configuration("testFixturesRuntimeClasspath")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This corresponds to a published artifact, so we should be tracking its classpath like the others.

}
}
}
2 changes: 0 additions & 2 deletions compiler-api/api/compiler-api.api
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
public final class com/squareup/anvil/compiler/api/AnalysisBackend : java/lang/Enum {
public static final field EMBEDDED Lcom/squareup/anvil/compiler/api/AnalysisBackend;
public static final field KSP Lcom/squareup/anvil/compiler/api/AnalysisBackend;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/squareup/anvil/compiler/api/AnalysisBackend;
public static fun values ()[Lcom/squareup/anvil/compiler/api/AnalysisBackend;
Expand Down Expand Up @@ -60,7 +59,6 @@ public final class com/squareup/anvil/compiler/api/CodeGeneratorKt {

public final class com/squareup/anvil/compiler/api/ComponentMergingBackend : java/lang/Enum {
public static final field IR Lcom/squareup/anvil/compiler/api/ComponentMergingBackend;
public static final field KSP Lcom/squareup/anvil/compiler/api/ComponentMergingBackend;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/squareup/anvil/compiler/api/ComponentMergingBackend;
public static fun values ()[Lcom/squareup/anvil/compiler/api/ComponentMergingBackend;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ package com.squareup.anvil.compiler.api
public enum class AnalysisBackend {
/** Anvil runs as a direct compiler plugin inside compileKotlin tasks. */
EMBEDDED,

/** Anvil runs as a Kotlin Symbol Processor (KSP). */
KSP,
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public interface AnvilContext {
* These models could be generated by any of:
* - Anvil via the [generateFactories] option
* - Dagger via KAPT
* - Dagger via KSP
*/
public val willHaveDaggerFactories: Boolean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ package com.squareup.anvil.compiler.api
public enum class ComponentMergingBackend {
/** Component merging runs as an IR plugin during kapt stub generation. */
IR,

/** Component merging runs as a Kotlin Symbol Processor (KSP) with dagger KSP. */
KSP,
}
2 changes: 0 additions & 2 deletions compiler-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ dependencies {
implementation(libs.inject)

testFixturesApi(libs.kotlin.compileTesting)
testFixturesApi(libs.kotlin.compileTesting.ksp)
testFixturesImplementation(project(":compiler"))
testFixturesImplementation(libs.dagger2.compiler)
testFixturesImplementation(libs.dagger2)
Expand All @@ -48,7 +47,6 @@ dependencies {
compileOnly(libs.dagger2.compiler)
compileOnly(libs.junit)
compileOnly(libs.kotlin.compileTesting)
compileOnly(libs.kotlin.compileTesting.ksp)
compileOnly(libs.truth)
}
}
45 changes: 45 additions & 0 deletions compiler-utils/dependencies/testFixturesRuntimeClasspath.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
com.google.auto.service:auto-service-annotations:1.1.1
com.google.auto.value:auto-value-annotations:1.11.0
com.google.auto.value:auto-value:1.11.0
com.google.code.findbugs:jsr305:3.0.2
com.google.dagger:dagger-compiler:2.52
com.google.dagger:dagger-spi:2.52
com.google.dagger:dagger:2.52
com.google.devtools.ksp:symbol-processing-api:1.9.24-1.0.20
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing KSP from our own dependencies, we removed:

com.google.devtools.ksp:symbol-processing-aa-embeddable:2.0.0-1.0.22
com.google.devtools.ksp:symbol-processing-api:2.0.0-1.0.22
com.google.devtools.ksp:symbol-processing-common-deps:2.0.0-1.0.22
com.google.devtools.ksp:symbol-processing:2.0.0-1.0.22
com.squareup:kotlinpoet-ksp:1.18.1
dev.zacsweers.kctfork:ksp:0.5.1

This com.google.devtools.ksp:symbol-processing-api:1.9.24-1.0.20 dependency comes from the dagger-compiler dependency.

com.google.errorprone:error_prone_annotations:2.28.0
com.google.errorprone:javac-shaded:9-dev-r4023-3
com.google.googlejavaformat:google-java-format:1.5
com.google.guava:failureaccess:1.0.2
com.google.guava:guava:33.2.1-android
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
com.google.truth:truth:1.4.4
com.squareup.okio:okio-jvm:3.9.0
com.squareup.okio:okio:3.9.0
com.squareup:javapoet:1.13.0
com.squareup:kotlinpoet-jvm:1.18.1
com.squareup:kotlinpoet:1.18.1
dev.zacsweers.kctfork:core:0.5.1
io.github.classgraph:classgraph:4.8.174
jakarta.inject:jakarta.inject-api:2.0.1
javax.annotation:jsr250-api:1.0
javax.inject:javax.inject:1
junit:junit:4.13.2
net.ltgt.gradle.incap:incap:0.2
org.checkerframework:checker-compat-qual:2.5.5
org.checkerframework:checker-qual:3.42.0
org.hamcrest:hamcrest-core:1.3
org.jetbrains.intellij.deps:trove4j:1.0.20200330
org.jetbrains.kotlin:kotlin-annotation-processing-compiler:2.0.0
org.jetbrains.kotlin:kotlin-annotation-processing-embeddable:2.0.0
org.jetbrains.kotlin:kotlin-bom:2.0.21
org.jetbrains.kotlin:kotlin-compiler-embeddable:2.0.21
org.jetbrains.kotlin:kotlin-daemon-embeddable:2.0.21
org.jetbrains.kotlin:kotlin-reflect:2.0.21
org.jetbrains.kotlin:kotlin-script-runtime:2.0.21
org.jetbrains.kotlin:kotlin-stdlib-jdk7:2.0.21
org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.21
org.jetbrains.kotlin:kotlin-stdlib:2.0.21
org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.6.4
org.jetbrains:annotations:13.0
org.jspecify:jspecify:0.3.0
org.ow2.asm:asm:9.7
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,22 @@ package com.squareup.anvil.compiler.internal.testing

import com.google.auto.value.processor.AutoAnnotationProcessor
import com.google.common.truth.Truth.assertWithMessage
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.squareup.anvil.annotations.ExperimentalAnvilApi
import com.squareup.anvil.compiler.AnvilCommandLineProcessor
import com.squareup.anvil.compiler.AnvilComponentRegistrar
import com.squareup.anvil.compiler.internal.testing.AnvilCompilationMode.Embedded
import com.squareup.anvil.compiler.internal.testing.AnvilCompilationMode.Ksp
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.addPreviousResultToClasspath
import com.tschuchort.compiletesting.kspProcessorOptions
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 org.jetbrains.kotlin.config.LanguageVersion
import java.io.File
import java.io.OutputStream
import java.nio.file.Files
import java.util.Locale
import java.util.ServiceLoader

/**
* A simple API over a [KotlinCompilation] with extra configuration support for Anvil.
Expand Down Expand Up @@ -78,11 +70,6 @@ public class AnvilCompilation internal constructor(
optionName = "disable-component-merging",
optionValue = disableComponentMerging.toString(),
),
PluginOption(
pluginId = anvilCommandLineProcessor.pluginId,
optionName = "analysis-backend",
optionValue = mode.analysisBackend.name.lowercase(Locale.US),
),
PluginOption(
pluginId = anvilCommandLineProcessor.pluginId,
optionName = "ir-merges-file",
Expand Down Expand Up @@ -137,29 +124,6 @@ public class AnvilCompilation internal constructor(
),
)
}

is Ksp -> {
symbolProcessorProviders += buildList {
addAll(
ServiceLoader.load(
SymbolProcessorProvider::class.java,
SymbolProcessorProvider::class.java.classLoader,
)
// TODO for now, we don't want to run the dagger KSP processor while we're testing
// KSP. This will change when we start supporting dagger-KSP, at which point we can
// change this filter to be based on https://github.com/square/anvil/pull/713
.filterNot { it is KspComponentProcessor.Provider },
)
addAll(mode.symbolProcessorProviders)
}
// Run KSP embedded directly within this kotlinc invocation
kspWithCompilation = true
kspProcessorOptions["will-have-dagger-factories"] = generateDaggerFactories.toString()
kspProcessorOptions["generate-dagger-factories"] = generateDaggerFactories.toString()
kspProcessorOptions["generate-dagger-factories-only"] =
generateDaggerFactoriesOnly.toString()
kspProcessorOptions["disable-component-merging"] = disableComponentMerging.toString()
}
}

if (enableExperimentalAnvilApis) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.squareup.anvil.compiler.internal.testing

import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.squareup.anvil.compiler.api.AnalysisBackend
import com.squareup.anvil.compiler.api.AnalysisBackend.EMBEDDED
import com.squareup.anvil.compiler.api.AnalysisBackend.KSP
import com.squareup.anvil.compiler.api.CodeGenerator

// TODO: Repurpose this as a way to pass a spec into `compile(...)` in tests,
// instead of individual flags.
// This is left in place for now because it's already wired up everywhere.
public sealed class AnvilCompilationMode(public val analysisBackend: AnalysisBackend) {
public data class Embedded(
val codeGenerators: List<CodeGenerator> = emptyList(),
) : AnvilCompilationMode(EMBEDDED)
public data class Ksp(
val symbolProcessorProviders: List<SymbolProcessorProvider> = emptyList(),
) : AnvilCompilationMode(KSP)
}
4 changes: 1 addition & 3 deletions compiler/api/compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public final class com/squareup/anvil/compiler/AnvilComponentRegistrar : org/jet

public final class com/squareup/anvil/compiler/CommandLineOptions {
public static final field Companion Lcom/squareup/anvil/compiler/CommandLineOptions$Companion;
public synthetic fun <init> (ZZZZZLcom/squareup/anvil/compiler/api/AnalysisBackend;Lcom/squareup/anvil/compiler/api/ComponentMergingBackend;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getBackend ()Lcom/squareup/anvil/compiler/api/AnalysisBackend;
public final fun getComponentMergingBackend ()Lcom/squareup/anvil/compiler/api/ComponentMergingBackend;
public synthetic fun <init> (ZZZZZLkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getDisableComponentMerging ()Z
public final fun getGenerateFactories ()Z
public final fun getGenerateFactoriesOnly ()Z
Expand Down
6 changes: 0 additions & 6 deletions compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ buildConfig {
useKotlinOutput { topLevelConstants = true }

buildConfigField("boolean", "FULL_TEST_RUN", libs.versions.config.fullTestRun.get())
buildConfigField("boolean", "INCLUDE_KSP_TESTS", libs.versions.config.includeKspTests.get())
}

conventions {
Expand Down Expand Up @@ -41,12 +40,9 @@ dependencies {
implementation(libs.dagger2)
implementation(libs.jsr250)
implementation(libs.kotlinpoet)
implementation(libs.kotlinpoet.ksp)

compileOnly(libs.auto.service.annotations)
compileOnly(libs.kotlin.compiler)
compileOnly(libs.ksp.compilerPlugin)
compileOnly(libs.ksp.api)

kapt(libs.auto.service.processor)

Expand All @@ -58,11 +54,9 @@ dependencies {
testImplementation(libs.kotest.assertions.core.jvm)
testImplementation(libs.kotlin.annotationProcessingEmbeddable)
testImplementation(libs.kotlin.compileTesting)
testImplementation(libs.kotlin.compileTesting.ksp)
testImplementation(libs.kotlin.compiler)
testImplementation(libs.kotlin.test)
testImplementation(libs.kotlin.reflect)
testImplementation(libs.ksp.compilerPlugin)
testImplementation(libs.truth)

testRuntimeOnly(libs.kotest.assertions.core.jvm)
Expand Down
1 change: 0 additions & 1 deletion compiler/dependencies/runtimeClasspath.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
com.google.dagger:dagger:2.52
com.squareup:kotlinpoet-jvm:1.18.1
com.squareup:kotlinpoet-ksp:1.18.1
com.squareup:kotlinpoet:1.18.1
jakarta.inject:jakarta.inject-api:2.0.1
javax.annotation:jsr250-api:1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.squareup.anvil.compiler

import com.google.auto.service.AutoService
import com.squareup.anvil.compiler.api.AnalysisBackend
import com.squareup.anvil.compiler.api.ComponentMergingBackend
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
Expand Down Expand Up @@ -49,14 +47,6 @@ internal const val willHaveDaggerFactoriesName = "will-have-dagger-factories"
internal val willHaveDaggerFactoriesKey =
CompilerConfigurationKey.create<Boolean>("anvil $willHaveDaggerFactoriesName")

internal const val analysisBackendName = "analysis-backend"
internal val analysisBackendKey =
CompilerConfigurationKey.create<String>("anvil $analysisBackendName")

internal const val mergingBackendName = "merging-backend"
internal val mergingBackendKey =
CompilerConfigurationKey.create<String>("anvil $mergingBackendName")

/**
* Parses arguments from the Gradle plugin for the compiler plugin.
*/
Expand Down Expand Up @@ -141,20 +131,6 @@ public class AnvilCommandLineProcessor : CommandLineProcessor {
required = false,
allowMultipleOccurrences = false,
),
CliOption(
optionName = analysisBackendName,
valueDescription = AnalysisBackend.entries.joinToString("|", "<", ">"),
description = "Controls whether Anvil analysis is running as an embedded plugin or as KSP.",
required = false,
allowMultipleOccurrences = false,
),
CliOption(
optionName = mergingBackendName,
valueDescription = ComponentMergingBackend.entries.joinToString("|", "<", ">"),
description = "Controls whether module merging is running as an IR plugin or as KSP.",
required = false,
allowMultipleOccurrences = false,
),
)

override fun processOption(
Expand Down Expand Up @@ -183,8 +159,6 @@ public class AnvilCommandLineProcessor : CommandLineProcessor {

trackSourceFilesName ->
configuration.put(trackSourceFilesKey, value.toBoolean())

analysisBackendName -> configuration.put(analysisBackendKey, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ package com.squareup.anvil.compiler
import com.google.auto.service.AutoService
import com.squareup.anvil.annotations.ExperimentalAnvilApi
import com.squareup.anvil.compiler.CommandLineOptions.Companion.commandLineOptions
import com.squareup.anvil.compiler.api.AnalysisBackend
import com.squareup.anvil.compiler.api.CodeGenerator
import com.squareup.anvil.compiler.api.ComponentMergingBackend
import com.squareup.anvil.compiler.codegen.CodeGenerationExtension
import com.squareup.anvil.compiler.codegen.ContributesSubcomponentHandlerGenerator
import com.squareup.anvil.compiler.codegen.incremental.BaseDir
Expand Down Expand Up @@ -50,25 +48,15 @@ public class AnvilComponentRegistrar : ComponentRegistrar {
val mergingEnabled =
!commandLineOptions.generateFactoriesOnly && !commandLineOptions.disableComponentMerging
if (mergingEnabled) {
if (commandLineOptions.componentMergingBackend == ComponentMergingBackend.IR) {
IrGenerationExtension.registerExtension(
project,
IrContributionMerger(
classScanner = scanner,
moduleDescriptorFactory = moduleDescriptorFactory,
trackSourceFiles = trackSourceFiles,
irMergesFile = irMergesFile,
),
)
} else {
// TODO in dagger-ksp support
}
}

// Everything below this point is only when running in embedded compilation mode. If running in
// KSP, there's nothing else to do.
if (commandLineOptions.backend != AnalysisBackend.EMBEDDED) {
return
IrGenerationExtension.registerExtension(
project,
IrContributionMerger(
classScanner = scanner,
moduleDescriptorFactory = moduleDescriptorFactory,
trackSourceFiles = trackSourceFiles,
irMergesFile = irMergesFile,
),
)
}

val sourceGenFolder = configuration.getNotNull(srcGenDirKey)
Expand Down
Loading
Loading