Skip to content

Commit cb688bf

Browse files
authored
feat: add examples of Rust and TypeScript (#53)
* feat: demonstrate rust client/server * feat: demonstrate TypeScript node client * feat: add typescript server * refactor: remove non-functional cc toolchain Rust depends on having one that works. We can find a more surgical way to prevent protoc cc_binary from getting built. * chore: missing file * refactor: move to tools
1 parent 3599677 commit cb688bf

34 files changed

+1227
-107
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ bazel-*
22
.bazelrc.user
33
.idea/
44
.ijwb/
5+
node_modules/
56
# Ignore until it is more stable
67
MODULE.bazel.lock

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
docs/*.md
2+
examples/**/*_pb.d.ts

examples/.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

examples/.bazelrc

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ common --@aspect_rules_py//py:interpreter_version=3.9.18
44

55
# Force rules_go to disable CGO even though we have a (fake) C++ toolchain registered.
66
common --host_platform=//tools:no_cgo_host_platform
7+
common --@aspect_rules_ts//ts:skipLibCheck=always
8+
common --@aspect_rules_ts//ts:default_to_tsc_transpiler

examples/BUILD.bazel

+12-23
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
load("@rules_go//proto:def.bzl", "go_proto_library")
2-
load("@rules_proto//proto:defs.bzl", "proto_library")
3-
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
1+
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
2+
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")
3+
load("@npm//:defs.bzl", "npm_link_all_packages")
44

55
package(default_visibility = ["//visibility:public"])
66

7-
proto_library(
8-
name = "foo_proto",
9-
srcs = ["foo.proto"],
10-
deps = ["@com_google_protobuf//:any_proto"],
7+
copy_to_bin(
8+
name = "package_json",
9+
srcs = ["package.json"],
1110
)
1211

13-
py_proto_library(
14-
name = "foo_py_proto",
15-
deps = [":foo_proto"],
16-
)
17-
18-
# Broken by https://github.com/protocolbuffers/protobuf/pull/19679
19-
# which causes building C++ code from source.
20-
# TODO: re-enable once protobuf honors the toolchain
21-
# java_proto_library(
22-
# name = "foo_java_proto",
23-
# deps = [":foo_proto"],
24-
# )
12+
# Link all direct dependencies in /package.json to
13+
# bazel-bin/node_modules
14+
npm_link_all_packages(name = "node_modules")
2515

26-
go_proto_library(
27-
name = "foo_go_proto",
28-
importpath = "example.com/foo_proto",
29-
proto = ":foo_proto",
16+
ts_config(
17+
name = "tsconfig",
18+
src = "tsconfig.json",
3019
)

examples/MODULE.bazel

+98
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
bazel_dep(name = "toolchains_protoc", version = "0.0.0")
22
bazel_dep(name = "aspect_bazel_lib", version = "2.11.0")
3+
bazel_dep(name = "aspect_rules_js", version = "2.2.0")
34
bazel_dep(name = "aspect_rules_py", version = "1.3.2")
5+
bazel_dep(name = "aspect_rules_ts", version = "3.5.1")
46
bazel_dep(name = "platforms", version = "0.0.11")
57
bazel_dep(name = "protobuf", version = "29.3")
68
bazel_dep(name = "rules_java", version = "8.6.3")
79
bazel_dep(name = "rules_proto", version = "7.1.0")
810
bazel_dep(name = "rules_python", version = "1.2.0-rc0")
11+
bazel_dep(name = "rules_rust", version = "0.59.1")
12+
bazel_dep(name = "rules_rust_prost", version = "0.59.1")
913
bazel_dep(name = "rules_go", version = "0.53.0")
1014
bazel_dep(name = "rules_uv", version = "0.56.0")
1115

@@ -72,3 +76,97 @@ http_jar(
7276
sha256 = "0532ad1024d62361561acaedb974d7d16889e7670b36e23e9321dd6b9d334ef9",
7377
urls = ["https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/4.27.0-RC3/protobuf-java-4.27.0-RC3.jar"],
7478
)
79+
80+
####### RUST ##########
81+
RUST_EDITION = "2021"
82+
83+
RUST_VERSION = "1.79.0"
84+
85+
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
86+
rust.toolchain(
87+
edition = RUST_EDITION,
88+
versions = [RUST_VERSION],
89+
)
90+
use_repo(rust, "rust_toolchains")
91+
92+
register_toolchains("@rust_toolchains//:all")
93+
94+
# Proto toolchain
95+
96+
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
97+
98+
# protobuf / gRPC dependencies
99+
crate.spec(
100+
package = "prost",
101+
version = "0.13.1",
102+
)
103+
crate.spec(
104+
default_features = False,
105+
package = "prost-types",
106+
version = "0.13.1",
107+
)
108+
crate.spec(
109+
features = ["transport"],
110+
package = "tonic",
111+
version = "0.12.1",
112+
)
113+
crate.spec(
114+
package = "tonic-build",
115+
version = "0.12.1",
116+
)
117+
crate.spec(
118+
package = "protoc-gen-prost",
119+
version = "0.4.0",
120+
)
121+
crate.annotation(
122+
crate = "protoc-gen-prost",
123+
gen_binaries = ["protoc-gen-prost"],
124+
)
125+
crate.spec(
126+
package = "protoc-gen-tonic",
127+
version = "0.4.0",
128+
)
129+
crate.annotation(
130+
crate = "protoc-gen-tonic",
131+
gen_binaries = ["protoc-gen-tonic"],
132+
)
133+
crate.spec(
134+
default_features = False,
135+
features = [
136+
"macros",
137+
"net",
138+
"rt-multi-thread",
139+
"signal",
140+
],
141+
package = "tokio",
142+
version = "1.38",
143+
)
144+
crate.from_specs()
145+
use_repo(crate, "crates")
146+
147+
####### TYPESCRIPT ##########
148+
npm = use_extension(
149+
"@aspect_rules_js//npm:extensions.bzl",
150+
"npm",
151+
dev_dependency = True,
152+
)
153+
154+
pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
155+
156+
npm.npm_translate_lock(
157+
name = "npm",
158+
pnpm_lock = "//:pnpm-lock.yaml",
159+
)
160+
use_repo(npm, "npm")
161+
162+
use_repo(pnpm, "pnpm")
163+
164+
rules_ts_ext = use_extension(
165+
"@aspect_rules_ts//ts:extensions.bzl",
166+
"ext",
167+
dev_dependency = True,
168+
)
169+
rules_ts_ext.deps(
170+
ts_version_from = "//:package.json",
171+
)
172+
use_repo(rules_ts_ext, "npm_typescript")

examples/WORKSPACE.bazel

Whitespace-only changes.

examples/foo.proto

-11
This file was deleted.

examples/go/BUILD

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@rules_go//go:def.bzl", "go_test")
22

33
go_test(
4-
name = "foo_proto_test",
5-
srcs = ["foo_proto_test.go"],
6-
deps = ["//:foo_go_proto"],
4+
name = "greeter_proto_test",
5+
srcs = ["greeter_proto_test.go"],
6+
deps = ["//proto:greeter_go_proto"],
77
)

examples/go/foo_proto_test.go

-16
This file was deleted.

examples/go/greeter_proto_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package proto_test
2+
3+
import (
4+
"testing"
5+
6+
"example.com/greeter_proto"
7+
)
8+
9+
func TestFoo(t *testing.T) {
10+
msg := &greeter_proto.HelloReply{
11+
Message: "hello world",
12+
}
13+
if msg.Message != "hello world" {
14+
t.Fail()
15+
}
16+
}

examples/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"dependencies": {
3+
"@bufbuild/protobuf": "^2.2.4",
4+
"@connectrpc/connect-node": "2.0.2",
5+
"@connectrpc/connect-fastify": "~2.0.2",
6+
"fastify": "~5.2.1"
7+
},
8+
"devDependencies": {
9+
"@bufbuild/protoc-gen-es": "~2.2.4",
10+
"@connectrpc/connect": "~2.0.2",
11+
"typescript": "5.7.2",
12+
"@types/node": "~22.13.10"
13+
},
14+
"type": "module"
15+
}

0 commit comments

Comments
 (0)