Skip to content

feat: add examples of Rust and TypeScript #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ bazel-*
.bazelrc.user
.idea/
.ijwb/
node_modules/
# Ignore until it is more stable
MODULE.bazel.lock
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
docs/*.md
examples/**/*_pb.d.ts
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 2 additions & 0 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ common --@aspect_rules_py//py:interpreter_version=3.9.18

# Force rules_go to disable CGO even though we have a (fake) C++ toolchain registered.
common --host_platform=//tools:no_cgo_host_platform
common --@aspect_rules_ts//ts:skipLibCheck=always
common --@aspect_rules_ts//ts:default_to_tsc_transpiler
35 changes: 12 additions & 23 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
load("@rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")
load("@npm//:defs.bzl", "npm_link_all_packages")

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

proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
deps = ["@com_google_protobuf//:any_proto"],
copy_to_bin(
name = "package_json",
srcs = ["package.json"],
)

py_proto_library(
name = "foo_py_proto",
deps = [":foo_proto"],
)

# Broken by https://github.com/protocolbuffers/protobuf/pull/19679
# which causes building C++ code from source.
# TODO: re-enable once protobuf honors the toolchain
# java_proto_library(
# name = "foo_java_proto",
# deps = [":foo_proto"],
# )
# Link all direct dependencies in /package.json to
# bazel-bin/node_modules
npm_link_all_packages(name = "node_modules")

go_proto_library(
name = "foo_go_proto",
importpath = "example.com/foo_proto",
proto = ":foo_proto",
ts_config(
name = "tsconfig",
src = "tsconfig.json",
)
98 changes: 98 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
bazel_dep(name = "toolchains_protoc", version = "0.0.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.11.0")
bazel_dep(name = "aspect_rules_js", version = "2.2.0")
bazel_dep(name = "aspect_rules_py", version = "1.3.2")
bazel_dep(name = "aspect_rules_ts", version = "3.5.1")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "protobuf", version = "29.3")
bazel_dep(name = "rules_java", version = "8.6.3")
bazel_dep(name = "rules_proto", version = "7.1.0")
bazel_dep(name = "rules_python", version = "1.2.0-rc0")
bazel_dep(name = "rules_rust", version = "0.59.1")
bazel_dep(name = "rules_rust_prost", version = "0.59.1")
bazel_dep(name = "rules_go", version = "0.53.0")
bazel_dep(name = "rules_uv", version = "0.56.0")

Expand Down Expand Up @@ -72,3 +76,97 @@ http_jar(
sha256 = "0532ad1024d62361561acaedb974d7d16889e7670b36e23e9321dd6b9d334ef9",
urls = ["https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/4.27.0-RC3/protobuf-java-4.27.0-RC3.jar"],
)

####### RUST ##########
RUST_EDITION = "2021"

RUST_VERSION = "1.79.0"

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = RUST_EDITION,
versions = [RUST_VERSION],
)
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

# Proto toolchain

crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")

# protobuf / gRPC dependencies
crate.spec(
package = "prost",
version = "0.13.1",
)
crate.spec(
default_features = False,
package = "prost-types",
version = "0.13.1",
)
crate.spec(
features = ["transport"],
package = "tonic",
version = "0.12.1",
)
crate.spec(
package = "tonic-build",
version = "0.12.1",
)
crate.spec(
package = "protoc-gen-prost",
version = "0.4.0",
)
crate.annotation(
crate = "protoc-gen-prost",
gen_binaries = ["protoc-gen-prost"],
)
crate.spec(
package = "protoc-gen-tonic",
version = "0.4.0",
)
crate.annotation(
crate = "protoc-gen-tonic",
gen_binaries = ["protoc-gen-tonic"],
)
crate.spec(
default_features = False,
features = [
"macros",
"net",
"rt-multi-thread",
"signal",
],
package = "tokio",
version = "1.38",
)
crate.from_specs()
use_repo(crate, "crates")

####### TYPESCRIPT ##########
npm = use_extension(
"@aspect_rules_js//npm:extensions.bzl",
"npm",
dev_dependency = True,
)

pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")

npm.npm_translate_lock(
name = "npm",
pnpm_lock = "//:pnpm-lock.yaml",
)
use_repo(npm, "npm")

use_repo(pnpm, "pnpm")

rules_ts_ext = use_extension(
"@aspect_rules_ts//ts:extensions.bzl",
"ext",
dev_dependency = True,
)
rules_ts_ext.deps(
ts_version_from = "//:package.json",
)
use_repo(rules_ts_ext, "npm_typescript")
Empty file removed examples/WORKSPACE.bazel
Empty file.
11 changes: 0 additions & 11 deletions examples/foo.proto

This file was deleted.

6 changes: 3 additions & 3 deletions examples/go/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@rules_go//go:def.bzl", "go_test")

go_test(
name = "foo_proto_test",
srcs = ["foo_proto_test.go"],
deps = ["//:foo_go_proto"],
name = "greeter_proto_test",
srcs = ["greeter_proto_test.go"],
deps = ["//proto:greeter_go_proto"],
)
16 changes: 0 additions & 16 deletions examples/go/foo_proto_test.go

This file was deleted.

16 changes: 16 additions & 0 deletions examples/go/greeter_proto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package proto_test

import (
"testing"

"example.com/greeter_proto"
)

func TestFoo(t *testing.T) {
msg := &greeter_proto.HelloReply{
Message: "hello world",
}
if msg.Message != "hello world" {
t.Fail()
}
}
15 changes: 15 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"dependencies": {
"@bufbuild/protobuf": "^2.2.4",
"@connectrpc/connect-node": "2.0.2",
"@connectrpc/connect-fastify": "~2.0.2",
"fastify": "~5.2.1"
},
"devDependencies": {
"@bufbuild/protoc-gen-es": "~2.2.4",
"@connectrpc/connect": "~2.0.2",
"typescript": "5.7.2",
"@types/node": "~22.13.10"
},
"type": "module"
}
Loading