You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Gradle Task with some field of type Provider<*> get evaluated twice if Configuration Cache is enabled.
Expected Behavior
Provider gets evaluated only once.
Context
I want to delay the time-expensive evaluation of some data. Either during task execution (when configuration cache isn't enabled)
or capture it in configuration cache and then reuse cached value during execution.
Steps to Reproduce
// build.gradle.ktsabstractclassMyTask : DefaultTask() {
privateval prop = project.provider {
println("Expensive data calculation")
"data"
}
@TaskAction
funaction() {
println("Results: ${prop.get()}")
}
}
val myTask by tasks.registering(MyTask::class)
Executing this task for the first time shows that prop gets evaluated twice: during configuration phase and execution phase
$ ./gradlew myTask --configuration-cache --rerun-tasks
Configuration cache is an incubating feature.
Calculating task graph as no configuration cache is available for tasks: myTask
Expensive data calculation
> Task :myTask
Expensive data calculation
Results: data
Executing this task second time shows that prop is recovered from cache and thus no evaluation happened.
$ ./gradlew myTask --configuration-cache --rerun-tasks
Configuration cache is an incubating feature.
Reusing configuration cache.
> Task :myTask
Results: data
Workaround
Kotlin lazy delegate works as expected. Although gradle configuration cache got a bit polluted with internal state of Lazy.
abstractclassMyTask : DefaultTask() {
privateval prop by lazy {
println("Expensive data calculation")
"data"
}
@TaskAction
funaction() {
println("Results: $prop")
}
}
val myTask by tasks.registering(MyTask::class)
1st execution:
$ ./gradlew myTask --configuration-cache --rerun-tasks
Configuration cache is an incubating feature.
Calculating task graph as configuration cache cannot be reused because file 'build.gradle.kts' has changed.
Expensive data calculation
> Task :myTask
Results: data
2nd execution:
$ ./gradlew myTask --configuration-cache --rerun-tasks
Configuration cache is an incubating feature.
Reusing configuration cache.
> Task :myTask
Results: data
Environment
Gradle 7.4.2
The text was updated successfully, but these errors were encountered:
A provider is calculated each time its value is requested, that is by design afaik.
But you can relatively easy produce a cached provider that is only calculated lazily once:
A Gradle Task with some field of type
Provider<*>
get evaluated twice if Configuration Cache is enabled.Expected Behavior
Provider
gets evaluated only once.Context
I want to delay the time-expensive evaluation of some data. Either during task execution (when configuration cache isn't enabled)
or capture it in configuration cache and then reuse cached value during execution.
Steps to Reproduce
Executing this task for the first time shows that
prop
gets evaluated twice: during configuration phase and execution phaseExecuting this task second time shows that
prop
is recovered from cache and thus no evaluation happened.Workaround
Kotlin
lazy
delegate works as expected. Although gradle configuration cache got a bit polluted with internal state ofLazy
.1st execution:
2nd execution:
Environment
Gradle 7.4.2
The text was updated successfully, but these errors were encountered: