Skip to content

Commit

Permalink
Make compileProtoPath variant selection criteria tighter (#489)
Browse files Browse the repository at this point in the history
Add usage attribute to compileProtoPath, which is used to resolve dependencies inherited from compileOnly and implementation, for extracting included proto files. Setting attributes too loosely can lead to ambiguous variant selection errors as more than one variants of the dependency can be matched. Adding JAVA_RUNTIME usage attribute, which would select the same variant as by compileClasspath configuration.
  • Loading branch information
voidzcy authored Apr 22, 2021
1 parent 76a1922 commit 44309a2
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.LibraryElements
import org.gradle.api.attributes.Usage
import org.gradle.api.file.FileCollection
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.internal.file.FileResolver
Expand Down Expand Up @@ -179,7 +180,7 @@ class ProtobufPlugin implements Plugin<Project> {
* Creates an internal 'compileProtoPath' configuration for the given source set that extends
* compilation configurations as a bucket of dependencies with resources attribute.
* The extract-include-protos task of each source set will extract protobuf files from
* dependencies in this configuration.
* resolved dependencies in this configuration.
*
* <p> For Java projects only.
* <p> This works around 'java-library' plugin not exposing resources to consumers for compilation.
Expand All @@ -196,8 +197,22 @@ class ProtobufPlugin implements Plugin<Project> {
transitive = true
extendsFrom = [compileConfig, implementationConfig]
canBeConsumed = false
}.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
project.getObjects().named(LibraryElements, LibraryElements.RESOURCES))
}.getAttributes()
// Variant attributes are not inherited. Setting it too loosely can
// result in ambiguous variant selection errors.
// CompileProtoPath only need proto files from dependency's resources.
// LibraryElement "resources" is compatible with "jar" (if a "resources" variant is
// not found, the "jar" variant will be used).
.attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
project.getObjects().named(LibraryElements, LibraryElements.RESOURCES))
// Although variants with any usage has proto files, not setting usage attribute
// can result in ambiguous variant selection if the producer provides multiple
// variants with different usage attribute.
// Preserve the usage attribute from CompileOnly and Implementation.
.attribute(
Usage.USAGE_ATTRIBUTE,
project.getObjects().named(Usage, Usage.JAVA_RUNTIME))
}
}

Expand Down

0 comments on commit 44309a2

Please sign in to comment.