Repository: square/anvil
Pull request #53 (2020-07-31 20:11:33)

Caching is hard, stop it.


Patch:
diff --git a/compiler/src/main/java/com/squareup/anvil/compiler/AnvilComponentRegistrar.kt b/compiler/src/main/java/com/squareup/anvil/compiler/AnvilComponentRegistrar.kt
index 51822c24..ed35c1e2 100644
--- a/compiler/src/main/java/com/squareup/anvil/compiler/AnvilComponentRegistrar.kt
+++ b/compiler/src/main/java/com/squareup/anvil/compiler/AnvilComponentRegistrar.kt
@@ -26,17 +26,14 @@ class AnvilComponentRegistrar : ComponentRegistrar {
     configuration: CompilerConfiguration
   ) {
     val sourceGenFolder = File(configuration.getNotNull(srcGenDirKey))
+    val scanner = ClassScanner()
 
     val codeGenerationExtension = CodeGenerationExtension(
         codeGenDir = sourceGenFolder,
         codeGenerators = listOf(
             ContributesToGenerator(),
             ContributesBindingGenerator(),
-
-            // Note that this class scanner cannot be shared with the other extensions. These
-            // code generators create new code and the ClassScanner maintains a cache that would
-            // be invalid.
-            BindingModuleGenerator(ClassScanner())
+            BindingModuleGenerator(scanner)
         )
     )
 
@@ -52,7 +49,6 @@ class AnvilComponentRegistrar : ComponentRegistrar {
         project, codeGenerationExtension
     )
 
-    val scanner = ClassScanner()
     SyntheticResolveExtension.registerExtension(
         project, InterfaceMerger(scanner)
     )
diff --git a/compiler/src/main/java/com/squareup/anvil/compiler/ClassScanner.kt b/compiler/src/main/java/com/squareup/anvil/compiler/ClassScanner.kt
index e37e2c1e..9570edf5 100644
--- a/compiler/src/main/java/com/squareup/anvil/compiler/ClassScanner.kt
+++ b/compiler/src/main/java/com/squareup/anvil/compiler/ClassScanner.kt
@@ -9,8 +9,6 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
 
 internal class ClassScanner {
 
-  private val cache = mutableMapOf<String, List<ClassDescriptor>>()
-
   /**
    * Returns a list of classes from the dependency graph annotated with `@ContributesTo`. Note that
    * the list includes inner classes already.
@@ -20,28 +18,24 @@ internal class ClassScanner {
     packageName: String,
     annotation: FqName
   ): List<ClassDescriptor> {
-    val key = packageName + annotation.asString()
-
-    return cache.getOrPut(key) {
-      val packageDescriptor = module.getPackage(FqName(packageName))
-      generateSequence(packageDescriptor.subPackages()) { subPackages ->
-        subPackages
-            .flatMap { it.subPackages() }
-            .ifEmpty { null }
-      }
-          .flatMap { it.asSequence() }
-          .flatMap {
-            it.memberScope.getContributedDescriptors(DescriptorKindFilter.VALUES)
-                .asSequence()
-          }
-          .filterIsInstance<PropertyDescriptor>()
-          .map {
-            it.type.argumentType()
-                .classDescriptorForType()
-          }
-          .filter { it.annotationOrNull(annotation) != null }
-          .toList()
+    val packageDescriptor = module.getPackage(FqName(packageName))
+    return generateSequence(packageDescriptor.subPackages()) { subPackages ->
+      subPackages
+          .flatMap { it.subPackages() }
+          .ifEmpty { null }
     }
+        .flatMap { it.asSequence() }
+        .flatMap {
+          it.memberScope.getContributedDescriptors(DescriptorKindFilter.VALUES)
+              .asSequence()
+        }
+        .filterIsInstance<PropertyDescriptor>()
+        .map {
+          it.type.argumentType()
+              .classDescriptorForType()
+        }
+        .filter { it.annotationOrNull(annotation) != null }
+        .toList()
   }
 }
 


Test patch:
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt
index 227b5a97..5551713b 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/InterfaceMergerTest.kt
@@ -6,7 +6,6 @@ import com.squareup.anvil.annotations.MergeSubcomponent
 import com.squareup.anvil.annotations.compat.MergeInterfaces
 import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.COMPILATION_ERROR
 import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.INTERNAL_ERROR
-import org.junit.Ignore
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.junit.runners.Parameterized
@@ -401,7 +400,6 @@ class InterfaceMergerTest(
     }
   }
 
-  @Ignore("Need to investigate. Somehow these compilations are successful now.")
   @Test fun `inner interfaces in merged component fail`() {
     // They could cause errors while compiling code when adding our contributed super classes.
     compile(
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt
index 8fb0a927..0300c1ac 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/ModuleMergerTest.kt
@@ -7,7 +7,6 @@ import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.COMPILATION_ERRO
 import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.INTERNAL_ERROR
 import dagger.Component
 import dagger.Subcomponent
-import org.junit.Ignore
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.junit.runners.Parameterized
@@ -535,7 +534,6 @@ class ModuleMergerTest(
     }
   }
 
-  @Ignore("Need to investigate. Somehow these compilations are successful now.")
   @Test fun `inner modules in a merged component fail`() {
     compile(
         """