# Is there a way to use the build cache with FileTree outputs?

**URL:** https://discuss.gradle.org/t/is-there-a-way-to-use-the-build-cache-with-filetree-outputs/48118
**Category:** Help/Discuss
**Created:** [April 5, 2024, 6:27pm UTC](https://discuss.gradle.org/t/is-there-a-way-to-use-the-build-cache-with-filetree-outputs/48118 "2024-04-05T18:27:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Nikerino](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nikerino](https://discuss.gradle.org/u/Nikerino)
#### Post date: [April 5, 2024, 6:27pm UTC](https://discuss.gradle.org/t/is-there-a-way-to-use-the-build-cache-with-filetree-outputs/48118/1 "2024-04-05T18:27:11Z")

</div>

I’m currently trying to make use of the build cache with a custom task but running into some problems -  
My task is configured like such (it gets created inside a plugin’s `apply()` method):

```groovy
TaskProvider<Task> buildTask = project.tasks.register("build", MSBuild) {
    outputs.files(project.fileTree("bin") {
        include "**"
        exclude "*.pdb"
    })
    outputs.cacheIf { true }
}

```

There are also some task dependencies, input files, things like that, but I didn’t include them above because this issue deals with caching the outputs specifically.

When attempting to build with the build-cache enabled (and org.gradle.caching.debug enabled) - I get a log message:

> Non-cacheable because Output property ‘$1$1’ contains a file tree [NON\_CACHEABLE\_OUTPUT]

I could have sworn that I was able to do this in gradle 8.5, and right now I’m using gradle 8.6. I tried reverting to 8.5 but get the same message in there.

Am I missing something here? I thought that file trees could be used for task outputs. Logically, it seems like that should be the case since one could simply evaluate the file tree and cache the files it evaluates to after the task runs successfully.

My main goal is that I really want to be able to filter the output files for a task by a pattern - so I guess a file tree is not required, just a way that I can cache a certain portion of output files in a directory.

I found these related issues:

> <https://github.com/gradle/gradle/issues/9131>
>
> We might want to support this, or at least document what people can do something… else (like delete output files they don't care about).
> 
> Use case: you are generating a CMAKE build, but you don't want the log files cached.

> <https://github.com/gradle/gradle/issues/2180>
>
> \### Expected Behavior
> When declaring task outputs I'd expect to be able to use …\`fileTree()\` with include/exclude patterns.
> 
> \### Current Behavior
> After removing an output file and running the first time, the up-to-date check is "false" and the task is executed, as expected. However, sometimes when running the task again, the up-to-date check still considers the task to be out of date.
> 
> \### Steps to Reproduce
> The following tasks have been used to reproduce the problem:
> \`\`\`
> task myTask1 {
> group 'build'
> inputs.file file('input.txt')
> outputs.file file('output1.txt')
> doLast {
> new File('output1.txt').text = new File('input.txt').text
> }
> }
> 
> task myTask2 {
> group 'build'
> inputs.file file('input.txt')
> outputs.files fileTree(dir: '/', include: 'output2.txt')
> doLast {
> new File('output2.txt').text = new File('input.txt').text
> }
> }
> 
> task myTask3 {
> group 'build'
> inputs.file file('input.txt')
> def outputFiles = fileTree(dir: '/', include: 'output3.txt').files
> outputs.files outputFiles
> doLast {
> new File('output3.txt').text = new File('input.txt').text
> }
> }
> \`\`\`
> 
> \`myTask1\` that uses \`file()\`
> 
> to specify the output never experiences the problem.
> \`myTask2\` experiences the problem ~5-10% of the time.
> \`myTask3\` also experiences the problem ~5-10% of the time, but \_only\_ when using the gradle daemon.
> 
> Simple do this to reproduce:
> \`\`\`
> rm output2.txt
> ./gradlew myTask2
> ./gradlew myTask2
> \`\`\`\`
> 
> \### Environment
> Windows 7
> git-bash
> gradle 3.5
> JVM 1.8.0\_40 (Oracle)

and they both seem to be resolved, but I’m not sure why I’m seeing this behavior.  
Any advice would be appreciated.

---

<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: [April 8, 2024, 7:37am UTC](https://discuss.gradle.org/t/is-there-a-way-to-use-the-build-cache-with-filetree-outputs/48118/2 "2024-04-08T07:37:09Z")

</div>

The second is resolved and only about up-to-date checks, not caching.

The first is still open and exactly your issue I’d say, so it probably is not possible currently.

---

<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: [April 8, 2024, 7:38am UTC](https://discuss.gradle.org/t/is-there-a-way-to-use-the-build-cache-with-filetree-outputs/48118/3 "2024-04-08T07:38:32Z")

</div>

If the pdb files are unimportant or could live somewhere else, you could maybe instead add a `doLast` action that deletes or moves them.

---

<div class="post-metadata">

### Author: ![Nikerino](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nikerino](https://discuss.gradle.org/u/Nikerino)
#### Post date: [April 8, 2024, 9:38pm UTC](https://discuss.gradle.org/t/is-there-a-way-to-use-the-build-cache-with-filetree-outputs/48118/4 "2024-04-08T21:38:04Z")

</div>

I see, thank you. It was mostly about having an easy way to define the files that will be cached. Really it would be nice if there was someway to define the files to cache by pattern, since it works nicely for incremental building.

---

<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: [April 8, 2024, 9:43pm UTC](https://discuss.gradle.org/t/is-there-a-way-to-use-the-build-cache-with-filetree-outputs/48118/5 "2024-04-08T21:43:57Z")

</div>

Definitely, that’s what the issue you posted is for. 🙂
