scalascalapb

zio-grpc with Scala 3 (2nd try)


Does anyone have a bare-bones zio-grpc server, with codegen in the project also, working with Scala 3?

I started with the HelloWorld project from their repo and attempted to get it to build with scalaVersion := "3.1.0"

Here is the relevant section in plugins.sbt:

libraryDependencies ++= Seq(
  "com.thesamet.scalapb.zio-grpc" % "zio-grpc-codegen_2.13" % zioGrpcVersion,
  "com.thesamet.scalapb" % "compilerplugin_2.13" % "0.11.1"
)

excludeDependencies ++= Seq(
  ExclusionRule("org.scala-lang.modules", "scala-collection-compat_2.12"),
  ExclusionRule("com.thesamet.scalapb", "protoc-bridge_2.12")
)

and in build.sbt:

libraryDependencies ++= Seq(
  "io.grpc" % "grpc-netty" % grpcVersion,
  "com.thesamet.scalapb" % "scalapb-runtime-grpc_2.13" % scalapb.compiler.Version.scalapbVersion
)

excludeDependencies ++= Seq(
  ExclusionRule("org.scala-lang.modules", "scala-collection-compat_2.12"),
  ExclusionRule("com.thesamet.scalapb", "protoc-bridge_2.12")
)

Since Scala 3 can use 2.13 libraries, that's what I'm doing. (Of the three zio-grpc-related libs, one, zio-grpc-codegen, does not have a Scala 3 version, so 2.13 must be used at least for that one.)

I get this error from sbt with the versions as above:

java.lang.NoSuchMethodError: scala.package$.Seq()Lscala/collection/immutable/Seq$; at protocbridge.gens$.java(gens.scala:17) at protocbridge.gens$.(gens.scala:11)

If I remove either of the scala-collection-compat exclusions, we get

[error] Modules were resolved with conflicting cross-version suffixes in ProjectRef(uri("file:/Users/xxx/dev/zio-grpc/examples/helloworld/project/"), "helloworld-build"): [error] com.thesamet.scalapb:protoc-bridge _2.12, _2.13 [error] com.thesamet.scalapb:compilerplugin _3, _2.13

In short, I cannot find any permutation of Scala 2.13/3 versions of zio-grpc-codegen, compilerplugin_3, and scalapb-runtime-grpc that will not give some sbt conflicting cross-version suffix error.


Solution

  • TL;DR: this is not possible yet as some of the code you are using rely on macros and is not yet published for Scala3.


    SBT plugins runs with Scala 2.12 no matter which Scala version is used in your project, thus you don't have to try to use plugins with _2.13 or _3 suffix, just use the regular syntax that will actually pick _2.12 artifacts.

    That is, in plugins.sbt:

    libraryDependencies ++= Seq(
      "com.thesamet.scalapb.zio-grpc" %% "zio-grpc-codegen" % zioGrpcVersion,
      "com.thesamet.scalapb" %% "compilerplugin" % "0.11.8"
    )
    

    (No need for any exclusion).

    You can confirm this by looking at sbt logs and you should see that it downloads plugins for version 2.12 of Scala:

    ...
    https://somerepository.com/com/thesamet/scalapb/zio-grpc/zio-grpc-codegen_2.12/0.5.1/zio-grpc-codegen_2.12-0.5.1.pom
    https://somerepository.com/com/thesamet/scalapb/compilerplugin_2.12/0.11.8/compilerplugin_2.12-0.11.8.pom
    ...
    

    Once you do that, you'll get an error as following dependencies do not exist:

    For the 1st and 2nd, you just have to update the compilerplugin version to 0.11.8 as I did already above (the compilerplugin version is used for the main dependency scalapb-runtime-grpc).

    For the 3rd, unfortunately it's not published yet for Scala 3. One attempt is to force the _2.13 version for this one with something like that in the build.sbt:

    libraryDependencies += ("com.thesamet.scalapb.zio-grpc" %% "zio-grpc-core" % "0.5.1") cross CrossVersion.for3Use2_13
    
    excludeDependencies += "com.thesamet.scalapb.zio-grpc" % "zio-grpc-core_3"
    

    But this doesn't compile with some errors related to macros, and that is something that is not compatible between Scala 2.13 and 3. You cannot workaround that.


    Remember you can check available versions of a lib for a Scala version on Maven central: