Skip to content

Commit c226c18

Browse files
committed
Workaround mapped value before task completed error for excluded task
When using provider.map, Gradle has checks that can fail if the provider is used too early. For example: > Querying the mapped value of provider(interface java.util.Set) before > task ':p1:compileScala' has completed is not supported However, it seems those checks can also fire even if the incomplete task is a properly registered dependency and only when accessed from an action. This is possible when using -x to exclude a task. This deserves a Gradle bug, but let's workaround it in the meantime. Fixes #550
1 parent 6a71480 commit c226c18

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy

+12-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ import org.gradle.api.DefaultTask
3535
import org.gradle.api.file.ConfigurableFileCollection
3636
import org.gradle.api.file.DuplicatesStrategy
3737
import org.gradle.api.file.FileCollection
38-
import org.gradle.api.file.FileSystemLocation
3938
import org.gradle.api.file.FileTree
4039
import org.gradle.api.logging.Logger
4140
import org.gradle.api.model.ObjectFactory
41+
import org.gradle.api.provider.ProviderFactory
4242
import org.gradle.api.tasks.Input
4343
import org.gradle.api.tasks.InputFiles
4444
import org.gradle.api.tasks.Internal
@@ -105,6 +105,9 @@ abstract class ProtobufExtract extends DefaultTask {
105105
@Inject
106106
abstract ObjectFactory getObjectFactory()
107107

108+
@Inject
109+
abstract ProviderFactory getProviderFactory()
110+
108111
@TaskAction
109112
void extract() {
110113
destDir.mkdir()
@@ -150,11 +153,16 @@ abstract class ProtobufExtract extends DefaultTask {
150153
boolean warningLogged = false
151154
ArchiveActionFacade archiveFacade = this.archiveActionFacade
152155
Logger logger = this.logger
153-
return objectFactory.fileCollection().from(inputFiles.getElements().map { files ->
156+
// Provider.map seems broken for excluded tasks. Add inputFiles with all contents excluded for
157+
// the dependency it provides, but then provide the files we actually care about in our own
158+
// provider. https://github.com/google/protobuf-gradle-plugin/issues/550
159+
return objectFactory.fileCollection()
160+
.from(inputFiles.filter { false })
161+
.from(providerFactory.provider { unused ->
162+
Set<File> files = inputFiles.files
154163
PatternSet protoFilter = new PatternSet().include("**/*.proto")
155164
Set<Object> protoInputs = [] as Set
156-
for (FileSystemLocation location : files) {
157-
File file = location.asFile
165+
for (File file : files) {
158166
if (file.isDirectory()) {
159167
protoInputs.add(file)
160168
} else if (file.path.endsWith('.proto')) {

0 commit comments

Comments
 (0)