1

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?

James Matlik
  • 185
  • 2
  • 8

1 Answers1

3

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.

thesamet
  • 6,382
  • 2
  • 31
  • 42
  • In the rare case someone else looks for a way to generate only the `FileDescriptorSet`, `sbt-protoc` happens to provide a generator for that purpose: `Compile / PB.targets += (PB.gens.descriptorSet -> (Compile / crossTarget).value / "my.fds")` – rdesgroppes Apr 14 '21 at 13:37