Search code examples
scalapb

Generate file descriptor set (.desc) with scalapb


I am using scalapb in a project that needs to have access to the FileDescriptorSet. Is there a way to have scalapb generate the .desc file in addition to all other class files? Or is there some programatic way of obtaining a FileDescriptorSet from what is already generated?


Solution

  • Yes, to both questions.

    If you are using sbt-protoc, you can have the following definition in your SBT file:

    PB.protocOptions in Compile := Seq(
        "--descriptor_set_out=" + 
            (baseDirectory in Compile).value.getParentFile / "src" / "main" / "resources" /"out.desc"
    )
    

    One caveat is that you would have to create src/main/resources yourself, otherwise you would get an error. It would probably be better to generate into resourceManaged (that would also require creating a directory ahead of time, since protoc doesn't do that)

    You can also build a FileDescriptorSet at run time. For each proto file, ScalaPB generates a Scala object with scalaDescriptor (and also javaDescriptor if that's more convenient). The descriptors contains a list of their dependencies which are also FileDesciptors - you can traverse that tree structure and build a list of FileDescriptors which is essentially a FileDescriptorSet.