Skip to content

Commit 75b5a7e

Browse files
feat: Implement support for new swift_proto_library rule (#834)
This PR implements support in the gazelle plugin for the new swift proto library implementation created in rules_swift: bazelbuild/rules_swift#1140 There is more context on why the rewrite was necessary and what opportunities the new implementation affords us on that PR. The new implementation allows us to address the two most significant issues with the current proto support: - The old implementation used the SwiftProtobuf / GRPC libraries from rules_swift, making them mutually exclusive with SPM-provided versions in the same binary. - As a workaround to the previous issue, we could generate the targets but could not resolve the GRPC runtime dependencies because they came from rules_swift, so you would see warnings about unknown GRPC, SwiftProtobuf and SwiftNIO targets. The new implementation fixes these issues by allowing us to declare our own swift_proto_compiler target which uses the SwiftProtobuf and GRPC targets provided through Swift Package Manager / rules_swift_package_manager. E.g. ``` swift_proto_compiler( name = "swift_proto", plugin = "@swiftpkg_swift_protobuf//:Sources_protoc-gen-swift", plugin_name = "swift", plugin_options = BASE_PLUGIN_OPTIONS, protoc = "@com_google_protobuf//:protoc", suffixes = [".pb.swift"], visibility = ["//visibility:public"], deps = [ "@swiftpkg_swift_protobuf//:Sources_SwiftProtobuf", ], ) ``` These can then be passed into the swift_proto_library targets like so: ``` swift_proto_library( name = "echo_service_messages_swift_proto", compilers = ["//compilers:swift_proto"], module_name = "EchoServiceMessages", protos = [":echo_service_messages_proto"], visibility = ["//visibility:public"], ) ``` This is how the go_proto_library rule works as well. I updated the gazelle plugin to support generating the new rules, with a directive that allows you to pass these custom compiler targets. I also updated the GRPC example to leverage all of these capabilities. Now the gazelle plugin can properly resolve swift imports of the proto libraries by their module name, E.g. "import EchoServiceMessages", and it can also resolve dependencies between proto libraries mapped to swift proto libraries, so if the code-generated swift imports the same target, it'll receive the same dependency. NOTE: Due to a bug in rules_swift_package_manager, the GRPC dependencies generated by this repository from the Package.swift are failing to compile on Ubuntu 22.04 in CI. #1012 Until this issue is fixed, we need to continue using the GRPC dependencies from rules_swift, but I have split out a follow-up PR to use the SPM version which I can land after the bug is fixed. #1019 --------- Co-authored-by: Logan Shire <3255391+AttilaTheFun@users.noreply.github.com> Co-authored-by: Chuck Grindel <chuck.grindel@gmail.com>
1 parent 4080822 commit 75b5a7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1097
-277
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# To update these lines, execute
22
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
3-
build --deleted_packages=bzlmod/workspace,bzlmod/workspace/Sources/MyExecutable,bzlmod/workspace/Sources/MyLibrary,bzlmod/workspace/Sources/System,bzlmod/workspace/Tests/MyLibraryTests,examples/firebase_example,examples/firebase_example/abtesting,examples/firebase_example/abtesting/SharedApp,examples/firebase_example/analytics/AnalyticsExample,examples/firebase_example/appdistribution,examples/firebase_example/appdistribution/AppDistributionExample,examples/firebase_example/appdistribution/AppDistributionTests,examples/firebase_example/crashlytics,examples/grpc_example,examples/grpc_example/aaa_test,examples/grpc_example/protos/echoservice,examples/grpc_example/protos/echoservice/messages,examples/grpc_example/sources,examples/grpc_example/sources/client,examples/grpc_example/sources/server,examples/grpc_example/sources/test,examples/http_archive_ext_deps,examples/http_archive_ext_deps/Sources/MyDequeModule,examples/http_archive_ext_deps/Sources/PrintStuff,examples/http_archive_ext_deps/Tests/MyDequeModuleTests,examples/http_archive_ext_deps/third_party,examples/interesting_deps,examples/ios_sim,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/ios_sim/third-party/swift-cmark,examples/lottie_ios_example,examples/lottie_ios_example/LottieExample,examples/lottie_ios_example/LottieExampleUITest,examples/messagekit_example,examples/messagekit_example/Sources,examples/messagekit_example/UITests,examples/nimble_example,examples/nimble_example/Sources/NimbleExample,examples/objc_code,examples/phone_number_kit,examples/phone_number_kit/Tests/PhoneNumberKitTests,examples/pkg_manifest_minimal,examples/pkg_manifest_minimal/Sources/MyExecutable,examples/pkg_manifest_minimal/Sources/MyLibrary,examples/pkg_manifest_minimal/Tests/MyLibraryTests,examples/pkg_manifest_minimal/third_party,examples/resources_example,examples/resources_example/Sources/MyApp,examples/resources_example/Tests/MyAppTests,examples/resources_example/Tests/MyAppUITests,examples/resources_example/third_party,examples/shake_ios_example,examples/shake_ios_example/ShakeIOSExample,examples/shake_ios_example/ShakeIOSExampleUITests,examples/snapkit_example,examples/soto_example,examples/soto_example/Tests/SotoTests,examples/stripe_example,examples/stripe_example/PaymentSheet/PaymentSheetExample,examples/stripe_example/PaymentSheet/PaymentSheetUITest,examples/tca_example,examples/tca_example/Sources,examples/tca_example/Tests,examples/vapor_example,examples/vapor_example/Sources/App,examples/vapor_example/Sources/Run,examples/vapor_example/Tests/AppTests,examples/vapor_example/swift,examples/xcmetrics_example
4-
query --deleted_packages=bzlmod/workspace,bzlmod/workspace/Sources/MyExecutable,bzlmod/workspace/Sources/MyLibrary,bzlmod/workspace/Sources/System,bzlmod/workspace/Tests/MyLibraryTests,examples/firebase_example,examples/firebase_example/abtesting,examples/firebase_example/abtesting/SharedApp,examples/firebase_example/analytics/AnalyticsExample,examples/firebase_example/appdistribution,examples/firebase_example/appdistribution/AppDistributionExample,examples/firebase_example/appdistribution/AppDistributionTests,examples/firebase_example/crashlytics,examples/grpc_example,examples/grpc_example/aaa_test,examples/grpc_example/protos/echoservice,examples/grpc_example/protos/echoservice/messages,examples/grpc_example/sources,examples/grpc_example/sources/client,examples/grpc_example/sources/server,examples/grpc_example/sources/test,examples/http_archive_ext_deps,examples/http_archive_ext_deps/Sources/MyDequeModule,examples/http_archive_ext_deps/Sources/PrintStuff,examples/http_archive_ext_deps/Tests/MyDequeModuleTests,examples/http_archive_ext_deps/third_party,examples/interesting_deps,examples/ios_sim,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/ios_sim/third-party/swift-cmark,examples/lottie_ios_example,examples/lottie_ios_example/LottieExample,examples/lottie_ios_example/LottieExampleUITest,examples/messagekit_example,examples/messagekit_example/Sources,examples/messagekit_example/UITests,examples/nimble_example,examples/nimble_example/Sources/NimbleExample,examples/objc_code,examples/phone_number_kit,examples/phone_number_kit/Tests/PhoneNumberKitTests,examples/pkg_manifest_minimal,examples/pkg_manifest_minimal/Sources/MyExecutable,examples/pkg_manifest_minimal/Sources/MyLibrary,examples/pkg_manifest_minimal/Tests/MyLibraryTests,examples/pkg_manifest_minimal/third_party,examples/resources_example,examples/resources_example/Sources/MyApp,examples/resources_example/Tests/MyAppTests,examples/resources_example/Tests/MyAppUITests,examples/resources_example/third_party,examples/shake_ios_example,examples/shake_ios_example/ShakeIOSExample,examples/shake_ios_example/ShakeIOSExampleUITests,examples/snapkit_example,examples/soto_example,examples/soto_example/Tests/SotoTests,examples/stripe_example,examples/stripe_example/PaymentSheet/PaymentSheetExample,examples/stripe_example/PaymentSheet/PaymentSheetUITest,examples/tca_example,examples/tca_example/Sources,examples/tca_example/Tests,examples/vapor_example,examples/vapor_example/Sources/App,examples/vapor_example/Sources/Run,examples/vapor_example/Tests/AppTests,examples/vapor_example/swift,examples/xcmetrics_example
3+
build --deleted_packages=bzlmod/workspace,bzlmod/workspace/Sources/MyExecutable,bzlmod/workspace/Sources/MyLibrary,bzlmod/workspace/Sources/System,bzlmod/workspace/Tests/MyLibraryTests,examples/firebase_example,examples/firebase_example/abtesting,examples/firebase_example/abtesting/SharedApp,examples/firebase_example/analytics/AnalyticsExample,examples/firebase_example/appdistribution,examples/firebase_example/appdistribution/AppDistributionExample,examples/firebase_example/appdistribution/AppDistributionTests,examples/firebase_example/crashlytics,examples/grpc_example,examples/grpc_example/aaa_test,examples/grpc_example/protos,examples/grpc_example/protos/echo_service,examples/grpc_example/protos/echo_service/requests,examples/grpc_example/protos/echo_service/responses,examples/grpc_example/sources,examples/grpc_example/sources/client,examples/grpc_example/sources/server,examples/grpc_example/sources/test,examples/grpc_package_example,examples/grpc_package_example/aaa_test,examples/grpc_package_example/protos,examples/grpc_package_example/protos/echo_service,examples/grpc_package_example/sources,examples/grpc_package_example/sources/client,examples/grpc_package_example/sources/server,examples/grpc_package_example/sources/test,examples/http_archive_ext_deps,examples/http_archive_ext_deps/Sources/MyDequeModule,examples/http_archive_ext_deps/Sources/PrintStuff,examples/http_archive_ext_deps/Tests/MyDequeModuleTests,examples/http_archive_ext_deps/third_party,examples/interesting_deps,examples/ios_sim,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/ios_sim/third-party/swift-cmark,examples/lottie_ios_example,examples/lottie_ios_example/LottieExample,examples/lottie_ios_example/LottieExampleUITest,examples/messagekit_example,examples/messagekit_example/Sources,examples/messagekit_example/UITests,examples/nimble_example,examples/nimble_example/Sources/NimbleExample,examples/objc_code,examples/phone_number_kit,examples/phone_number_kit/Tests/PhoneNumberKitTests,examples/pkg_manifest_minimal,examples/pkg_manifest_minimal/Sources/MyExecutable,examples/pkg_manifest_minimal/Sources/MyLibrary,examples/pkg_manifest_minimal/Tests/MyLibraryTests,examples/pkg_manifest_minimal/third_party,examples/resources_example,examples/resources_example/Sources/MyApp,examples/resources_example/Tests/MyAppTests,examples/resources_example/Tests/MyAppUITests,examples/resources_example/third_party,examples/shake_ios_example,examples/shake_ios_example/ShakeIOSExample,examples/shake_ios_example/ShakeIOSExampleUITests,examples/snapkit_example,examples/soto_example,examples/soto_example/Tests/SotoTests,examples/stripe_example,examples/stripe_example/PaymentSheet/PaymentSheetExample,examples/stripe_example/PaymentSheet/PaymentSheetUITest,examples/tca_example,examples/tca_example/Sources,examples/tca_example/Tests,examples/vapor_example,examples/vapor_example/Sources/App,examples/vapor_example/Sources/Run,examples/vapor_example/Tests/AppTests,examples/vapor_example/swift,examples/xcmetrics_example
4+
query --deleted_packages=bzlmod/workspace,bzlmod/workspace/Sources/MyExecutable,bzlmod/workspace/Sources/MyLibrary,bzlmod/workspace/Sources/System,bzlmod/workspace/Tests/MyLibraryTests,examples/firebase_example,examples/firebase_example/abtesting,examples/firebase_example/abtesting/SharedApp,examples/firebase_example/analytics/AnalyticsExample,examples/firebase_example/appdistribution,examples/firebase_example/appdistribution/AppDistributionExample,examples/firebase_example/appdistribution/AppDistributionTests,examples/firebase_example/crashlytics,examples/grpc_example,examples/grpc_example/aaa_test,examples/grpc_example/protos,examples/grpc_example/protos/echo_service,examples/grpc_example/protos/echo_service/requests,examples/grpc_example/protos/echo_service/responses,examples/grpc_example/sources,examples/grpc_example/sources/client,examples/grpc_example/sources/server,examples/grpc_example/sources/test,examples/grpc_package_example,examples/grpc_package_example/aaa_test,examples/grpc_package_example/protos,examples/grpc_package_example/protos/echo_service,examples/grpc_package_example/sources,examples/grpc_package_example/sources/client,examples/grpc_package_example/sources/server,examples/grpc_package_example/sources/test,examples/http_archive_ext_deps,examples/http_archive_ext_deps/Sources/MyDequeModule,examples/http_archive_ext_deps/Sources/PrintStuff,examples/http_archive_ext_deps/Tests/MyDequeModuleTests,examples/http_archive_ext_deps/third_party,examples/interesting_deps,examples/ios_sim,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/ios_sim/third-party/swift-cmark,examples/lottie_ios_example,examples/lottie_ios_example/LottieExample,examples/lottie_ios_example/LottieExampleUITest,examples/messagekit_example,examples/messagekit_example/Sources,examples/messagekit_example/UITests,examples/nimble_example,examples/nimble_example/Sources/NimbleExample,examples/objc_code,examples/phone_number_kit,examples/phone_number_kit/Tests/PhoneNumberKitTests,examples/pkg_manifest_minimal,examples/pkg_manifest_minimal/Sources/MyExecutable,examples/pkg_manifest_minimal/Sources/MyLibrary,examples/pkg_manifest_minimal/Tests/MyLibraryTests,examples/pkg_manifest_minimal/third_party,examples/resources_example,examples/resources_example/Sources/MyApp,examples/resources_example/Tests/MyAppTests,examples/resources_example/Tests/MyAppUITests,examples/resources_example/third_party,examples/shake_ios_example,examples/shake_ios_example/ShakeIOSExample,examples/shake_ios_example/ShakeIOSExampleUITests,examples/snapkit_example,examples/soto_example,examples/soto_example/Tests/SotoTests,examples/stripe_example,examples/stripe_example/PaymentSheet/PaymentSheetExample,examples/stripe_example/PaymentSheet/PaymentSheetUITest,examples/tca_example,examples/tca_example/Sources,examples/tca_example/Tests,examples/vapor_example,examples/vapor_example/Sources/App,examples/vapor_example/Sources/Run,examples/vapor_example/Tests/AppTests,examples/vapor_example/swift,examples/xcmetrics_example
55

66
# Import Shared settings
77
import %workspace%/shared.bazelrc

examples/grpc_example/BUILD.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ tidy(
1313
# MARK: - Gazelle
1414

1515
# Ignore the Swift build folder
16-
# gazelle:exclude .build
16+
# gazelle:proto file
17+
# gazelle:proto_strip_import_prefix /protos
18+
# gazelle:proto_import_prefix example
1719
# gazelle:swift_module_naming_convention pascal_case
1820
# gazelle:swift_generate_proto_libraries true
19-
# gazelle:swift_generate_grpc_libraries_with_flavors client,client_stubs,server
21+
# gazelle:swift_generate_grpc_libraries_with_flavors swift_client_proto,swift_server_proto
22+
# gazelle:exclude .build
2023

2124
gazelle_binary(
2225
name = "gazelle_bin",

examples/grpc_example/MODULE.bazel

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Example module for grpc + proto target generation.
3+
"""
4+
15
bazel_dep(
26
name = "rules_swift_package_manager",
37
version = "0.0.0",
@@ -25,12 +29,6 @@ use_repo(
2529
"build_bazel_rules_swift_index_import",
2630
"build_bazel_rules_swift_local_config",
2731
"com_github_apple_swift_log",
28-
"com_github_apple_swift_nio",
29-
"com_github_apple_swift_nio_extras",
30-
"com_github_apple_swift_nio_http2",
31-
"com_github_apple_swift_nio_transport_services",
32-
"com_github_apple_swift_protobuf",
33-
"com_github_grpc_grpc_swift",
3432
)
3533

3634
bazel_dep(

examples/grpc_example/protos/BUILD.bazel

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
load("@build_bazel_rules_swift//proto:proto.bzl", "swift_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
proto_library(
5+
name = "echo_service_proto",
6+
srcs = ["echo_service.proto"],
7+
import_prefix = "example",
8+
strip_import_prefix = "/protos",
9+
visibility = ["//visibility:public"],
10+
deps = [
11+
"//protos/echo_service/requests:echo_request_proto",
12+
"//protos/echo_service/responses:echo_response_proto",
13+
],
14+
)
15+
16+
swift_proto_library(
17+
name = "echo_service_swift_client_proto",
18+
compilers = [
19+
"@build_bazel_rules_swift//proto/compilers:swift_proto",
20+
"@build_bazel_rules_swift//proto/compilers:swift_client_proto",
21+
],
22+
module_name = "EchoServiceClient",
23+
protos = [":echo_service_proto"],
24+
visibility = ["//visibility:public"],
25+
deps = [
26+
"//protos/echo_service/requests:echo_request_swift_proto",
27+
"//protos/echo_service/responses:echo_response_swift_proto",
28+
],
29+
)
30+
31+
swift_proto_library(
32+
name = "echo_service_swift_server_proto",
33+
compilers = [
34+
"@build_bazel_rules_swift//proto/compilers:swift_proto",
35+
"@build_bazel_rules_swift//proto/compilers:swift_server_proto",
36+
],
37+
module_name = "EchoServiceServer",
38+
protos = [":echo_service_proto"],
39+
visibility = ["//visibility:public"],
40+
deps = [
41+
"//protos/echo_service/requests:echo_request_swift_proto",
42+
"//protos/echo_service/responses:echo_response_swift_proto",
43+
],
44+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
syntax = "proto3";
2+
3+
package echo_service;
4+
5+
import "example/echo_service/requests/echo_request.proto";
6+
import "example/echo_service/responses/echo_response.proto";
7+
8+
service Echo {
9+
rpc Echo(EchoRequest) returns (EchoResponse);
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("@build_bazel_rules_swift//proto:proto.bzl", "swift_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
proto_library(
5+
name = "echo_request_proto",
6+
srcs = ["echo_request.proto"],
7+
import_prefix = "example",
8+
strip_import_prefix = "/protos",
9+
visibility = ["//visibility:public"],
10+
deps = ["@com_google_protobuf//:any_proto"],
11+
)
12+
13+
swift_proto_library(
14+
name = "echo_request_swift_proto",
15+
compilers = ["@build_bazel_rules_swift//proto/compilers:swift_proto"],
16+
module_name = "EchoRequest",
17+
protos = [":echo_request_proto"],
18+
visibility = ["//visibility:public"],
19+
)
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
syntax = "proto3";
22

3-
package messages;
3+
package echo_service;
44

55
import "google/protobuf/any.proto";
66

77
message EchoRequest {
88
string contents = 1;
99
google.protobuf.Any extra = 2;
1010
}
11-
12-
message EchoResponse {
13-
string contents = 1;
14-
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("@build_bazel_rules_swift//proto:proto.bzl", "swift_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
proto_library(
5+
name = "echo_response_proto",
6+
srcs = ["echo_response.proto"],
7+
import_prefix = "example",
8+
strip_import_prefix = "/protos",
9+
visibility = ["//visibility:public"],
10+
)
11+
12+
swift_proto_library(
13+
name = "echo_response_swift_proto",
14+
compilers = ["@build_bazel_rules_swift//proto/compilers:swift_proto"],
15+
module_name = "EchoResponse",
16+
protos = [":echo_response_proto"],
17+
visibility = ["//visibility:public"],
18+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package echo_service;
4+
5+
message EchoResponse {
6+
string contents = 1;
7+
}

0 commit comments

Comments
 (0)