Skip to content
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

Use GRPC from Swift Package Manager #1019

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AttilaTheFun
Copy link
Contributor

@AttilaTheFun AttilaTheFun commented Apr 16, 2024

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

Because of these changes, now we can depend on swift GRPC and Protobuf normally through Package.swift and these targets resolve normally too.

let package = Package(
    name: "grpc_example",
    dependencies: [
        .package(url: "https://github.com/grpc/grpc-swift.git", from: "1.15.0"),
        .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.25.2"),
    ]
)

cgrindel added a commit that referenced this pull request Apr 18, 2024
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant