简体   繁体   中英

scalapb and custom source generator conflict

I wish to add a custom source generator to sbt and use it along with scalapb, the Scala protocol buffer generator. Each works by itself. However when both are combined the project fails to compile the first time after a clean. If I run compile again, it succeeds.

name := "Foo root project"

scalaVersion in ThisBuild:= "2.12.1"

sourceGenerators in Compile += Def.task {
  val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
  IO.write(file, """object Test extends App { println("Hi") }""")
  Seq(file)
}.taskValue

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

The error message:

[error] source file '/ ... /target/scala-2.12/src_managed/main/demo/Test.scala' could not be found
[error] one error found
[error] (compile:compileIncremental) Compilation failed

To reproduce this error you will need at least one proto file in src/main/protobuf.

It puzzles me that two source generators, my custom task and scalapb would conflict. Shouldn't they just both write to the src_managed directories? Am I missing some fundamental sbt concept?

There is a known issue with sbt-protoc that makes it delete the sources in the src managed directory.

Option 1: Make ScalaPB generate to a subdirectory of srcManaged so it only deletes that directory:

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

Option 2: Make ScalaPB not delete the subdirectory, but you'll have to clean it yourself from time to time (for example when a protobuf message get renamed):

PB.deleteTargetDirectory in Compile := false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM