How to ConfigurableFileCollection.setFrom with Provider?

Thanks to @Stefan_Wolf’s suggestion in that topic I solved my case with:

task.sourceDirs.setFrom({ 
    extension.mainSourceSets.get()*.allSource*.srcDirs
} as Callable<Set<SourceDirectorySet>>)

There were two elements which prevent me from doing it before:

  1. I missed originally try to play with Closure, but it didn’t work due to missing .srcDirs in my code which resulted in empty FileCollection which was failing my test.

  2. Closure has to be coerced into Callable with a proper type to help determine what is returned to prevent:

java.lang.ClassCastException: class ArrayList1_groovyProxy cannot be cast to class java.util.Iterator (ArrayList1_groovyProxy is in unnamed module of loader org.codehaus.groovy.runtime.ProxyGeneratorAdapter$InnerLoader @38f617f4; java.util.Iterator is in module java.base of loader ‘bootstrap’)