From 111a2ece72b1ab2f4392ee17b0ff132888343b72 Mon Sep 17 00:00:00 2001 From: Ivan Gavrilovic Date: Fri, 26 Jun 2020 23:02:06 +0100 Subject: [PATCH] KAPT: Fix error reporting When incremental analysis is unable to run, handle failure gracefully. More info will be provided by compiler diagnostics. Fixes KT-36302 --- .../kotlin/kapt3/base/annotationProcessing.kt | 42 ++++++++++++------- .../kotlin/kapt3/base/incremental/cache.kt | 4 +- .../kapt3/base/incremental/javacVisitors.kt | 24 +++++++---- .../incremental/InheritedAnnotationTest.kt | 2 +- .../incremental/ReferencedContantsTest.kt | 2 +- .../TestComplexIncrementalAptCache.kt | 2 +- .../TestSimpleIncrementalAptCache.kt | 2 +- 7 files changed, 50 insertions(+), 28 deletions(-) diff --git a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/annotationProcessing.kt b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/annotationProcessing.kt index 87ccacec835..9f8b4c35dd3 100644 --- a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/annotationProcessing.kt +++ b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/annotationProcessing.kt @@ -13,10 +13,9 @@ import com.sun.tools.javac.processing.JavacFiler import com.sun.tools.javac.processing.JavacProcessingEnvironment import com.sun.tools.javac.tree.JCTree import org.jetbrains.kotlin.base.kapt3.KaptFlag -import org.jetbrains.kotlin.kapt3.base.incremental.GeneratedTypesTaskListener -import org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor -import org.jetbrains.kotlin.kapt3.base.incremental.MentionedTypesTaskListener +import org.jetbrains.kotlin.kapt3.base.incremental.* import org.jetbrains.kotlin.kapt3.base.util.KaptBaseError +import org.jetbrains.kotlin.kapt3.base.util.KaptLogger import org.jetbrains.kotlin.kapt3.base.util.isJava9OrLater import org.jetbrains.kotlin.kapt3.base.util.measureTimeMillisWithResult import java.io.File @@ -84,23 +83,14 @@ fun KaptContext.doAnnotationProcessing( throw KaptBaseError(KaptBaseError.Kind.EXCEPTION, e.cause ?: e) } - cacheManager?.updateCache(processors) + cacheManager?.updateCache(processors, sourcesStructureListener?.failureReason != null) sourcesStructureListener?.let { if (logger.isVerbose) { logger.info("Analyzing sources structure took ${it.time}[ms].") } } - if (cacheManager != null) { - val missingIncrementalSupport = processors.filter { it.isMissingIncrementalSupport() } - if (missingIncrementalSupport.isNotEmpty()) { - val nonIncremental = missingIncrementalSupport.map { "${it.processorName} (${it.incrementalSupportType})" } - logger.warn( - "Incremental annotation processing requested, but support is disabled because the following " + - "processors are not incremental: ${nonIncremental.joinToString()}." - ) - } - } + reportIfRunningNonIncrementally(sourcesStructureListener, cacheManager, logger, processors) val log = compilerAfterAP.log @@ -138,6 +128,30 @@ private fun showProcessorTimings(wrappedProcessors: List, logg } } +private fun reportIfRunningNonIncrementally( + listener: MentionedTypesTaskListener?, + cacheManager: JavaClassCacheManager?, + logger: KaptLogger, + processors: List +) { + listener ?: return + cacheManager ?: return + + listener.failureReason?.let { failure -> + logger.warn("\n$failure") + return + } + + val missingIncrementalSupport = processors.filter { it.isMissingIncrementalSupport() } + if (missingIncrementalSupport.isNotEmpty()) { + val nonIncremental = missingIncrementalSupport.map { "${it.processorName} (${it.incrementalSupportType})" } + logger.warn( + "Incremental annotation processing requested, but support is disabled because the following " + + "processors are not incremental: ${nonIncremental.joinToString()}." + ) + } +} + private class ProcessorWrapper(private val delegate: IncrementalProcessor) : Processor by delegate { private var initTime: Long = 0 private val roundTime = mutableListOf() diff --git a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/cache.kt b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/cache.kt index 3ce5a245d29..ce2d1e999b8 100644 --- a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/cache.kt +++ b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/cache.kt @@ -18,8 +18,8 @@ class JavaClassCacheManager(val file: File) : Closeable { private var closed = false - fun updateCache(processors: List) { - if (!aptCache.updateCache(processors)) { + fun updateCache(processors: List, failedToAnalyzeSources: Boolean) { + if (failedToAnalyzeSources || !aptCache.updateCache(processors)) { javaCache.invalidateAll() } } diff --git a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/javacVisitors.kt b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/javacVisitors.kt index 194533d62cc..28288c05928 100644 --- a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/javacVisitors.kt +++ b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/incremental/javacVisitors.kt @@ -22,24 +22,32 @@ class MentionedTypesTaskListener( ) : TaskListener { var time = 0L + var failureReason: String? = null + override fun started(e: TaskEvent) { // do nothing, we just process on finish } override fun finished(e: TaskEvent) { + if (failureReason != null) return // stop processing if we failed to analyze some sources + if (e.kind != TaskEvent.Kind.ENTER || cache.isAlreadyProcessed(e.sourceFile.toUri())) return - val l = System.currentTimeMillis() - val compilationUnit = e.compilationUnit + try { + val l = System.currentTimeMillis() + val compilationUnit = e.compilationUnit - val structure = SourceFileStructure(e.sourceFile.toUri()) + val structure = SourceFileStructure(e.sourceFile.toUri()) - val treeVisitor = TypeTreeVisitor(elementUtils, trees, compilationUnit, structure) - compilationUnit.typeDecls.forEach { - it.accept(treeVisitor, Visibility.ABI) + val treeVisitor = TypeTreeVisitor(elementUtils, trees, compilationUnit, structure) + compilationUnit.typeDecls.forEach { + it.accept(treeVisitor, Visibility.ABI) + } + cache.addSourceStructure(structure) + time += System.currentTimeMillis() - l + } catch (t: Throwable) { + failureReason = "Running non-incrementally because analyzing ${e.sourceFile.toUri()} failed." } - cache.addSourceStructure(structure) - time += System.currentTimeMillis() - l } } diff --git a/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/InheritedAnnotationTest.kt b/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/InheritedAnnotationTest.kt index f025eba91c9..648f3400bf0 100644 --- a/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/InheritedAnnotationTest.kt +++ b/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/InheritedAnnotationTest.kt @@ -45,7 +45,7 @@ class TestInheritedAnnotation { listOf(processor), generatedSources ) { elementUtils, trees -> MentionedTypesTaskListener(cache.javaCache, elementUtils, trees) } - cache.updateCache(listOf(processor)) + cache.updateCache(listOf(processor), false) } } diff --git a/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/ReferencedContantsTest.kt b/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/ReferencedContantsTest.kt index 8797a5be48b..62829566294 100644 --- a/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/ReferencedContantsTest.kt +++ b/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/ReferencedContantsTest.kt @@ -48,7 +48,7 @@ class ReferencedConstantsTest { generatedSources, listOf(compiledClasses) ) { elements, trees -> MentionedTypesTaskListener(cache.javaCache, elements, trees) } - cache.updateCache(listOf(processor)) + cache.updateCache(listOf(processor), false) } } diff --git a/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/TestComplexIncrementalAptCache.kt b/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/TestComplexIncrementalAptCache.kt index b9194ec7cea..c7a71a056d0 100644 --- a/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/TestComplexIncrementalAptCache.kt +++ b/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/TestComplexIncrementalAptCache.kt @@ -47,7 +47,7 @@ class TestComplexIncrementalAptCache { listOf(processor), generatedSources ) { elements, trees -> MentionedTypesTaskListener(cache.javaCache, elements, trees) } - cache.updateCache(listOf(processor)) + cache.updateCache(listOf(processor), false) } } diff --git a/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/TestSimpleIncrementalAptCache.kt b/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/TestSimpleIncrementalAptCache.kt index 2c782135768..e7af1dbc5b6 100644 --- a/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/TestSimpleIncrementalAptCache.kt +++ b/plugins/kapt3/kapt3-base/test/org/jetbrains/kotlin/kapt3/base/incremental/TestSimpleIncrementalAptCache.kt @@ -72,6 +72,6 @@ class TestSimpleIncrementalAptCache { listOf(processor), generatedSources ) { elementUtils, trees -> MentionedTypesTaskListener(cache.javaCache, elementUtils, trees) } - cache.updateCache(listOf(processor)) + cache.updateCache(listOf(processor), false) } } \ No newline at end of file