Skip to content

Commit

Permalink
Workaround mapped value before task completed error for excluded task
Browse files Browse the repository at this point in the history
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 google#550
  • Loading branch information
ejona86 committed Jul 6, 2022
1 parent 2b702eb commit 976d787
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileSystemLocation
import org.gradle.api.file.FileTree
import org.gradle.api.logging.Logger
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Internal
Expand Down Expand Up @@ -105,6 +105,9 @@ abstract class ProtobufExtract extends DefaultTask {
@Inject
abstract ObjectFactory getObjectFactory()

@Inject
abstract ProviderFactory getProviderFactory()

@TaskAction
void extract() {
destDir.mkdir()
Expand Down Expand Up @@ -150,11 +153,16 @@ abstract class ProtobufExtract extends DefaultTask {
boolean warningLogged = false
ArchiveActionFacade archiveFacade = this.archiveActionFacade
Logger logger = this.logger
return objectFactory.fileCollection().from(inputFiles.getElements().map { files ->
// Provider.map seems broken for excluded tasks. Add inputFiles with all contents excluded for
// the dependency it provides, but then provide the files we actually care about in our own
// provider. https://github.com/google/protobuf-gradle-plugin/issues/550
return objectFactory.fileCollection()
.from(inputFiles.filter { false })
.from(providerFactory.provider { unused ->
Set<File> files = inputFiles.files
PatternSet protoFilter = new PatternSet().include("**/*.proto")
Set<Object> protoInputs = [] as Set
for (FileSystemLocation location : files) {
File file = location.asFile
for (File file : files) {
if (file.isDirectory()) {
protoInputs.add(file)
} else if (file.path.endsWith('.proto')) {
Expand Down

0 comments on commit 976d787

Please sign in to comment.