@@ -69,6 +69,9 @@ internal class CodeGenerationExtension(
6969 val anvilModule = RealAnvilModuleDescriptor (module)
7070 anvilModule.addFiles(files)
7171
72+ val (privateCodeGenerators, nonPrivateCodeGenerators) =
73+ codeGenerators.partition { it is PrivateCodeGenerator }
74+
7275 fun Collection<GeneratedFile>.toKtFile (): Collection <KtFile > {
7376 return this
7477 .mapNotNull { (file, content) ->
@@ -95,19 +98,19 @@ internal class CodeGenerationExtension(
9598 codeGenerator.flush(codeGenDir, anvilModule).toKtFile()
9699 }
97100
98- var newFiles = codeGenerators.generateCode(files)
101+ var newFiles = nonPrivateCodeGenerators.generateCode(files)
102+
99103 while (newFiles.isNotEmpty()) {
100104 // Parse the KtFile for each generated file. Then feed the code generators with the new
101105 // parsed files until no new files are generated.
102- newFiles = codeGenerators .generateCode(newFiles)
106+ newFiles = nonPrivateCodeGenerators .generateCode(newFiles)
103107 }
104108
105- val flushedFiles = codeGenerators .flush()
109+ nonPrivateCodeGenerators .flush()
106110
107- // The contract is that PrivateCodeGenerators are called one last time after flush().
108- codeGenerators
109- .filterIsInstance<PrivateCodeGenerator >()
110- .generateCode(flushedFiles)
111+ // PrivateCodeGenerators don't impact other code generators. Therefore, they can be called a
112+ // single time at the end.
113+ privateCodeGenerators.generateCode(anvilModule.allFiles)
111114
112115 // This restarts the analysis phase and will include our files.
113116 return RetryWithAdditionalRoots (
0 commit comments