square / anvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.
Apache License 2.0
1.29k stars 76 forks source link

treat any input `KtFile`s as changed/invalidated #922

Closed RBusarow closed 4 months ago

RBusarow commented 4 months ago

fixes #898

By "input KtFiles" I'm referring to the files: List<KtFile> parameters in CodeGeneratorExtension.doAnalysis(...) and CodeGeneratorExtension.onAnalysisCompleted(...).

For any given compilation, on the very first round of analysis/generation, the files parameter will roughly correspond to the incremental files from Gradle's InputChanges instance.

We should treat these files as changed because:

  1. If they're also in our internal cache, with the same contents (MD5 hash), then there must be some reason that Gradle decided to pass them as input again and the compiler agreed. This is most likely because of a classpath change, either in a dependency project or an external library.

    In this case, since Anvil uses classpath discovery to generate binding modules and merged interfaces, its expected output can easily change even if the source files in that module haven't.

  2. If they come from InputChanges, they're changed by definition.

  3. If they're input files, then we'll be generating code for them again anyway and it will be up-to-date.

With this change, a source file is considered to be unchanged (eligible for restoration) if it exists on the disk and in the cache with the same content hash and it wasn't passed as an input file. Since they aren't inputs, they won't be passed to the code generators, and we need to restore any downstream generated files from the cache.