scalasbtsbt-pluginscalapb

sbt-protoc error File does not reside within any path specified using --proto_path (or -I).You must specify a --proto_path which encompasses this file


I've scala grpc protobuf project with below build.sbt.

ThisBuild / version := "0.1.0-SNAPSHOT"

val openRtbCoreVersion = "1.5.5"
val googleCommonProtosVersion = "1.12.0"

val commonSettings: Seq[Def.Setting[_]] = Seq[Def.Setting[_]](
  scalaVersion := "2.12.14",
  organization := "com.td",
)

def scalaGrpcSettings: Seq[Def.Setting[_]] = Seq[Def.Setting[_]](
  libraryDependencies += "com.thesamet.scalapb"  %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion,
  libraryDependencies += "com.google.api.grpc" % "proto-google-common-protos" % googleCommonProtosVersion % "protobuf",

  PB.targets in Compile := Seq(
    PB.gens.java -> (sourceManaged in Compile).value,
    scalapb.gen(javaConversions = true) -> (sourceManaged in Compile).value
  ),

  PB.includePaths in Compile := Seq(
    target.value / "protobuf_external",
),
  PB.protoSources in Compile := Seq(
    PB.externalIncludePath.value / "google" / "api",
    target.value / "protobuf_external",
    new File("definitions/common")
  ),

  PB.additionalDependencies := Nil
)

lazy val root = (project in file("."))
  .settings(
    name := "proto-path-error"
  ).settings(scalaGrpcSettings)

project/plugins.sbt file is -

addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.20")

libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.0"

project/build.properties is -

sbt.version = 1.3.13

The src/main/protobuf/definitions/common/common.proto -

syntax = "proto2";

option java_outer_classname = "CommonUtils";
package com.td.protobuf;

message CMap {
    map<int32, Assets> cs  = 1;
}

message Assets {
    repeated int32 assets = 1;
}

The whole project is on github here

This project is working fine. I want to update the sbt-protoc plugin in project/plugins.sbt to 1.0.0.

After I upgrade the sbt-protoc version to 1.0.0 I get below error when I do sbt clean compile -

  /home/rajkumar/Coding/scala/proto-path-error/definitions/common/common.proto: File does not reside within any path specified using --proto_path (or -I).  You must specify a --proto_path which encompasses this file.  Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
  [error] java.lang.RuntimeException: protoc returned exit code: 1
  [error]         at scala.sys.package$.error(package.scala:30)
  [error]         at sbtprotoc.ProtocPlugin$.compile(ProtocPlugin.scala:438)
  [error]         at sbtprotoc.ProtocPlugin$.compileProto$1(ProtocPlugin.scala:537)
  [error]         at sbtprotoc.ProtocPlugin$.$anonfun$sourceGeneratorTask$12(ProtocPlugin.scala:544)
  [error]         at sbt.util.FileFunction$.$anonfun$cached$1(FileFunction.scala:73)
  [error]         at sbt.util.FileFunction$.$anonfun$cached$4(FileFunction.scala:146)
  [error]         at sbt.util.Difference.apply(Tracked.scala:323)
  [error]         at sbt.util.Difference.apply(Tracked.scala:303)
  [error]         at sbt.util.FileFunction$.$anonfun$cached$3(FileFunction.scala:142)
  [error]         at sbt.util.Difference.apply(Tracked.scala:323)
  [error]         at sbt.util.Difference.apply(Tracked.scala:298)
  [error]         at sbt.util.FileFunction$.$anonfun$cached$2(FileFunction.scala:141)
  [error]         at sbtprotoc.ProtocPlugin$.$anonfun$sourceGeneratorTask$4(ProtocPlugin.scala:549)
  [error]         at scala.Function1.$anonfun$compose$1(Function1.scala:49)
  [error]         at sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:62)
  [error]         at sbt.std.Transform$$anon$4.work(Transform.scala:67)
  [error]         at sbt.Execute.$anonfun$submit$2(Execute.scala:281)
  [error]         at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:19)
  [error]         at sbt.Execute.work(Execute.scala:290)
  [error]         at sbt.Execute.$anonfun$submit$1(Execute.scala:281)
  [error]         at sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:178)
  [error]         at sbt.CompletionService$$anon$2.call(CompletionService.scala:37)
  [error]         at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
  [error]         at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
  [error]         at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
  [error]         at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  [error]         at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  [error]         at java.base/java.lang.Thread.run(Thread.java:829)
  [error] (Compile / protocGenerate) protoc returned exit code: 1
  [error] Total time: 2 s, completed Sep 9, 2022, 7:15:30 PM

what do I miss in the plugin upgrade. Any idea how can I resolve the error?


Solution

  • There are multiple things to fix in build.sbt to update for sbt-protoc 1.0.x. Perhaps it was relying on old behaviors that were fixed. Let's start first with the error that protoc is reporting:

    File does not reside within any path specified using --proto_path (or -I).  
    You must specify a --proto_path which encompasses this file.  Note that the
    proto_path must be an exact prefix of the .proto file names -- protoc is too
    dumb to figure out when two paths (e.g. absolute and relative) are 
    equivalent (it's harder than you think).
    

    In other words, every file we generate the sources for, need to be under "proto_path". ScalaPB takes cares of setting proto_path for you automatically, unless you somehow circumvent it by manually resetting it with := like in here:

    PB.includePaths in Compile := Seq(
        target.value / "protobuf_external",
    ),
    

    And since protobuf_external is in the proto_path by default (also in older version), those lines are unnecessary and should be removed.

    It appears that you want to unpack the protos that are in proto-google-common-protos. There's a shortcut for that: use protobuf-src instead of protobuf as the scope:

    libraryDependencies += "com.google.api.grpc" % "proto-google-common-protos" % googleCommonProtosVersion % "protobuf-src",
    

    In summary, your config should be:

    def scalaGrpcSettings: Seq[Def.Setting[_]] = Seq[Def.Setting[_]](
      libraryDependencies += "com.thesamet.scalapb"  %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion,
      libraryDependencies += "com.google.api.grpc" % "proto-google-common-protos" % googleCommonProtosVersion % "protobuf-src",
      PB.targets in Compile := Seq(
        PB.gens.java -> (sourceManaged in Compile).value,
        scalapb.gen(javaConversions = true) -> (sourceManaged in Compile).value
      ),
    
      PB.protoSources in Compile ++= Seq(
        new File("definitions/common")
      )
    )