Skip to main content

Package-scoped options

Package-scoped options let you share customizations across proto files, including third-party files that you cannot edit. For rules that select fields by their types or options, see field transformations.

Package-scoped options​

Note: this option is available in ScalaPB 0.8.2 and later.

Sometimes you want to have the same file-level options applied to all the proto files in your project. To accomplish that, add a package.proto file (the name does not matter) next to your proto files that looks like this:

import "scalapb/scalapb.proto";

package com.mypackage;

option (scalapb.options) = {
scope: PACKAGE
flat_package: true
};

All the options in this file will be applied to all proto files in the package com.mypackage and its sub-packages.

There is no need to explicitly import this file from other protos. If you are using sbt-protoc and the file is in the proto source directory (default is src/main/protobuf) then the file will be found and the options applied. If you are invoking protoc in another way, you need to ensure that this file is passed to protoc together with the rest of the files.

If you are generating Scala code for proto files that you don't own, you can use this feature to customize code generation by creating a package.proto file for that third-party package and include it within your proto source directory.

The following rules are applied when validating package-scoped options:

  • At most one file in each package may provide package-scoped options.
  • Sub-packages may override package-scoped options provided by their parent packages. The options are merged using the Protocol Buffers mergeFrom semantics. Specifically, this implies that repeated fields such as import and preamble are concatenated.
  • Proto files get the most specific package-scoped options for the package they are in. File-level options defined in a proto file get merged with the package-level options using mergeFrom.
  • Proto files with package-scoped options must have a package statement. This is to prevent the possibility of options applied globally. Standard classes that are shipped with ScalaPB already assume certain options, so overriding options globally may lead to compilation errors.

Flat packages and dependencies​

Prefer file- or package-scoped flat_package options over the generator-wide flatPackage option. Keeping package layout in the proto configuration makes it consistent across builds and avoids changing how unrelated imports are resolved.

The generator option scalapb.gen(flatPackage = true) (or flat_package on the command line) sets the default package layout for both the protos being generated and their imports. File- and package-scoped flat_package settings take precedence. The global option does not change the layout of the bundled google.protobuf and scalapb proto packages.

When importing protos whose Scala classes are already compiled in a dependency, the package layout must match the one used to build that dependency. For example, a message Widget in model.proto with package acme normally generates acme.model.Widget. Enabling the global option makes references to that message use acme.Widget unless the dependency's proto options override it. If the JAR contains only acme.model.Widget, the generated code will fail to compile.

For libraries that publish protos and generated Scala classes, prefer file- or package-scoped options so consumers can use the same layout. Set flat_package: false explicitly when publishing non-flat classes to prevent a consumer's global flag from changing references to them. The package-scoped example above enables flat packaging only for com.mypackage and its subpackages; this scope follows the proto package, not java_package or ScalaPB's package_name. Use the publishing instructions below to make those options available to downstream builds.

Publishing package-scoped options​

If you are publishing a library that includes protos with package-scoped options, you need to make sure your library users source the package-scoped option proto file so the customizations are applied when they generate code.

Your users can simply import your package-scoped options from any proto file in their project to have the settings applied (a single import of the package-scoped options file makes its settings available to matching packages throughout that generator invocation). However, since ScalaPB 0.10.11 and sbt-protoc 1.0.1, sbt-protoc provides a way to automate this with no need to manually import the package-scoped options file. This is accomplished by including a special attribute in the manifest of the library you publish. Add the following to your library's settings:

Compile / packageBin / packageOptions += (
Package.ManifestAttributes("ScalaPB-Options-Proto" -> "path/to/package.proto")
)

The path above is relative to the root directory of the published JAR (so src/main/protobuf is not needed). Users add your library to their projects like this:

libraryDependencies ++= Seq(
"com.example" %% "your-library" % "0.1.0",
"com.example" %% "your-library" % "0.1.0" % "protobuf"
)

The first dependency provides the precompiled class files. The second dependency makes it possible for users to import the protos in the jar file. sbt-protoc will look for the ScalaPB-Options-Proto attribute in the jar's manifest and automatically add the package scoped options file to the protoc command line.

note

Since the package-scoped options file is used as a source file in multiple projects, it should not define any types (messages, enums, services). This ensures that the package-scoped proto file does not generate any code on its own so we don't end up with duplicate class files.

Disabling package-scoped options processing​

As a consumer of third-party dependencies that come with options proto, you can disable the behavior of automatically adding the options proto to protoc by setting

Compile / PB.manifestProcessing := false

in sbt. In that case, it is your responsibility to either manually import the option protos in one of your own project source files so it gets applied, or ensure that the generator settings used in your project are consistent with the ones used to generate the dependency. Differences in settings can lead to generated code that does not compile.

Auxiliary options​

In some situations, you may want to set some options in a proto file, but without modifying the original proto file or adding anything ScalaPB-specific to it. To accomplish that, you can define auxiliary options under package-scoped options.

For example, if you are given this proto file:

syntax = "proto3";
package a.b.c;
message Foo {
string hello = 1;
}

You can add a file package.proto with the following content:

syntax = "proto3";
package a.b.c;
import "scalapb/scalapb.proto";
option (scalapb.options) = {
scope: PACKAGE
aux_message_options: [
{
target: "a.b.c.Foo"
options: {
extends: "com.myexample.SomeTrait"
}
}
]
aux_field_options: [
{
target: "a.b.c.Foo.hello"
options: {
scala_name: "goodbye"
}
}
]
};

The list aux_message_options contains options targeted at different messages defined under the same proto package of the package-scoped options. The target name needs to be fully-qualified message name in the protobuf namespace. Similar to aux_message_options, we also have aux_enum_options, aux_enum_value_options and aux_field_options. See example usage here. If the target is set to * then the options will be applied to all the entities in the file or package (depending on the scope option).

Making third-party models package-private​

Since ScalaPB 0.11.22 (and 1.0.0), enum companion_annotations and file-level object_annotations complement the message annotation options. Include a package.proto like this in the generation request alongside the third-party protos:

syntax = "proto3";
package vendor.proto;
import "scalapb/scalapb.proto";

option (scalapb.options) = {
scope: PACKAGE
package_name: "com.example.internal.proto"
object_annotations: "private[internal]"
aux_message_options: [{
target: "*"
options: {
annotations: "private[internal]"
companion_annotations: "private[internal]"
}
}]
aux_enum_options: [{
target: "*"
options: {
base_annotations: "private[internal]"
companion_annotations: "private[internal]"
}
}]
};

The options apply to vendor.proto and its subpackages without modifying their proto files. The visibility qualifier must name an enclosing generated Scala package. This hides message classes and companions, enum classes and companions, and the per-file descriptor objects. Ordinary oneofs are nested inside message companions and inherit their enclosing visibility. These options do not change gRPC service visibility.

object_annotations can also be set at file scope, and enum companion_annotations directly on an enum. Both accept repeated strings, so annotations can be combined with a visibility modifier. They work with both single-file and multiple-file generation; neither adds anything by default.