Reputation: 2513
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?
Upvotes: 0
Views: 561
Reputation: 6582
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
Upvotes: 6