0

I'm trying to configure a new Scala build using sbt multi-project. I want to use sbt-protoc and in particular ScalaPB which requires this configuration setting:

Compile / PB.targets := Seq(
  scalapb.gen() -> (Compile / sourceManaged).value
)

Now the question is how to apply that sbt.librarymanagement.Configurations.Compile configuration correctly in a multi-project. I'm using Scala 2.13 and sbt 1.4.7.

My current build.sbt:

Compile / PB.targets := Seq(
  scalapb.gen() -> (Compile / sourceManaged).value
)

lazy val commonSettings = List(
  scalaVersion := scala212,
  scalacOptions ++= Seq(
    "utf8",
    "-Ypartial-unification"
  )
)

lazy val core = (project in file("core"))
  .settings(
    commonSettings,
    name := "twirp4s-core",
    crossScalaVersions := Seq(scala212, scala213),
    libraryDependencies ++= Seq(
      catsCore,
      circeCore,
      circeGeneric,
      circeParser,
      http4sDsl,
      http4sCirce
    ),
    addCompilerPlugin(betterMonadicForPlugin),
  )

lazy val root = (project in file("."))
  .aggregate(core)
  .settings(
    name := "twirp4s-root",
    libraryDependencies += scalaTest % Test,
    skip in publish := true
  )

When I try to compile my project the compiler says:

[info] Protobufs files found, but PB.targets is empty.
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
sentenza
  • 1,608
  • 3
  • 29
  • 51
  • is `scalapb.gen() -> (core / Compile / sourceManaged).value` what you want? Or maybe just move the whole scalapb thing to be part of `core`'s settings rather than part of the root project's settings? – Seth Tisue Feb 07 '21 at 15:37
  • I don't know how to move that part inside core.settings. Could you please provide some examples? I didn't find any clear documentation about that and I'm just trying to move that manually. I tried to import `sbtprotoc.ProtocPlugin.autoImport.PB` but so far it hasn't worked – sentenza Feb 07 '21 at 15:45
  • How does that `Compile /` thing work exactly? – sentenza Feb 07 '21 at 15:47
  • (Perhaps someone else would like to help you further.) – Seth Tisue Feb 07 '21 at 15:52
  • By the way, moving `Compile / PB.targets` into core.settings works. – sentenza Feb 07 '21 at 16:10

1 Answers1

2

As you have already figured out, and that @Seth suggested in the comments, moving Compile / PB.targets into core.settings works. This is the build.sbt you should use:

lazy val commonSettings = List(
  scalaVersion := scala212,
  scalacOptions ++= Seq(
    "utf8",
    "-Ypartial-unification"
  )
)

lazy val core = (project in file("core"))
  .settings(
    commonSettings,
    name := "twirp4s-core",
    crossScalaVersions := Seq(scala212, scala213),
    libraryDependencies ++= Seq(
      catsCore,
      circeCore,
      circeGeneric,
      circeParser,
      http4sDsl,
      http4sCirce
    ),
    addCompilerPlugin(betterMonadicForPlugin),
    Compile / PB.targets := Seq(
      scalapb.gen() -> (Compile / sourceManaged).value
    )
  )

lazy val root = (project in file("."))
  .aggregate(core)
  .settings(
    name := "twirp4s-root",
    libraryDependencies += scalaTest % Test,
    skip in publish := true
  )
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35