Skip to content

codegen/protobuf/scala: wire up protobuf_sources(grpc=True) (feature parity with the Java backend)#23532

Open
robertpi wants to merge 1 commit into
pantsbuild:mainfrom
robertpi:fix/scalapb-grpc-codegen
Open

codegen/protobuf/scala: wire up protobuf_sources(grpc=True) (feature parity with the Java backend)#23532
robertpi wants to merge 1 commit into
pantsbuild:mainfrom
robertpi:fix/scalapb-grpc-codegen

Conversation

@robertpi

@robertpi robertpi commented Jul 15, 2026

Copy link
Copy Markdown

protobuf_sources(grpc=True) currently has no effect when the target is resolved by the Scala codegen backend (pants.backend.codegen.protobuf.scala) — no *Grpc.scala service stubs are generated and the ScalaPB gRPC runtime is never injected as a dependency. The equivalent Java backend (pants.backend.codegen.protobuf.java) already reads this field correctly; this PR mirrors that existing, accepted pattern for Scala.

Two independent pieces, closing the gap end-to-end:

  1. rules.pygenerate_scala_from_protobuf now reads ProtobufGrpcToggleField off the protocol target and, when set, passes ScalaPB's grpc: colon-modifier via --scala_out=grpc:<dir> (instead of the always-bare --scala_out=<dir>), enabling ScalaPB's built-in gRPC service-stub codegen — the same modifier sbt-protoc/ScalaPB passes automatically whenever a .proto declares a service.
  2. dependency_inference.pyScalaPBRuntimeDependencyInferenceFieldSet now also requires ProtobufGrpcToggleField, and infer_scalapb_runtime_dependency conditionally injects the scalapb-runtime-grpc_<binary_version> artifact plus io.grpc:grpc-stub/grpc-protobuf/grpc-netty-shaded when grpc=True, resolving the correct scala-binary-versioned coordinate the same way the base scalapb-runtime dependency already does. Without this, generated *Grpc.scala files from (1) fail to compile (object grpc is not a member of package scalapb).

No behavior change for existing users who don't set grpc=True on a Scala-resolved protobuf_sources/protobuf_source target — the toggle defaults to False and both code paths are no-ops in that case, exactly as they are today.

Provenance: this fix was originally written and verified against a pinned Pants 2.32.0 install in a downstream monorepo (applied as a direct source patch to the installed distribution, since neither touched @rule is override-able from a plugin — both are the sole concrete rule bound to their respective UnionRule). This PR re-diffs that logic against current main.

Testing: added test_generates_grpc_scala to protobuf/scala/rules_integration_test.py, mirroring the existing protobuf/java/rules_integration_test.py::test_generates_grpc_java and the neighboring test_generates_fs2_grpc_via_jvm_plugin for file-naming conventions.

Closes #23528

protobuf_sources(grpc=True) currently has no effect when the target is
resolved by the Scala codegen backend (pants.backend.codegen.protobuf.scala)
-- no *Grpc.scala service stubs are generated and the ScalaPB gRPC runtime
is never injected as a dependency. The equivalent Java backend
(pants.backend.codegen.protobuf.java) already reads this field correctly;
this PR mirrors that existing, accepted pattern for Scala.

Two independent pieces, closing the gap end-to-end:

1. rules.py -- generate_scala_from_protobuf now reads ProtobufGrpcToggleField
   off the protocol target and, when set, passes ScalaPB's grpc: colon-
   modifier via --scala_out=grpc:<dir> (instead of the always-bare
   --scala_out=<dir>), enabling ScalaPB's built-in gRPC service-stub codegen
   -- the same modifier sbt-protoc/ScalaPB passes automatically whenever a
   .proto declares a service.
2. dependency_inference.py -- ScalaPBRuntimeDependencyInferenceFieldSet now
   also requires ProtobufGrpcToggleField, and infer_scalapb_runtime_dependency
   conditionally injects the scalapb-runtime-grpc_<binary_version> artifact
   plus io.grpc:grpc-stub/grpc-protobuf/grpc-netty-shaded when grpc=True,
   resolving the correct scala-binary-versioned coordinate the same way the
   base scalapb-runtime dependency already does. Without this, generated
   *Grpc.scala files from (1) fail to compile
   (object grpc is not a member of package scalapb).

No behavior change for existing users who don't set grpc=True on a
Scala-resolved protobuf_sources/protobuf_source target -- the toggle
defaults to False and both code paths are no-ops in that case, exactly as
they are today.

Fixes pantsbuild#23528
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

protobuf_sources(grpc=True) has no effect on the Scala codegen backend (works for Java, silently no-ops for Scala)

1 participant