Skip to content

Protobuf TypeScript sources

ts_proto_library generates the direct sources of one proto_library with protobuf-es 2.16.0, then passes those TypeScript files to the existing source-mode ts_compile owner.

ts_proto_library(name = "messages", proto = ":messages_proto", out_dir = "generated", tsconfig = "tsconfig.json", node_modules = ":node_modules", deps = ["@npm//:bufbuild_protobuf"], options = ["target=ts", "json_types=true"])
Attribute Contract
proto One proto_library with direct sources; its graph owns imports and import-prefix mapping.
out_dir Package-relative generated directory. Canonical proto import paths determine nested output paths.
tsconfig, node_modules, deps, type_inputs Existing ts_compile inputs. Declare generated sibling libraries and the consumer's protobuf runtime in deps.
options Options passed directly to protobuf-es, defaulting to target=ts. Supports json_types=true and import_extension. Output must remain TypeScript.

The rule calls protobuf's proto_common.compile with the protoc of protobuf's proto toolchain and the declared protobuf-es plugin. Under Bazel 9 that is the protoc proto_library uses: protobuf's prebuilt binary where one exists for the exec platform, otherwise the source build, which --@protobuf//bazel/toolchains:prefer_prebuilt_protoc=false also forces. The proto dependency enables protobuf's source-info flag to preserve comments. Only direct files are generated; imported TypeScript modules belong to their own generated libraries.

The Bazel target graph selects schemas. The rule does not interpret Buf configuration or apply managed schema options. Schema options affecting generated descriptors belong in the proto sources.

Between protoc and protobuf-es sits //proto/private/bufrequest, which re-encodes protoc's CodeGeneratorRequest the way buf generate v1.73.0 does for a plugin with types or exclude_types: custom option values decoded against the request's own files and marshalled deterministically, and options messages with no fields dropped. The embedded fileDesc(...) bytes are then buf's. Without a type filter buf keeps the empty MethodOptions protoc writes for rpc ... {}; the rule drops it. The wrapper links whichever protobuf-go the module graph selects, at least v1.36.12, buf v1.73.0's.

One library for a proto graph

ts_proto_closure takes protos instead of proto and generates every proto_library they reach through deps, exports and aliases, each with its own protoc action as above, into one source-mode ts_compile. Generated imports resolve inside that library, so deps names only the protobuf runtime. The protobuf module's own schemas are skipped: protobuf-es imports the well-known types from @bufbuild/protobuf/wkt, so bootstrap_wkt=true is refused. A proto counts as a well-known type when its proto_library is in the protobuf module; a non-WKT proto from that module fails the type check. Gazelle neither writes nor indexes ts_proto_closure; it is written by hand.

ts_proto_closure(name = "schema", protos = [":api_proto", ":events_proto"], out_dir = "generated", tsconfig = "tsconfig.json", node_modules = ":node_modules", deps = ["@npm//:bufbuild_protobuf"])

Generated BUILD targets

Gazelle protobuf identities generate wrappers from native proto rules. A common ancestor package owns each product output root; native schema packages retain their own targets. Different plugin options use different identities, while sibling dependencies remain inside each identity.

The selected tsconfig's compiler-observed ambient types use the same dependency projection as ordinary compiler rules. An existing compiler owner becomes a dependency. Eligible declarations without an owner, their observed closure and required package scopes become type_inputs; they remain compiler inputs without joining the generated runtime sources. Their existing Bazel visibility still applies.

Invariants

  1. Canonical proto import paths determine generated paths. The import-prefix fixture fails if generated sibling imports no longer resolve.
  2. Each proto target owns only its direct generated files. The two-library fixture fails on duplicate ownership or missing imported sources.
  3. Generated TypeScript uses the existing source-mode compiler and test runner. The JSON source-runtime fixture fails if emit-disabled generated sources cannot compile or execute.
  4. Embedded descriptors are buf's bytes. //tests/proto:service_buf_descriptor_test fails if an extension value's wire order or an empty options message differs from buf's output.
  5. Selected-config ambient inputs survive separate owner updates and repeated generation. TestProtoAmbientSourceDependencySurvivesSeparateOwnerUpdates fails if either a declared owner or an unowned declaration closure disappears.
  6. A closure generates what its roots reach and none of the runtime's schemas. //tests/proto:closure_files_test fails if a proto reached through deps or exports is missing or descriptor.proto is generated.