scalaprotocol-buffersscalapb

Set file-level option to scalapb project


I'm using ScalaPB (version 0.11.1) and plugin sbt-protoc (version 1.0.3) to try to compile an old project with ProtocolBuffers in Scala 2.12. Reading the documentation, I want to set the file property preserve_unknown_fields to false. But my question is, where? Where do I need to set this flag? On the .proto file?

I've also tried to include the flag as a package-scoped option by creating a package.proto file next to my other .proto file, with the following content (as it is specified here):

import "scalapb/scalapb.proto";

package eur.astrata.eu.bigdata.tpms.protobuf;

option (scalapb.options) = {
  preserve_unknown_fields: false
};

But when trying to compile, I get the following error:

[libprotobuf WARNING T:\src\github\protobuf\src\google\protobuf\compiler\parser.cc:648] No syntax specified for the proto file: package.proto. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
scalapb/scalapb.proto: File not found.
package.proto:1:1: Import "scalapb/scalapb.proto" was not found or had errors.

I've also tried with syntax = "proto3"; at the beginning but it doesn't work.

Any help would be greatly appreciated.


Solution

  • From the docs:

    If you are using sbt-protoc and importing protos like scalapb/scalapb.proto, or common protocol buffers like google/protobuf/wrappers.proto:

    Add the following to your build.sbt:

    libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"

    This tells sbt-protoc to extract protos from this jar (and all its dependencies, which includes Google's common protos), and make them available in the include path that is passed to protoc.

    It is important to add that by setting preserve_unknown_fields to false you are turning off a protobuf feature that could prevent data loss when different parts of a distributed system are not running the same version of the schema.