akka / akka-grpc

Akka gRPC
https://doc.akka.io/docs/akka-grpc/
Other
432 stars 123 forks source link

Naming a message `Segment` breaks code gen #977

Closed longshorej closed 4 years ago

longshorej commented 4 years ago

Hi! I think there's a bug in the code generation of akka-grpc if Segment is used as the name of a message in the protobuf definitions. I suspect the name Segment isn't the only one with an issue.

Versions used

1.0.0-M1

Expected Behavior

When I run the following:

git clone https://github.com/longshorej/garbage /tmp/garbage && cd /tmp/garbage/akka-grpc-codegen-issue && sbt compile

I expect it to complete successfully.

Actual Behavior

Instead, it fails to complete compilation. When I change Segment to Segmentz in the protobuf definitions, it successfully compiles.

Relevant logs

~ $ git clone https://github.com/longshorej/garbage /tmp/garbage && cd /tmp/garbage/akka-grpc-codegen-issue && sbt compile
Cloning into '/tmp/garbage'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 218 (delta 0), reused 11 (delta 0), pack-reused 206
Receiving objects: 100% (218/218), 35.08 KiB | 326.00 KiB/s, done.
Resolving deltas: 100% (49/49), done.
[info] Loading settings for project global-plugins from sbt-updates.sbt,idea.sbt,metals.sbt,plugins.sbt ...
[info] Loading global plugins from /home/longshorej/.sbt/1.0/plugins
[info] Loading settings for project akka-grpc-codegen-issue-build from plugins.sbt ...
[info] Loading project definition from /tmp/garbage/akka-grpc-codegen-issue/project
[info] Loading settings for project akka-grpc-codegen-issue from build.sbt ...
[info] Set current project to akka-grpc-codegen-issue (in build file:/tmp/garbage/akka-grpc-codegen-issue/)
[info] Executing in batch mode. For better performance use sbt's shell
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
[info] Compiling 2 protobuf files to /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main,/tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main,/tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main,/tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main
[info] Compiling schema /tmp/garbage/akka-grpc-codegen-issue/src/main/protobuf/service.proto
[info] Compiling schema /tmp/garbage/akka-grpc-codegen-issue/src/main/protobuf/message.proto
protoc-jar: protoc version: 3.11.4, detected platform: linux-x86_64 (linux/amd64)
protoc-jar: embedded: bin/3.11.4/protoc-3.11.4-linux-x86_64.exe
protoc-jar: executing: [/tmp/protocjar8508517233552498327/bin/protoc.exe, --plugin=protoc-gen-scala_0=/tmp/protocbridge4142489742036064517, --plugin=protoc-gen-akka-grpc-scaladsl-trait_1=/tmp/protocbridge2988643863802192766, --plugin=protoc-gen-akka-grpc-scaladsl-client_2=/tmp/protocbridge2358276463814585496, --plugin=protoc-gen-akka-grpc-scaladsl-server_3=/tmp/protocbridge533355923081037008, --scala_0_out=flat_package:/tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main, --akka-grpc-scaladsl-trait_1_out=flat_package:/tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main, --akka-grpc-scaladsl-client_2_out=flat_package:/tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main, --akka-grpc-scaladsl-server_3_out=flat_package:/tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main, -I/tmp/garbage/akka-grpc-codegen-issue/src/main/protobuf, -I/tmp/garbage/akka-grpc-codegen-issue/target/protobuf_external_src, -I/tmp/garbage/akka-grpc-codegen-issue/src/main/proto, -I/tmp/garbage/akka-grpc-codegen-issue/target/protobuf_external, /tmp/garbage/akka-grpc-codegen-issue/src/main/protobuf/service.proto, /tmp/garbage/akka-grpc-codegen-issue/src/main/protobuf/message.proto]
/tmp/garbage/akka-grpc-codegen-issue/target/protobuf_external_src: warning: directory does not exist.
/tmp/garbage/akka-grpc-codegen-issue/src/main/proto: warning: directory does not exist.
/tmp/garbage/akka-grpc-codegen-issue/target/protobuf_external: warning: directory does not exist.
[info] Generating Akka gRPC service interface for com.turtles.Turtles
[info] Generating Akka gRPC client for com.turtles.Turtles
[info] Generating Akka gRPC service handler for com.turtles.Turtles
[info] Compiling protobuf
[info] Protoc target: /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main
[info] Protoc target: /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main
[info] Protoc target: /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main
[info] Protoc target: /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main
[info] Compiling 8 Scala sources to /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/classes ...
[warn] /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main/com/turtles/TurtlesHandler.scala:14:42: imported `Segment' is permanently hidden by definition of object Segment in package turtles
[warn] import akka.http.scaladsl.model.Uri.Path.Segment
[warn]                                          ^
[error] /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main/com/turtles/TurtlesHandler.scala:101:25: constructor cannot be instantiated to expected type;
[error]  found   : com.turtles.Segment
[error]  required: akka.http.scaladsl.model.Uri.Path
[error]         case Path.Slash(Segment(`prefix`, Path.Slash(Segment(method, Path.Empty)))) =>
[error]                         ^
[error] /tmp/garbage/akka-grpc-codegen-issue/target/scala-2.12/src_managed/main/com/turtles/TurtlesHandler.scala:101:54: constructor cannot be instantiated to expected type;
[error]  found   : com.turtles.Segment
[error]  required: akka.http.scaladsl.model.Uri.Path
[error]         case Path.Slash(Segment(`prefix`, Path.Slash(Segment(method, Path.Empty)))) =>
[error]                                                      ^
[warn] one warning found
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 2 s, completed May 20, 2020 6:39:41 PM

Reproducible Test Case

https://github.com/longshorej/garbage/tree/master/akka-grpc-codegen-issue

raboof commented 4 years ago

Thanks! Yes, we still have some problems when the generated filenames overlap with other imports in the generated code.

The short-path solution in Akka gRPC would be to remove the imports and refer to the explicit FQCN's. That is fairly ugly, though.

A 'fuller' solution might be to make the code generation more intelligent so it knows when it can use imports and when it must use FQCN's. I think that can fall under #641, but almost calls for switching to another code generation library than twirl...

longshorej commented 4 years ago

Thanks for the thoughts! Would you accept an ugly short-path solution PR for now?

raboof commented 4 years ago

Thanks for the thoughts! Would you accept an ugly short-path solution PR for now?

Yes, definitely!

raboof commented 4 years ago

(it's also fine if you just fix 'Segment' and not all imports ;) )

raboof commented 4 years ago

This particular instance (and a number of others) were fixed in #1005, thanks for the ping!