-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: Chuck Grindel <[email protected]>
- Loading branch information
1 parent
4080822
commit 75b5a7e
Showing
55 changed files
with
1,097 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
load("@build_bazel_rules_swift//proto:proto.bzl", "swift_proto_library") | ||
load("@rules_proto//proto:defs.bzl", "proto_library") | ||
|
||
proto_library( | ||
name = "echo_service_proto", | ||
srcs = ["echo_service.proto"], | ||
import_prefix = "example", | ||
strip_import_prefix = "/protos", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//protos/echo_service/requests:echo_request_proto", | ||
"//protos/echo_service/responses:echo_response_proto", | ||
], | ||
) | ||
|
||
swift_proto_library( | ||
name = "echo_service_swift_client_proto", | ||
compilers = [ | ||
"@build_bazel_rules_swift//proto/compilers:swift_proto", | ||
"@build_bazel_rules_swift//proto/compilers:swift_client_proto", | ||
], | ||
module_name = "EchoServiceClient", | ||
protos = [":echo_service_proto"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//protos/echo_service/requests:echo_request_swift_proto", | ||
"//protos/echo_service/responses:echo_response_swift_proto", | ||
], | ||
) | ||
|
||
swift_proto_library( | ||
name = "echo_service_swift_server_proto", | ||
compilers = [ | ||
"@build_bazel_rules_swift//proto/compilers:swift_proto", | ||
"@build_bazel_rules_swift//proto/compilers:swift_server_proto", | ||
], | ||
module_name = "EchoServiceServer", | ||
protos = [":echo_service_proto"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//protos/echo_service/requests:echo_request_swift_proto", | ||
"//protos/echo_service/responses:echo_response_swift_proto", | ||
], | ||
) |
10 changes: 10 additions & 0 deletions
10
examples/grpc_example/protos/echo_service/echo_service.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
syntax = "proto3"; | ||
|
||
package echo_service; | ||
|
||
import "example/echo_service/requests/echo_request.proto"; | ||
import "example/echo_service/responses/echo_response.proto"; | ||
|
||
service Echo { | ||
rpc Echo(EchoRequest) returns (EchoResponse); | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/grpc_example/protos/echo_service/requests/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
load("@build_bazel_rules_swift//proto:proto.bzl", "swift_proto_library") | ||
load("@rules_proto//proto:defs.bzl", "proto_library") | ||
|
||
proto_library( | ||
name = "echo_request_proto", | ||
srcs = ["echo_request.proto"], | ||
import_prefix = "example", | ||
strip_import_prefix = "/protos", | ||
visibility = ["//visibility:public"], | ||
deps = ["@com_google_protobuf//:any_proto"], | ||
) | ||
|
||
swift_proto_library( | ||
name = "echo_request_swift_proto", | ||
compilers = ["@build_bazel_rules_swift//proto/compilers:swift_proto"], | ||
module_name = "EchoRequest", | ||
protos = [":echo_request_proto"], | ||
visibility = ["//visibility:public"], | ||
) |
6 changes: 1 addition & 5 deletions
6
.../echoservice/messages/echo_messages.proto → .../echo_service/requests/echo_request.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
syntax = "proto3"; | ||
|
||
package messages; | ||
package echo_service; | ||
|
||
import "google/protobuf/any.proto"; | ||
|
||
message EchoRequest { | ||
string contents = 1; | ||
google.protobuf.Any extra = 2; | ||
} | ||
|
||
message EchoResponse { | ||
string contents = 1; | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/grpc_example/protos/echo_service/responses/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@build_bazel_rules_swift//proto:proto.bzl", "swift_proto_library") | ||
load("@rules_proto//proto:defs.bzl", "proto_library") | ||
|
||
proto_library( | ||
name = "echo_response_proto", | ||
srcs = ["echo_response.proto"], | ||
import_prefix = "example", | ||
strip_import_prefix = "/protos", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
swift_proto_library( | ||
name = "echo_response_swift_proto", | ||
compilers = ["@build_bazel_rules_swift//proto/compilers:swift_proto"], | ||
module_name = "EchoResponse", | ||
protos = [":echo_response_proto"], | ||
visibility = ["//visibility:public"], | ||
) |
7 changes: 7 additions & 0 deletions
7
examples/grpc_example/protos/echo_service/responses/echo_response.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
syntax = "proto3"; | ||
|
||
package echo_service; | ||
|
||
message EchoResponse { | ||
string contents = 1; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
examples/grpc_example/protos/echoservice/messages/BUILD.bazel
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.