https://gradle.com/ logo
#configuration-cache
Title
# configuration-cache
z

Zak Taccardi

09/28/2022, 7:16 PM
Is it possible to convert the output of a
TaskProvider<Sync>
into a
Provider<FileSystemLocation>
and support the config cache? With:
Copy code
fun TaskProvider<Sync>.asFileSystemLocationSingleFile(): Provider<FileSystemLocation> {
    return map {
        it.outputs.files.asFileTree.elements.get()
    }
        .map {
            // seeing `> Collection is empty.` error with config cache on
            it.first()
        }
}
I’m seeing this error:
Copy code
1 problem was found storing the configuration cache.
- Task `:jenkinsfile:jenkinsfile-shared:modifySharedGroovy` of type `Build_gradle$ModifySharedGroovy`: value 'map(org.gradle.api.file.FileSystemLocation map(map(provider(task 'syncOriginalSharedGroovy', class org.gradle.api.tasks.Sync))) check-type())' failed to unpack provider
This (using
Set<FileSystemLocation>
instead of
FileSystemLocation
), however works:
Copy code
fun TaskProvider<Sync>.asFileSystemLocationSet(): Provider<Set<FileSystemLocation>> {
    return map {
        it.outputs.files.asFileTree.elements.get()
    }
}
I think this is because Gradle understands that the
Set<..>
is live, and pulling a single
FileSystemLocation
out of it breaks that behavior
However - I don’t want to define my task input as
Set<FileSystemLocation>
when it’s only 1 file - I want to define it as
FileSystemLocation
r

Rodrigo Oliveira

09/28/2022, 9:47 PM
Perhaps the following would work:
Copy code
fun TaskProvider<Sync>.asFileSystemLocationSingleFile(): Provider<FileSystemLocation> {
    return flatMap { sync ->
        sync.outputs.files.asFileTree.elements.map { it.first() }
    }
}
The
asFileTree
step looks redundant:
Copy code
fun TaskProvider<Sync>.asFileSystemLocationSingleFile(): Provider<FileSystemLocation> {
    return flatMap { sync ->
        sync.outputs.files.elements.map { it.first() }
    }
}
z

Zak Taccardi

09/28/2022, 9:49 PM
I’ve had so many issues with flatmap tbh bc it switches the provider and seems to break the chain of dependent tasks, but I’ll try it
Copy code
fun TaskProvider<Sync>.asFileSystemLocationSingleFile(): Provider<FileSystemLocation> {
    return flatMap { sync ->
        sync.outputs.files.asFileTree.elements.map { it.first() }
    }
}
worked! Needed to use
.asFileTree
to get the actual file output instead of the directory
3 Views