Skip to content

Commit 150c8cf

Browse files
qnighyTiago Cardoso
authored andcommitted
Add support for RBS, Ruby type signature
1 parent c338023 commit 150c8cf

16 files changed

+2066
-46
lines changed

.github/workflows/test_ruby.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,23 @@ jobs:
214214
bazel-bin/protoc --proto_path=src --proto_path=ruby/tests --proto_path=ruby --ruby_out=ruby tests/basic_test.proto;
215215
${{ matrix.ffi == 'FFI' && 'PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }} ruby ruby/tests/basic.rb;
216216
${{ matrix.ffi == 'FFI' && 'PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }} ruby ruby/tests/implementation.rb
217+
218+
typecheck:
219+
name: Typecheck
220+
runs-on: ubuntu-latest
221+
steps:
222+
- name: Checkout pending changes
223+
uses: protocolbuffers/protobuf-ci/checkout@v2
224+
with:
225+
ref: ${{ inputs.safe-checkout }}
226+
- name: Run type checker
227+
uses: protocolbuffers/protobuf-ci/bazel-docker@v2
228+
with:
229+
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:ruby-3.3.0-6.3.0-904cad5249547845454998ca3837a34c71fabf96
230+
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
231+
bazel-cache: ruby_typecheck
232+
bash:
233+
bazel build //:protoc
234+
cd ruby
235+
cp typecheck.gemfile.lock Gemfile.lock
236+
bundle exec rake steep

protobuf.bzl

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def _PyOuts(srcs, use_grpc_plugin = False):
6565
def _RubyOuts(srcs):
6666
return [s[:-len(".proto")] + "_pb.rb" for s in srcs]
6767

68+
def _RBSOuts(srcs):
69+
return [s[:-len(".proto")] + "_pb.rbs" for s in srcs]
70+
6871
def _CsharpOuts(srcs):
6972
return [
7073
"".join([token.capitalize() for token in src[:-len(".proto")].split("_")]) + ".cs"
@@ -158,6 +161,8 @@ def _proto_gen_impl(ctx):
158161
outs.extend(_PyOuts([src.basename], use_grpc_plugin = use_grpc_plugin))
159162
elif lang == "ruby":
160163
outs.extend(_RubyOuts([src.basename]))
164+
elif lang == "rbs":
165+
outs.extend(_RBSOuts([src.basename]))
161166

162167
# Otherwise, rely on user-supplied outs.
163168
args.append(("--%s_out=" + path_tpl) % (lang, gen_dir))
@@ -526,8 +531,6 @@ def internal_ruby_proto_library(
526531
527532
"""
528533

529-
# Note: we need to run the protoc build twice to get separate targets for
530-
# the generated header and the source files.
531534
_proto_gen(
532535
name = name + "_genproto",
533536
srcs = srcs,
@@ -552,6 +555,58 @@ def internal_ruby_proto_library(
552555
**kwargs
553556
)
554557

558+
def internal_rbs_proto_library(
559+
name,
560+
ruby_library,
561+
srcs = [],
562+
deps = [],
563+
includes = ["."],
564+
protoc = "@com_google_protobuf//:protoc",
565+
testonly = None,
566+
visibility = ["//visibility:public"],
567+
**kwargs):
568+
"""Bazel rule to create an RBS type definitions for the Ruby protobuf library
569+
from proto source files
570+
571+
NOTE: the rule is only an internal workaround to generate protos. The
572+
interface may change and the rule may be removed when bazel has introduced
573+
the native rule.
574+
575+
Args:
576+
name: the name of the ruby_proto_library.
577+
srcs: the .proto files to compile.
578+
deps: a list of dependency labels; must be a internal_rbs_proto_library.
579+
includes: a string indicating the include path of the .proto files.
580+
protoc: the label of the protocol compiler to generate the sources.
581+
testonly: common rule attribute (see:
582+
https://bazel.build/reference/be/common-definitions#common-attributes)
583+
visibility: the visibility of the generated files.
584+
**kwargs: other keyword arguments that are passed to ruby_library.
585+
586+
"""
587+
588+
_proto_gen(
589+
name = name + "_genproto_rbs",
590+
srcs = srcs,
591+
deps = [s + "_genproto_rbs" for s in deps],
592+
langs = ["rbs"],
593+
includes = includes,
594+
protoc = protoc,
595+
testonly = testonly,
596+
visibility = visibility,
597+
tags = ["manual"],
598+
)
599+
600+
ruby_library(
601+
name = name,
602+
srcs = [name + "_genproto_rbs"],
603+
deps = [],
604+
testonly = testonly,
605+
visibility = visibility,
606+
includes = includes,
607+
**kwargs
608+
)
609+
555610
# When canonical labels are in use, use additional "@" prefix
556611
_canonical_label_prefix = "@" if str(Label("//:protoc")).startswith("@@") else ""
557612

ruby/lib/google/protobuf/ffi/message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def self.decode_json(data, options = {})
254254
decoding_options = 0
255255
unless options.is_a? Hash
256256
if options.respond_to? :to_h
257-
options options.to_h
257+
options = options.to_h
258258
else
259259
#TODO can this error message be improve to include what was received?
260260
raise ArgumentError.new "Expected hash arguments"

src/file_lists.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ set(libprotoc_srcs
448448
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/helpers.cc
449449
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/pyi_generator.cc
450450
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/retention.cc
451+
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/rbs_generator.cc
451452
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/ruby_generator.cc
452453
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/rust/accessors/accessor_case.cc
453454
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/rust/accessors/accessors.cc
@@ -596,6 +597,7 @@ set(libprotoc_hdrs
596597
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/helpers.h
597598
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/pyi_generator.h
598599
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/retention.h
600+
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/rbs_generator.h
599601
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/ruby_generator.h
600602
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/rust/accessors/accessor_case.h
601603
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/rust/accessors/accessors.h
@@ -1538,6 +1540,7 @@ set(compiler_test_files
15381540
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/php/generator_unittest.cc
15391541
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/plugin_unittest.cc
15401542
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/retention_unittest.cc
1543+
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/rbs_generator_unittest.cc
15411544
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc
15421545
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/versions_test.cc
15431546
)

src/google/protobuf/compiler/main.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ int ProtobufMain(int argc, char* argv[]) {
9494
cli.RegisterGenerator("--ruby_out", "--ruby_opt", &rb_generator,
9595
"Generate Ruby source file.");
9696

97-
// Ruby
98-
ruby::RbsGenerator rbs_generator;
99-
cli.RegisterGenerator("--rbs_out", &rb_generator,
100-
"Generate Ruby rbs stub.");
97+
ruby::RBSGenerator rbs_generator;
98+
cli.RegisterGenerator("--rbs_out", "--rbs_opt", &rbs_generator,
99+
"Generate RBS type definition.");
101100

102101
// CSharp
103102
csharp::Generator csharp_generator;

src/google/protobuf/compiler/ruby/BUILD.bazel

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ load("//build_defs:cpp_opts.bzl", "COPTS")
88

99
cc_library(
1010
name = "ruby",
11-
srcs = ["ruby_generator.cc"],
12-
hdrs = ["ruby_generator.h"],
11+
srcs = [
12+
"ruby_generator.cc",
13+
"rbs_generator.cc",
14+
],
15+
hdrs = [
16+
"ruby_generator.h",
17+
"rbs_generator.h",
18+
],
1319
copts = COPTS,
1420
strip_include_prefix = "/src",
1521
visibility = [
@@ -31,19 +37,27 @@ cc_library(
3137

3238
cc_test(
3339
name = "generator_unittest",
34-
srcs = ["ruby_generator_unittest.cc"],
40+
srcs = [
41+
"ruby_generator_unittest.cc",
42+
"rbs_generator_unittest.cc",
43+
],
3544
data = [
3645
"ruby_generated_code.proto",
3746
"ruby_generated_code_pb.rb",
47+
"ruby_generated_code_pb.rbs",
3848
"ruby_generated_code_proto2.proto",
3949
"ruby_generated_code_proto2_import.proto",
4050
"ruby_generated_code_proto2_pb.rb",
51+
"ruby_generated_code_proto2_pb.rbs",
4152
"ruby_generated_pkg_explicit.proto",
4253
"ruby_generated_pkg_explicit_legacy.proto",
4354
"ruby_generated_pkg_explicit_legacy_pb.rb",
55+
"ruby_generated_pkg_explicit_legacy_pb.rbs",
4456
"ruby_generated_pkg_explicit_pb.rb",
57+
"ruby_generated_pkg_explicit_pb.rbs",
4558
"ruby_generated_pkg_implicit.proto",
4659
"ruby_generated_pkg_implicit_pb.rb",
60+
"ruby_generated_pkg_implicit_pb.rbs",
4761
"//src/google/protobuf:testdata",
4862
],
4963
deps = [

0 commit comments

Comments
 (0)