scalaPb deletes src_managed before compilation and deletes all other created files. How to disable?

228 views
Skip to first unread message

Martin T.

unread,
Nov 6, 2017, 5:29:30 PM11/6/17
to ScalaPB
Hi,

I am observing that scalapb is cleaning the src_managed directory prior to the proto compilation.
This deletes generated source files which are added by sbt's sourceGenerators.

## version
`scalaVersion=2.12.4`
`sbt.version=1.0.2`

## operating system
`Microsoft Windows [Version 6.1.7601]`

## build.sbt - shortened
```scala
lazy val scalaPBSettings = Seq(
  PB
.pythonExe := "C:\\Python27\\python.exe",
  PB
.protocVersion := "-v340",
  PB
.targets in Compile := Seq(
    scalapb
.gen(flatPackage = true, grpc = false) -> (sourceManaged in Compile).value
 
),
  PB
.protoSources in Compile += PB.externalIncludePath.value / "google" / "type",
  PB
.includePaths in Compile -= PB.externalIncludePath.value / "google" / "type"
)
val scalapbVersion
= com.trueaccord.scalapb.compiler.Version.scalapbVersion
lazy val scalaPB
= Seq(
 
"com.trueaccord.scalapb" %% "scalapb-runtime"          %  scalapbVersion %  "protobuf",
 
"com.google.api.grpc"    %  "googleapis-common-protos" %  "0.0.3"        %  "protobuf"
}

lazy val com4jDep
= "org.jvnet.com4j" % "com4j"  % "2.+"
lazy val tlbimp
= taskKey[Seq[File]]("Source generator for dll")
tlbimp
:= {
  val importer
= new com4j.tlbimp.driver.AntTaskImpl()
  importer
.setDestDir((sourceManaged in Compile).value)
  importer
.setFile(new File(((unmanagedResources in Compile).value / "someLegacyApi.x64.dll").absString))
  importer
.setPackage("some.legacy.api")
  importer
.execute()
 
PathFinder((sourceManaged in Compile).value / "some" / "legacy" / "api").**("*.java").get
}

sourceGenerators
in Compile += tlbimp.taskValue
```
## plugins.sbt
```scala
addSbtPlugin("com.thesamet"     %  "sbt-protoc"            %  "0.99.12")
addSbtPlugin
("com.typesafe.sbt" %  "sbt-git"               %  "0.9.3")
addSbtPlugin
("org.scalastyle"   %% "scalastyle-sbt-plugin" %  "1.0.0")
addSbtPlugin
("com.typesafe.sbt" %  "sbt-native-packager"   %  "1.2.2")

libraryDependencies
++= Seq(
 
"com.trueaccord.scalapb"  %% "compilerplugin" %  "0.6.6",
 
"org.slf4j"               %  "slf4j-nop"      %  "1.7.21",
 
"org.jvnet.com4j"         %  "tlbimp"         %  "2.+"
)
```
## output --shortened
$ sbt clean compile
[info] Loading settings from idea.sbt ...
[info] Loading global plugins from C:\Users\user\.sbt\1.0\plugins
Waiting for lock on C:\Users\user\.ivy2\.sbt.ivy.lock to be available...
[info] Loading settings from plugins.sbt ...
[info] Loading project definition from C:\cygwin64\home\user\sourcecode\app\project
[info] Loading settings from build.sbt ...
[info] Set current project to app (in build file:/C:/cygwin64/home/user/sourcecode/app/)
[info] Executing in batch mode. For better performance use sbt's shell
[success] Total time: 1 s, completed 06-Nov-2017 22:49:32
[info] Updating {file:/C:/cygwin64/home/user/sourcecode/app/}root...
Generating definitions from LegacyLib
[info] Compiling 21 protobuf files to C:\cygwin64\home\user\sourcecode\app\target\scala-2.12\src_managed\main
.
.
.
protoc-jar: protoc version: 340, detected platform: windows 7/amd64
protoc-jar: executing: [C:\cygwin64\tmp\protocjar2493774537079605891\bin\protoc.exe, --plugin=protoc-gen-scala=C:\cygwin64\tmp\protocbridge752459294302360679.bat, --scala_out=flat_package:C:\cygwin64\home\user\sourcecode\app\target\scala-2.12\src_managed\main, -IC:\cygwin64\home\user\sourcecode\app\src\main\protobuf, -IC:\cygwin64\home\
user\sourcecode\app\target\protobuf_external, ...]
[info] Compiling protobuf
[info] Protoc target directory: C:\cygwin64\home\
user\sourcecode\app\target\scala-2.12\src_managed\main
[info] Compiling 48 Scala sources and 4 Java sources to C:\cygwin64\home\r892107\sourcecode\xenomorph\target\scala-2.12\classes ...
[error] source file 'C:\cygwin64\home\
user\sourcecode\app\target\scala-2.12\src_managed\main\some\legacy\api\Factory.java' could not be found
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 32 s, completed 06-Nov-2017 22:50:04

## expected behavior
generates scala code and compiles
## actual behavior
compilation error due to deleted source files

So it creates the code from task  `tlbimp` via sourceGenerators when it reaches '
Generating definitions from LegacyLib' but when 'Compiling protobuf...' starts it deletes everything from src_managed and the 4 java files are gone, see:
[error] source file 'C:\cygwin64\home\user\sourcecode\app\target\scala-2.12\src_managed\main\some\legacy\api\Factory.java' could not be found

Nadav Samet

unread,
Nov 6, 2017, 5:42:47 PM11/6/17
to Martin T., ScalaPB
Hi Martin,

There are a few ways to work around it. 

The one I recommend is to have ScalaPB to generate to a subdirectory of src_managed, so it would only delete the subdirectory:
  PB.targets in Compile := Seq(

    scalapb
.gen(flatPackage = true, grpc = false) -> (sourceManaged in Compile).value / "protos"
  
),

The other option is to disable deletion. The problem with that is that if you rename messages or delete proto files, you may have stale files linger files. They take space and slow down compilation at best, but could also cause compilation errors if name conflicts occur later. When going that route, you'll need to do manual clean ups when necessary:

PB.deleteTargetDirecoty in Compile := false


--
You received this message because you are subscribed to the Google Groups "ScalaPB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalapb+unsubscribe@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalapb/00099b5f-6acf-44c4-93ea-20bf92e17d48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-Nadav
Reply all
Reply to author
Forward
0 new messages