rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

What's a good default for protoc-gen-{prost|tonic}'s compile_well_known_types?

Open aaronmondal opened this issue 2 years ago • 2 comments

The compile_well_known_types flag controls whether generated libraries use prost-types or types from generated google.protobuf.* protos.

This flag is currently always enabled here (and a bit further below for tonic): https://github.com/bazelbuild/rules_rust/blob/478213bb114b6c2649f1c2533589ac3962d3bf28/proto/prost/private/prost.bzl#L63

Being enabled by default means that we have to use e.g. timestamp_proto::google::protobuf::Timestamp instead of prost_types::Timestamp.

I'm not sure whether this is a good or a bad thing.

  • It's more precise if users explicitly use the types that are actually written in the proto files.
  • It seems the ergonomics of prost-types are nicer. E.g. a generated timestamp_proto::google::protobuf::Timestamp doesn't implement From<SystemTime>, but prost_types::Timestamp does. Intuitively I could also imagine the specialized prost-types to be more performant.

There is also a protoc-wkt crate that adds more extensive chrono-timestamp interop and other goodies.

Adding configurability for compile_well_known_types seems straightforward, but I'm not sure what the better default would be.

cc @allada @UebelAndre @freeformstu @illicitonion

aaronmondal avatar Jul 07 '23 14:07 aaronmondal

compile_well_known_types could probably be an attribute on the toolchain along with something similar to proto_lang_toolchain.blacklisted_protos so users can specify what should and should not be compiled.

UebelAndre avatar Jul 10 '23 15:07 UebelAndre

probably related to this; I'm struggling to compile proto rust libraries with code that relies on prost types.

To illustrate what I'm doing, I have pushed very small modifications to the bzlmod/proto example , see: https://github.com/bazelbuild/rules_rust/compare/main...joune:rules_rust:wkt-wrappers https://github.com/bazelbuild/rules_rust/compare/main...joune:rules_rust:wkt-timestamp

I get these errors when compiling with bazel:

the trait `From<bool>` is not implemented for `wrappers_proto::google::protobuf::BoolValue`, which is required by `bool: Into<_>`
the trait `From<SystemTime>` is not implemented for `timestamp_proto::google::protobuf::Timestamp`, which is required by `SystemTime: Into<_>`

What would be the recommended way to make this work?

PS: I have even tried removing the compile_well_known_types option from the rule implementation, but that leads to another strange error:

--prost_out: duplicate extern Protobuf path: .google.protobuf.BoolValue

joune avatar Sep 12 '24 19:09 joune

I'm also struggling with the default compile_well_known_types option. It was surprising to me that it was passed by default, when prost's actual default is not to set it. I would like the ability to override it from the toolchain definition.

alexkirsz avatar Jan 31 '25 11:01 alexkirsz