# onlyIf with TaskProvider and configuration cache

**URL:** <https://discuss.gradle.org/t/onlyif-with-taskprovider-and-configuration-cache/48587>\
**Category:** Help/Discuss\
**Created:** [May 30, 2024, 12:57pm UTC](https://discuss.gradle.org/t/onlyif-with-taskprovider-and-configuration-cache/48587 "2024-05-30T12:57:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vboriesazeau](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vboriesazeau](https://discuss.gradle.org/u/vboriesazeau)\
**Post date:** [May 30, 2024, 12:57pm UTC](https://discuss.gradle.org/t/onlyif-with-taskprovider-and-configuration-cache/48587/1 "2024-05-30T12:57:23Z")

</div>

Hi,

I wrote a Java plugin that registers a task “customTask” having an onlyIf spec that depends on another task.

```java
project.getTasks.register("customTask", CustomTask.class, task -> {
    // ... define group, inputs, outputs, etc.
    task.setOnlyIf(t -> t.getProject().getTasks().named("anotherTask", <boolean predicate>).orElse(false));
});

```

Without the configuration cache, the task is executed or skipped as expected depending on the result of the onlyIf spec at execution time.  
With the configuration cache, the build fails with the following error:

```gradle
1 problem was found storing the configuration cache.
- Task `:aTask` of type `...`: cannot serialize object of type 'org.gradle.api.internal.tasks.DefaultTaskContainer', a subtype of 'org.gradle.api.tasks.TaskContainer', as these are not supported with the configuration cache.

```

How can I register a task with a conditional execution (onlyIf) that depends on a predicate based on another task, and compatible with the configuration cache?

Thanks in advance for your help.  
BR

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [May 31, 2024, 2:19am UTC](https://discuss.gradle.org/t/onlyif-with-taskprovider-and-configuration-cache/48587/2 "2024-05-31T02:19:00Z")

</div>

Can you elaborate a bit on what you do with that other task / draw from that other task?  
Your example is a bit too condensed to really be helpful in understanding your issue, especially as it does not look like it would be near to compilable. 🙂

---

<div class="post-metadata">

**Author:** ![vboriesazeau](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vboriesazeau](https://discuss.gradle.org/u/vboriesazeau)\
**Post date:** [June 6, 2024, 12:40pm UTC](https://discuss.gradle.org/t/onlyif-with-taskprovider-and-configuration-cache/48587/3 "2024-06-06T12:40:27Z")

</div>

Hi @Vampire,

Thank you for your answer. I found another way to skip the task I was talking about without relying on an input/output of an other task.

I don’t understand why the configuration cache is sensitive/uses the onlyIf spec during its serialization. The [javadoc about the onlyIf method](https://docs.gradle.org/current/javadoc/org/gradle/api/DefaultTask.html#onlyIf(org.gradle.api.specs.Spec)) states the spec is run at execution time, not at configuration. It appears inconsistent to me.

Am I missing something about the configuration cache and/or this spec?  
BR

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [June 6, 2024, 1:21pm UTC](https://discuss.gradle.org/t/onlyif-with-taskprovider-and-configuration-cache/48587/4 "2024-06-06T13:21:36Z")

</div>

You are right, that `onlyIf` is run at execution time, that’s exactly your problem.  
If it would run at configuration time, the CC could just serialize the result.  
But as it runs at execution time, it needs to serialize the action you give to it, the lambda.  
And in serializing that action it also needs to serialize the things from outside that it refers to like the project or the tasks container and there you have things that are not CC-safe.

---

<div class="post-metadata">

**Author:** ![vboriesazeau](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vboriesazeau](https://discuss.gradle.org/u/vboriesazeau)\
**Post date:** [June 6, 2024, 1:36pm UTC](https://discuss.gradle.org/t/onlyif-with-taskprovider-and-configuration-cache/48587/5 "2024-06-06T13:36:21Z")

</div>

Ok, I think I understand my mistake. That may be obvious but that was not for me until now:

- The `onlyIf`method is another configuration option, and as such, its value is serialized in the configuration cache.
- But the `onlyIf` value (the spec) is run at execution time.

Thanks @Vampire for your help.
