square / wire

gRPC and protocol buffers for Android, Kotlin, Swift and Java.
https://square.github.io/wire/
Apache License 2.0
4.21k stars 571 forks source link

Compile several roles #1914

Open hadrienk opened 3 years ago

hadrienk commented 3 years ago

Is there a way to compile the proto files with multiple configurations?

I tried this way but without success:

wire {
    kotlin {
        rpcRole = "client"
    }
    kotlin {
        rpcRole = "server"
    }
    kotlin {
        rpcRole = "server"
        rpcCallStyle = "blocking"
    }
}
oldergod commented 3 years ago

Have you tried with exclusive false?

wire {
    kotlin {
        rpcRole = "client"
        // True if types emitted for this target should not also be emitted for other
        // targets. Use this to cause multiple outputs to be emitted for the same input
        // type.
        exclusive = false
    }
    kotlin {
        rpcRole = "server"
        exclusive = false
    }
    kotlin {
        rpcRole = "server"
        rpcCallStyle = "blocking"
    }
}
hadrienk commented 3 years ago

It looks like it tries but fail because the messages are being generated many time:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':generateMainProtos'.
> Same type is getting generated by different messages:
  Input at [...]/ test.proto:7:1
  Input at [...]/ test.proto:7:1

Can I use the excludes property to avoid generating the messages more than once?

oldergod commented 3 years ago

You should be able to. Common types should be generated once only indeed.

jasonab commented 1 month ago

I haven't had any success using exclusive or excludes - I still get the "same file is getting generated by different messages" error.

oldergod commented 1 month ago

You should not see that if you're using exclusive = false. If you can write a small repro config, please share it.

Breefield commented 1 week ago

Here is a branch that fails to assemble https://github.com/Breefield/wire-playground/tree/bh/failed-exclusive

wire {
    custom {
        // Be sure that `server-generator` is on the classpath for Gradle to resolve
        // `GrpcServerSchemaHandler`.
        schemaHandlerFactory = com.squareup.wire.kotlin.grpcserver.GrpcServerSchemaHandler.Factory()
        options = mapOf(
            // Defaults to `true` if absent. Any other value than `true` is considered false.
            "singleMethodServices" to "false",
            // Defaults to `suspending` if absent. Any other value than `suspending` is considered
            // non-suspending.
            "rpcCallStyle" to "blocking",
        )
        // We set the custom block exclusivity to false so that the next `kotlin {}` block can also
        // generate the protobuf Messages.
        exclusive = false
    }

    sourcePath {
        srcDir("src/main/protos")
    }

    kotlin {
        exclusive = false
        rpcRole = "client"
    }

    kotlin {
        exclusive = false
        rpcCallStyle = "blocking"
        rpcRole = "server"
    }
}

Gives me:

Same file /Users/bree/Desktop/square-wire/build/generated/source/wire/com/bree/dinosaurs/Dinosaur.kt is getting generated by different messages:
  Dinosaur at /Users/bree/Desktop/square-wire/src/main/protos/example/dinosaur.proto:6:1
  Dinosaur at /Users/bree/Desktop/square-wire/src/main/protos/example/dinosaur.proto:6:1

This is a bit of a blocker for me because I have a single repo where I define my protobufs, and then we've set up gradle to build various languages/clients/servers and publish them to artifactories/etc so our various repos don't all have to setup their own build pipelines.

oldergod commented 1 week ago

You need to configure your generator blocks in order to make it so that only one of them generate Dinosaur.kt