Skip to content

Commit

Permalink
Include stack tracing failure for PropertyProblem
Browse files Browse the repository at this point in the history
  • Loading branch information
alllex committed Apr 26, 2024
1 parent 6d44ee2 commit 70f2748
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,6 @@ class ConfigurationCacheFingerprintWriter(
PropertyProblem(
trace,
StructuredMessage.build(messageBuilder),
null,
documentationSection = documentationSection
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class ConfigurationCacheInjectedClasspathInstrumentationStrategy(
PropertyProblem(
PropertyTrace.Gradle,
StructuredMessage.build { text("support for using a Java agent with TestKit builds is not yet implemented with the configuration cache.") },
null,
DocumentationSection.NotYetImplementedTestKitJavaAgent
documentationSection = DocumentationSection.NotYetImplementedTestKitJavaAgent
)
)
return CachedClasspathTransformer.StandardTransform.BuildLogic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.gradle.configurationcache.initialization.ConfigurationCacheStartParam
import org.gradle.initialization.RootBuildLifecycleListener
import org.gradle.internal.InternalBuildAdapter
import org.gradle.internal.event.ListenerManager
import org.gradle.internal.problems.failure.FailureFactory
import org.gradle.internal.service.scopes.Scope
import org.gradle.internal.service.scopes.ServiceScope
import org.gradle.problems.buildtree.ProblemReporter
Expand All @@ -52,7 +53,10 @@ class ConfigurationCacheProblems(
val cacheKey: ConfigurationCacheKey,

private
val listenerManager: ListenerManager
val listenerManager: ListenerManager,

private
val failureFactory: FailureFactory

) : ProblemsListener, ProblemReporter, AutoCloseable {

Expand Down Expand Up @@ -130,7 +134,8 @@ class ConfigurationCacheProblems(
}

override fun onError(trace: PropertyTrace, error: Exception, message: StructuredMessageBuilder) {
onProblem(PropertyProblem(trace, StructuredMessage.build(message), error))
val failure = failureFactory.create(error)
onProblem(PropertyProblem(trace, StructuredMessage.build(message), error, failure))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import org.gradle.internal.hash.HashCode
import org.gradle.internal.hash.Hashing
import org.gradle.internal.hash.HashingOutputStream
import org.gradle.internal.problems.failure.Failure
import org.gradle.internal.problems.failure.FailureFactory
import org.gradle.internal.service.scopes.Scope
import org.gradle.internal.service.scopes.ServiceScope
import java.io.Closeable
Expand All @@ -42,8 +41,7 @@ import kotlin.contracts.contract
class ConfigurationCacheReport(
executorFactory: ExecutorFactory,
temporaryFileProvider: TemporaryFileProvider,
internalOptions: InternalOptions,
private val failureFactory: FailureFactory
internalOptions: InternalOptions
) : Closeable {

companion object {
Expand Down Expand Up @@ -229,7 +227,7 @@ class ConfigurationCacheReport(

private
fun decorateProblem(problem: PropertyProblem, severity: ProblemSeverity): DecoratedPropertyProblem {
val failure = problem.exception?.toFailure()
val failure = problem.stackTracingFailure
return DecoratedPropertyProblem(
problem.trace,
decorateMessage(problem, failure),
Expand All @@ -238,9 +236,6 @@ class ConfigurationCacheReport(
)
}

private
fun Throwable.toFailure() = failureFactory.create(this)

private
fun decoratedFailureFor(failure: Failure?, severity: ProblemSeverity): DecoratedFailure? {
return when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,24 @@ class FailureDecorator {

private
fun exceptionSummaryFor(failure: Failure): StructuredMessage? {
failure.stackTrace.forEachIndexed { index, element ->
if (failure.getStackTraceRelevance(index).isUserCode()) {
return exceptionSummaryFrom(element)
return failure.findFirstUserCode()?.let(::exceptionSummaryFrom)
}

private
fun Failure.findFirstUserCode(): StackTraceElement? {
stackTrace.forEachIndexed { index, element ->
if (getStackTraceRelevance(index).isUserCode()) {
return element
}
}

return null
}

private
fun exceptionSummaryFrom(elem: StackTraceElement) = StructuredMessage.build {
fun exceptionSummaryFrom(frame: StackTraceElement) = StructuredMessage.build {
text("at ")
reference(elem.toString())
reference(frame.toString())
}

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class DefaultProblemFactory(
locationForCaller(consumer, userCodeContext.current()?.source)

override fun problem(message: StructuredMessage, exception: Throwable?, documentationSection: DocumentationSection?): PropertyProblem {
val trace = locationForCaller(null, problemStream.forCurrentCaller(exception))
return PropertyProblem(trace, message, exception, documentationSection)
val diagnostics = problemStream.forCurrentCaller(exception)
val trace = locationForCaller(null, diagnostics)
return PropertyProblem(trace, message, exception, diagnostics.failure, documentationSection)
}

override fun problem(consumer: String?, messageBuilder: StructuredMessage.Builder.() -> Unit): ProblemFactory.Builder {
Expand Down Expand Up @@ -73,13 +74,14 @@ class DefaultProblemFactory(
}

override fun build(): PropertyProblem {
val exceptionMessage = exceptionMessage
val diagnostics = if (exceptionMessage == null) {
problemStream.forCurrentCaller()
} else {
problemStream.forCurrentCaller(Supplier { InvalidUserCodeException(exceptionMessage!!) })
problemStream.forCurrentCaller(Supplier { InvalidUserCodeException(exceptionMessage) })
}
val location = locationMapper(locationForCaller(consumer, diagnostics))
return PropertyProblem(location, message, diagnostics.exception, documentationSection)
return PropertyProblem(location, message, diagnostics.exception, diagnostics.failure, documentationSection)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.gradle.configurationcache.problems

import org.gradle.internal.DisplayName
import org.gradle.internal.problems.failure.Failure
import kotlin.reflect.KClass


Expand All @@ -27,6 +28,11 @@ data class PropertyProblem internal constructor(
val trace: PropertyTrace,
val message: StructuredMessage,
val exception: Throwable? = null,
/**
* A failure containing stack tracing information.
* The failure may be synthetic when the cause of the problem was not an exception.
*/
val stackTracingFailure: Failure? = null,
val documentationSection: DocumentationSection? = null
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ import kotlin.reflect.KClass

fun IsolateContext.logPropertyProblem(
action: String,
exception: Throwable? = null,
documentationSection: DocumentationSection? = null,
message: StructuredMessageBuilder
) {
logPropertyProblem(action, PropertyProblem(trace, build(message), exception, documentationSection))
logPropertyProblem(action, PropertyProblem(trace, build(message), documentationSection = documentationSection))
}


Expand Down Expand Up @@ -108,15 +107,15 @@ fun IsolateContext.logNotImplemented(feature: String, documentationSection: Docu
build {
text("support for $feature is not yet implemented with the configuration cache.")
},
null, documentationSection
documentationSection = documentationSection
)
)
}


private
fun IsolateContext.logPropertyProblem(documentationSection: DocumentationSection? = null, message: StructuredMessageBuilder) {
val problem = PropertyProblem(trace, build(message), null, documentationSection)
val problem = PropertyProblem(trace, build(message), documentationSection = documentationSection)
logPropertyProblem("serialize", problem)
}

Expand Down

0 comments on commit 70f2748

Please sign in to comment.