How to integrate sbt-protoc and scalapb with sbt cross build?

262 views Asked by At

I have a library that needs two different versions of "com.thesamet.scalapb" %% "compilerplugin" depending on the Scala version.

In my project/scalapb.sbt I have this code:

def scalapbVersion(version:String): String =
  if(version == "2.11") {
    println(s">>>>>>>> Using 0.9.7 to fix 2.11 compat. ${version}")
    "0.9.7"
  } else {
    println(s">>>>>>>> Using last version. ${version}")
    "0.10.2"
  }

libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % scalapbVersion(scalaBinaryVersion.value)

Executing sbt clean "++2.11.12 compile I get >>>>>>>> Using lastest version. 2.12 but in the logs, also, I can see that the cross-build plugin changes the version to Scala 2.11 after the previous message:

[info] Setting Scala version to 2.11.12 on 13 projects.
[info] Excluded 1 projects, run ++ 2.11.12 -v for more details.

So I suppose that the order is:

  1. sbt load plugins configuration with the default Scala version.
  2. cross-build changes the scala version

How to integrate sbt-protoc with sbt cross-build?

1

There are 1 answers

1
thesamet On BEST ANSWER

sbt files in the project directory are evaluated before a specific scala version is picked up for cross building. This is why passing ++2.11.12 has no effect on scalaBinaryVersion in the context of project/scalapb.sbt.

Using different versions of compilerplugin in a single build is not officially supported at this point, but there are a few workarounds that you can try:

  1. Download scalapbc for the version of ScalaPB you would like to use. Write a shell script that generates sources using ScalaPBC. Check in the generated sources into your code repository. Manually add scalapb-runtime into your libraryDependencies in build.sbt:
libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime" % (if (scalaVersion.value == "2.12.10") "0.10.8" else "0.9.7")
  1. Use 0.9.7 for all scala versions.

  2. If it's reasonable for your situation, consider dropping Scala 2.11 support as Scala 2.11 reached end-of-life a few years ago.