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

Fix false positive with anvil an UnusedModules #246

Merged
merged 1 commit into from
Sep 2, 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
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dagger = "2.48.1"
junit_jupiter = "5.11.0"
kctfork = "0.5.1"
ksp = "1.9.25-1.0.20"
anvil = "2.4.9"

[libraries]
android_gradle_plugin = { module = "com.android.tools.build:gradle", version = "8.0.0" }
Expand All @@ -20,7 +21,8 @@ ksp_gradle_plugin = { module = "com.google.devtools.ksp:symbol-processing-gradle
ksp_api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
kotlin_gradle_plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
ktlint = { module = "com.pinterest.ktlint:ktlint-cli", version = "1.3.1" }
anvil_annotations = { module = "com.squareup.anvil:annotations", version = "2.4.9" }
anvil_annotations = { module = "com.squareup.anvil:annotations", version.ref = "anvil" }
anvil_compiler = { module = "com.squareup.anvil:compiler", version.ref = "anvil" }

[plugins]
kotlin_plugin = { id = "org.jetbrains.kotlin", version.ref = "kotlin" }
Expand Down
1 change: 1 addition & 0 deletions lightsaber/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
testImplementation(libs.kctfork.core)
testImplementation(libs.kctfork.ksp)
testImplementation(libs.anvil.annotations)
testImplementation(libs.anvil.compiler)
}

configure<com.diffplug.gradle.spotless.SpotlessExtension> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ internal fun checkUnusedModules(
codePosition = { component.getModulesCodePosition(daggerProcessingEnv) },
)
}
.map { (errorMessage, codePosition) ->
.filterNot { it.module.toString().startsWith("anvil.module.") }
.map {
Finding(
errorMessage,
codePosition,
it.message,
it.codePosition,
component.componentPath().currentComponent()::hasSuppress,
)
}
Expand All @@ -43,16 +44,16 @@ private fun getErrorMessages(
daggerProcessingEnv: DaggerProcessingEnv,
codePosition: () -> CodePosition,
path: List<String> = emptyList(),
): List<Pair<String, CodePosition>> {
): List<UnusedModuleFinding> {
return buildList {
if (!used.contains(node.value)) {
val usedChildren = findUsedChildren(used, node)
add(
generateMessage(
usedChildren.map { it.value },
UnusedModuleFinding(
generateMessage(usedChildren.map { it.value }, node.value, path),
codePosition.invoke(),
node.value,
path,
) to codePosition.invoke(),
),
)
val newPath = path.plus(node.value.toString())
addAll(
Expand Down Expand Up @@ -83,6 +84,8 @@ private fun getErrorMessages(
}
}

data class UnusedModuleFinding(val message: String, val codePosition: CodePosition, val module: Module)

private fun generateMessage(
usedChildren: List<Module>,
node: Module,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,34 @@ internal class UnusedModulesKtTest {
)
}

@ParameterizedTest
@CsvSource("kapt,4,29")
fun anvil(
@ConvertWith(CompilerArgumentConverter::class) compiler: KotlinCompiler,
line: Int,
column: Int?,
) {
val component = createSource(
"""
package test

import com.squareup.anvil.annotations.MergeComponent
import javax.inject.Singleton

@MergeComponent(Singleton::class, modules = [MyModule::class])
interface MyComponent
""".trimIndent(),
)

val compilation = compiler.compile(component, module)

compilation.assertUnusedModules(
message = "The @Module `test.MyModule` is not used.",
line = line,
column = column,
)
}

@ParameterizedTest
@CsvSource("kapt,3,29", "ksp,5,")
fun includeModules1(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ package schwarz.it.lightsaber.utils

import com.google.common.truth.Truth.assertThat
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.squareup.anvil.compiler.AnvilCommandLineProcessor
import com.squareup.anvil.compiler.AnvilComponentRegistrar
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.kspProcessorOptions
import com.tschuchort.compiletesting.kspSourcesDir
Expand Down Expand Up @@ -36,6 +39,35 @@ internal class KaptKotlinCompiler(
)
kaptArgs = getLightsaberArguments(*rules)
verbose = false

@Suppress("DEPRECATION")
componentRegistrars = listOf(AnvilComponentRegistrar())

val anvilCommandLineProcessor = AnvilCommandLineProcessor()
commandLineProcessors = listOf(anvilCommandLineProcessor)

pluginOptions += listOf(
PluginOption(
pluginId = anvilCommandLineProcessor.pluginId,
optionName = "disable-component-merging",
optionValue = "false",
),
PluginOption(
pluginId = anvilCommandLineProcessor.pluginId,
optionName = "src-gen-dir",
optionValue = File(workingDir, "build/anvil").absolutePath,
),
PluginOption(
pluginId = anvilCommandLineProcessor.pluginId,
optionName = "generate-dagger-factories",
optionValue = "false",
),
PluginOption(
pluginId = anvilCommandLineProcessor.pluginId,
optionName = "generate-dagger-factories-only",
optionValue = "false",
),
)
}

override fun compile(vararg sourceFiles: SourceFile): CompilationResult {
Expand Down