From 4dab3e324905b8fea49b705cb5837c0ce0c148ec Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Thu, 19 Nov 2026 17:34:09 +0100 Subject: [PATCH 1/2] Resolve required Pekko gRPC template sources explicitly The Twirl compatibility generator needs pekko-grpc-codegen's sources JAR. Resolve it as a normal dependency in a private configuration instead of assuming optional IDE classifier downloads always contain it. Select the source artifact by metadata and keep the build-only dependency off the compile classpath and out of the published POM. --- build.sbt | 2 ++ project/WorkaroundTwirlFormatCompat.scala | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index 557779e1..b801ece3 100644 --- a/build.sbt +++ b/build.sbt @@ -108,6 +108,7 @@ val playActionsTestData = Project("play-grpc-actions-testdata", file("play-actio .pluginTestingSettings val playGenerators = Project(id = "play-grpc-generators", file("play-generators")) + .configs(WorkaroundTwirlFormatCompat.Sources) .enablePlugins(SbtTwirl, BuildInfoPlugin) .settings( // Temporary Twirl compatibility shim: @@ -118,6 +119,7 @@ val playGenerators = Project(id = "play-grpc-generators", file("play-generators" Compile / sourceGenerators += WorkaroundTwirlFormatCompat.generate.taskValue, libraryDependencies ++= Seq( Dependencies.Compile.pekkoGrpcCodegen, + Dependencies.Compile.pekkoGrpcCodegen.classifier("sources").intransitive() % WorkaroundTwirlFormatCompat.Sources, Dependencies.Test.scalaTest, ), buildInfoKeys ++= Seq[BuildInfoKey](organization, name, version, scalaVersion, sbtVersion), diff --git a/project/WorkaroundTwirlFormatCompat.scala b/project/WorkaroundTwirlFormatCompat.scala index 57d2bc07..70a50372 100644 --- a/project/WorkaroundTwirlFormatCompat.scala +++ b/project/WorkaroundTwirlFormatCompat.scala @@ -4,6 +4,8 @@ import sbt._ import sbt.Keys._ object WorkaroundTwirlFormatCompat { + val Sources = config("twirlCompatSources").hide + private val TemplateSources = Seq( "templates/JavaClient/txt/GenMethodImports.template.scala", "templates/JavaClient/txt/ClientPowerApi.template.scala", @@ -35,10 +37,17 @@ object WorkaroundTwirlFormatCompat { val outputDir = (Compile / sourceManaged).value / "pekko-grpc-twirl-compat" val extractDir = streams.value.cacheDirectory / "pekko-grpc-twirl-compat" - val sourceJar = (Compile / updateClassifiers).value.allFiles - .find(file => - file.getName == s"pekko-grpc-codegen_${scalaBinaryVersion.value}-${Dependencies.Versions.pekkoGrpc}-sources.jar" + // This is a required build input; optional IDE classifier downloads can omit it. + val sourceJar = update.value + .matching( + configurationFilter(Sources.name) && + moduleFilter( + organization = "org.apache.pekko", + name = s"pekko-grpc-codegen_${scalaBinaryVersion.value}", + revision = Dependencies.Versions.pekkoGrpc, + ) && artifactFilter(classifier = "sources") ) + .headOption .getOrElse(sys.error("Could not locate pekko-grpc-codegen sources jar for Twirl compatibility generation")) IO.delete(outputDir) From 9f796eb0b8d8be8553aa0572084749105714a9b9 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Thu, 19 Nov 2026 17:34:10 +0100 Subject: [PATCH 2/2] Generate Scala 3 context arguments in Play gRPC routers Pekko gRPC 2 enables scala3_sources for Scala 3, so generated handlers take their actor system through a using clause. Match that syntax in both Play router templates. Enable Scala 3 generation in the integration fixtures so CI exercises this path, import the generated serializers by name, and cover both syntax modes, router variants and power APIs in regression tests. --- .../templates/PlayScala/Router.scala.txt | 2 +- .../PlayScala/RouterUsingActions.scala.txt | 2 +- .../pekko/grpc/gen/scaladsl/BadCitizen.scala | 7 ++-- .../PlayScalaServerCodeGeneratorSpec.scala | 42 +++++++++++++++++++ .../scala/play/grpc/PlayScalaRouterSpec.scala | 3 +- project/ProjectExtensions.scala | 4 ++ 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 play-generators/src/test/scala/play/grpc/gen/scaladsl/PlayScalaServerCodeGeneratorSpec.scala diff --git a/play-generators/src/main/twirl/templates/PlayScala/Router.scala.txt b/play-generators/src/main/twirl/templates/PlayScala/Router.scala.txt index f5d907d5..1fd00607 100644 --- a/play-generators/src/main/twirl/templates/PlayScala/Router.scala.txt +++ b/play-generators/src/main/twirl/templates/PlayScala/Router.scala.txt @@ -21,6 +21,6 @@ import play.grpc.internal.PlayRouter extends PlayRouter(@{service.name}.name) with @{serviceName} { - override protected val respond = @{serviceName}Handler(this, @{service.name}.name, eHandler)(system) + override protected val respond = @{serviceName}Handler(this, @{service.name}.name, eHandler)(@{service.scalaCompatConstants.ImplicitUsing}system) } } diff --git a/play-generators/src/main/twirl/templates/PlayScala/RouterUsingActions.scala.txt b/play-generators/src/main/twirl/templates/PlayScala/RouterUsingActions.scala.txt index 67d4c4fb..2d40681f 100644 --- a/play-generators/src/main/twirl/templates/PlayScala/RouterUsingActions.scala.txt +++ b/play-generators/src/main/twirl/templates/PlayScala/RouterUsingActions.scala.txt @@ -36,7 +36,7 @@ import scala.concurrent.ExecutionContext */ @@InternalApi final override protected def createHandler(serviceName: String, system: ActorSystem): RequestHeader => EssentialAction = { - val handler = @{serviceName}Handler(this, serviceName, eHandler)(system) + val handler = @{serviceName}Handler(this, serviceName, eHandler)(@{service.scalaCompatConstants.ImplicitUsing}system) reqOuter => implicit val ec: ExecutionContext = system.dispatcher Path(reqOuter.path) match { diff --git a/play-generators/src/test/scala/org/apache/pekko/grpc/gen/scaladsl/BadCitizen.scala b/play-generators/src/test/scala/org/apache/pekko/grpc/gen/scaladsl/BadCitizen.scala index 371a474f..943ba258 100644 --- a/play-generators/src/test/scala/org/apache/pekko/grpc/gen/scaladsl/BadCitizen.scala +++ b/play-generators/src/test/scala/org/apache/pekko/grpc/gen/scaladsl/BadCitizen.scala @@ -4,7 +4,8 @@ package org.apache.pekko.grpc.gen.scaladsl object BadCitizen { - def scalaCompatConstants(): ScalaCompatConstants = new org.apache.pekko.grpc.gen.scaladsl.ScalaCompatConstants( - emitScala3Sources = true, - ) + def scalaCompatConstants(emitScala3Sources: Boolean = true): ScalaCompatConstants = + new org.apache.pekko.grpc.gen.scaladsl.ScalaCompatConstants( + emitScala3Sources = emitScala3Sources, + ) } diff --git a/play-generators/src/test/scala/play/grpc/gen/scaladsl/PlayScalaServerCodeGeneratorSpec.scala b/play-generators/src/test/scala/play/grpc/gen/scaladsl/PlayScalaServerCodeGeneratorSpec.scala new file mode 100644 index 00000000..97a4d8c7 --- /dev/null +++ b/play-generators/src/test/scala/play/grpc/gen/scaladsl/PlayScalaServerCodeGeneratorSpec.scala @@ -0,0 +1,42 @@ +/* + * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. + */ +package play.grpc.gen.scaladsl + +import com.google.protobuf.DescriptorProtos.ServiceOptions +import org.apache.pekko.grpc.gen.scaladsl.BadCitizen +import org.apache.pekko.grpc.gen.scaladsl.Service +import org.scalatest.matchers.should.Matchers +import org.scalatest.wordspec.AnyWordSpec + +class PlayScalaServerCodeGeneratorSpec extends AnyWordSpec with Matchers { + "The Scala server router templates" must { + for { + scala3Sources <- Seq(false, true) + powerApis <- Seq(false, true) + useActions <- Seq(false, true) + } { + s"pass the actor system correctly with scala3Sources=$scala3Sources, powerApis=$powerApis and useActions=$useActions" in { + val service = Service( + "descriptor", + "example", + "GreeterService", + "example.GreeterService", + Nil, + powerApis, + useActions, + ServiceOptions.getDefaultInstance, + None, + BadCitizen.scalaCompatConstants(scala3Sources), + ) + val source = + if (useActions) templates.PlayScala.txt.RouterUsingActions(service, powerApis).body + else templates.PlayScala.txt.Router(service, powerApis).body + val name = if (powerApis) "GreeterServicePowerApi" else "GreeterService" + val prefix = if (useActions) "serviceName" else "GreeterService.name" + val systemArgument = if (scala3Sources) "using system" else "system" + source should include(s"${name}Handler(this, $prefix, eHandler)($systemArgument)") + } + } + } +} diff --git a/play-interop-test-scala/src/test/scala/play/grpc/PlayScalaRouterSpec.scala b/play-interop-test-scala/src/test/scala/play/grpc/PlayScalaRouterSpec.scala index 4af26e87..7b165dc6 100644 --- a/play-interop-test-scala/src/test/scala/play/grpc/PlayScalaRouterSpec.scala +++ b/play-interop-test-scala/src/test/scala/play/grpc/PlayScalaRouterSpec.scala @@ -35,7 +35,8 @@ import play.api.mvc.request.RequestTarget import play.api.mvc.request.Scheme import play.api.mvc.request.TransportConnection import play.api.mvc.Headers -import GreeterServiceMarshallers._ +import GreeterServiceMarshallers.HelloReplySerializer +import GreeterServiceMarshallers.HelloRequestSerializer class PlayScalaRouterSpec extends AnyWordSpec with Matchers with BeforeAndAfterAll with ScalaFutures { implicit val sys: ActorSystem = ActorSystem() diff --git a/project/ProjectExtensions.scala b/project/ProjectExtensions.scala index 84a0d33f..af01297f 100644 --- a/project/ProjectExtensions.scala +++ b/project/ProjectExtensions.scala @@ -20,6 +20,10 @@ object ProjectExtensions { // Defaults to `Seq("Scala")` so we only need to add Java ReflectiveCodeGen.generatedLanguages += PekkoGrpc.Java, ReflectiveCodeGen.codeGeneratorSettings -= "flat_package", // avoid Java+Scala fqcn conflicts + // Match the sbt plugin's Scala 3 source generation in the integration fixtures. + ReflectiveCodeGen.codeGeneratorSettings ++= { + if (scalaBinaryVersion.value == "3") Seq("scala3_sources") else Seq.empty + }, ) } }