gradle-scripts icon indicating copy to clipboard operation
gradle-scripts copied to clipboard

gPRC module build fails with duplicate source error

Open ks-yim opened this issue 3 years ago • 2 comments

Issue

  • A gPRC module(a module contains proto files`) build fails with the following error.
    $ ./gradlew clean :protocol:build
    
    Type-safe dependency accessors is an incubating feature.
    > Task :protocol:sourcesJar FAILED
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':protocol:sourcesJar'.
    > Entry com/example/foo/FooOuterClass.java is a duplicate but no duplicate handling strategy has been set. Please refer to 
    https://docs.gradle.org/7.4/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.
    
    • Reproducer: https://github.com/ks-yim/grpc-gensrc-dub-err-reproducer.git

Potential Cause

  • Files under gen-src/main/java gets registered twice as sourceSet once by line-gradle-scripts at java.gradle:L58 and again by protobuf-gradle-plugin.

Current Workaround

  1. Comment out java.gradle:L58
  2. Define Jar task duplicatesStrategy as follows:
    tasks.withType<Jar> {
        duplicatesStrategy = DuplicatesStrategy.INCLUDE
    }
    

ks-yim avatar Oct 17 '22 01:10 ks-yim

Hello! Thanks for the report and reproducer. 🙇‍♂️ How about adding the sourceSet only if it's not added?

afterEvaluate { // We should do this in afterEvaluate so that prototbuf plugin applied first.
    project.sourceSets.all { sourceSet ->
        def javaSrcDir = file("${project.ext.genSrcDir}/${sourceSet.name}/java")
        if (!sourceSet.java.srcDirs.contains(javaSrcDir)) {
            sourceSet.java.srcDir javaSrcDir
        }
        ...
    }
    ...
}

minwoox avatar Oct 17 '22 14:10 minwoox

@minwoox Thanks for the advice. I will check if that works and send a fix PR!

ks-yim avatar Oct 18 '22 01:10 ks-yim