[IC] Make CompilationTransaction to be able to close CachesManager
This commit is contained in:
committed by
Space Team
parent
3ed651a7a6
commit
25a3dcf3d1
+13
-35
@@ -160,6 +160,11 @@ abstract class IncrementalCompilerRunner<
|
||||
class Failed(val reason: BuildAttribute, val cause: Throwable) : ICResult
|
||||
}
|
||||
|
||||
private fun incrementalCompilationExceptionTransformer(t: Throwable): ICResult = when (t) {
|
||||
is CachesManagerCloseException -> ICResult.Failed(IC_FAILED_TO_CLOSE_CACHES, t)
|
||||
else -> throw t
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to compile incrementally and returns either [ICResult.Completed], [ICResult.RequiresRebuild], or [ICResult.Failed].
|
||||
*
|
||||
@@ -178,9 +183,12 @@ abstract class IncrementalCompilerRunner<
|
||||
}
|
||||
changedFiles as ChangedFiles.Known?
|
||||
|
||||
createTransaction().use { transaction ->
|
||||
return createTransaction().runWithin(::incrementalCompilationExceptionTransformer) { transaction ->
|
||||
val icContext = createIncrementalCompilationContext(projectDir, transaction)
|
||||
val caches = createCacheManager(icContext, args)
|
||||
val caches = createCacheManager(icContext, args).also {
|
||||
// this way we make the transaction to be responsible for closing the caches manager
|
||||
transaction.cachesManager = it
|
||||
}
|
||||
|
||||
fun compile(): ICResult {
|
||||
// Step 1: Get changed files
|
||||
@@ -235,41 +243,11 @@ abstract class IncrementalCompilerRunner<
|
||||
return ICResult.Completed(exitCode)
|
||||
}
|
||||
|
||||
fun closeCaches(caches: CacheManager, activeException: Throwable) {
|
||||
try {
|
||||
caches.close()
|
||||
} catch (e: Throwable) {
|
||||
activeException.addSuppressed(e)
|
||||
compile().also { icResult ->
|
||||
if (icResult is ICResult.Completed && icResult.exitCode == ExitCode.OK) {
|
||||
transaction.markAsSuccessful()
|
||||
}
|
||||
}
|
||||
|
||||
// Because `caches` is a Closeable resource, it is important to close them in both cases:
|
||||
// 1. in the event of an exception
|
||||
// 2. after a normal execution
|
||||
// Note: Historically, closing caches used to throw exceptions sometimes, so currently we want to collect those exceptions. In the
|
||||
// future, if closing caches is safe, we can simplify the code by using Kotlin's `Closable.use` function (similar to the code in
|
||||
// `compileNonIncrementally`).
|
||||
val icResult = try {
|
||||
compile()
|
||||
} catch (e: Throwable) {
|
||||
// Case 1 - Close the caches upon an exception
|
||||
closeCaches(caches, e)
|
||||
throw e
|
||||
}
|
||||
|
||||
// Case 2 - Close the caches after a normal execution
|
||||
try {
|
||||
caches.close()
|
||||
} catch (e: Throwable) {
|
||||
return ICResult.Failed(
|
||||
IC_FAILED_TO_CLOSE_CACHES,
|
||||
RuntimeException("Failed to close caches, previous ICResult `$icResult` was discarded", e)
|
||||
)
|
||||
}
|
||||
if (icResult is ICResult.Completed && icResult.exitCode == ExitCode.OK) {
|
||||
transaction.markAsSuccessful()
|
||||
}
|
||||
return icResult
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user