How to define Compile settings in sbt multi-project?

482 Views Asked by At

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.
1

There are 1 best solutions below

0
On

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
  )