grpc-kotlin icon indicating copy to clipboard operation
grpc-kotlin copied to clipboard

The source for this tutorial is too complicated

Open johnlabarge opened this issue 2 years ago • 6 comments

What I needed at least was something more minimal that showed off the project structure that makes sense for a server and client stub. This has lots of dependent modules and it's hard to get your head around how to use it in real life.

johnlabarge avatar May 11 '23 18:05 johnlabarge

Yeah I agree. For now, here is a more minimal example that might help: https://github.com/GoogleCloudPlatform/kotlin-samples/tree/main/run/grpc-hello-world-gradle

jamesward avatar May 11 '23 18:05 jamesward

@jamesward do you have the same example for bazel? support for bzlmod with MODULE.bazel is appreciated

dkthezero avatar Aug 20 '23 07:08 dkthezero

The examples in this repo have bazel builds. Let me know if that helps.

jamesward avatar Aug 21 '23 17:08 jamesward

@jamesward I have looked around all examples, but you use the rules but do not setup it up from scratch.

I practice with Bzlmod, a new way to maintain the Bazel in the next release 7.0, can you help me to resolve it:

  • Add the grpc-kotlin into project
## MODULE.bzl
# =========================================
# Custom rule for grpc-kotlin v1.3.0
# =========================================
bazel_dep(name = "grpc_kotlin", repo_name = "grpc_kotlin")

archive_override(
    module_name = "grpc_kotlin",
    urls = ["https://github.com/grpc/grpc-kotlin/archive/refs/tags/v1.3.0.tar.gz"],
)
  • Use it
## protos/BUILD
package(default_visibility = ["//visibility:public"])

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@grpc_kotlin//kt_jvm_grpc.bzl", "kt_jvm_grpc_library", "kt_jvm_proto_library")

proto_library(
    name = "embedded_proto",
    srcs = [
        "inits.proto",
        "specs.proto",
    ],
    deps = [],
)

kt_jvm_proto_library(
    name = "embedded_kt_proto",
    deps = [":embedded_proto"],
)

kt_jvm_grpc_library(
    name = "embedded_kt_grpc",
    srcs = [":embedded_proto"],
    deps = [":embedded_kt_proto"],
)

kt_jvm_grpc_library(
    name = "embedded_kt_grpc_lite",
    srcs = [":embedded_proto"],
    flavor = "lite",
    deps = [":embedded_kt_proto"],
)

Then I got error while build:

Loading: 
ERROR: Error computing the main repository mapping: MODULE.bazel expected but not found at /private/var/tmp/_bazel_dkthezero/aedcf60688cfee836e103ea62b320c98/external/grpc_kotlin~override/MODULE.bazel

dkthezero avatar Aug 21 '23 23:08 dkthezero

Another setup is:

## grpc_kotlin.bzl
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def grpc_kotlin():
    release = "v1.3.0"
    http_archive(
        name = "grpc_kotlin",
        urls = ["https://github.com/grpc/grpc-kotlin/archive/refs/tags/%s.tar.gz" % release],
    )

def _grpc_kotlin_repositories(ctx):
    grpc_kotlin()

grpc_kotlin_dependencies = module_extension(
    implementation = _grpc_kotlin_repositories,
)
## MODULE.bzl
grpc_kotlin_dependencies = use_extension("//:grpc_kotlin.bzl", "grpc_kotlin_dependencies")
use_repo(grpc_kotlin_dependencies, "grpc_kotlin")

Then when we build:

ERROR: error loading package 'embed/kotlin/vn/uhand/embed/proto': Every .bzl file must have a corresponding package, but '@_main~grpc_kotlin_dependencies~grpc_kotlin//:kt_jvm_grpc.bzl' does not have one. Please create a BUILD file in the same or any parent directory. Note that this BUILD file does not need to do anything except exist.
ERROR: Skipping '//embed/kotlin/vn/uhand/embed/proto/...:all': no targets found beneath 'embed/kotlin/vn/uhand/embed/proto'

dkthezero avatar Aug 21 '23 23:08 dkthezero

I attempted to get this to work. It is very close but there is a problem with the external dependency name being mangled. This is what I have, in case anyone is able to get something working based on it:

# grpc_kotlin.bzl
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")


def grpc_kotlin():
    grpc_kotlin_version = "1.4.1"
    http_archive(
        name = "com_github_grpc_grpc_kotlin",
        urls = ["https://github.com/grpc/grpc-kotlin/archive/refs/tags/v%s.tar.gz" % grpc_kotlin_version],
        strip_prefix = "grpc-kotlin-%s" % grpc_kotlin_version,
    )
    grpc_java_version = "1.60.1"
    http_archive(
        name = "io_grpc_grpc_java",
        urls = ["https://github.com/grpc/grpc-java/archive/refs/tags/v%s.tar.gz" % grpc_java_version],
        strip_prefix = "grpc-java-%s" % grpc_java_version,
    )
    protobuf_version = "25.1"
    http_archive(
        name = "com_google_protobuf",
        urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v25.1/protobuf-%s.tar.gz" % protobuf_version],
        strip_prefix = "protobuf-%s" % protobuf_version,
    )
    rules_ruby_version = "0.6.0"
    http_archive(
        name = "rules_ruby",
        strip_prefix = "rules_ruby-%s" % rules_ruby_version,
        urls = [
            "https://github.com/protocolbuffers/rules_ruby/archive/v%s.zip" % rules_ruby_version,
        ],
    )


def _grpc_kotlin_repositories(ctx):
    grpc_kotlin()

grpc_kotlin_dependencies = module_extension(
    implementation = _grpc_kotlin_repositories,
)
# MODULE.bazel

maven.install(
    artifacts = [
        "com.google.errorprone:error_prone_annotations:2.3.2",
        "com.squareup:kotlinpoet:1.15.3",
        "com.google.guava:guava:31.1-jre",
        "com.google.guava:failureaccess:1.0.2",
    ],
    generate_compat_repositories = True,
)

use_repo(
    maven,
    "maven",
    "com_google_guava_guava",
    "com_google_j2objc_j2objc_annotations",
    "com_google_code_findbugs_jsr305",
    "com_google_errorprone_error_prone_annotations",
    "com_google_guava_failureaccess",
)

Fails with:

ERROR: /private/var/tmp/_bazel_ekohlwey/80429706e7731d7db50de017c57ccb50/external/_main~grpc_kotlin_dependencies~com_github_grpc_grpc_kotlin/compiler/src/main/java/io/grpc/kotlin/generator/BUILD.bazel:9:15: no such target '@rules_jvm_external~5.3~maven~com_google_guava_guava//:com_google_guava_guava': target 'com_google_guava_guava' not declared in package '' defined by /private/var/tmp/_bazel_ekohlwey/80429706e7731d7db50de017c57ccb50/external/rules_jvm_external~5.3~maven~com_google_guava_guava/BUILD (Tip: use `query "@com_google_guava_guava//:*"` to see all the targets in that package) and referenced by '@_main~grpc_kotlin_dependencies~com_github_grpc_grpc_kotlin//compiler/src/main/java/io/grpc/kotlin/generator:generator'
ERROR: Analysis of target '//Service:Service' failed; build aborted: 

The output of the query command shows that the target name is mangled, matching the sub-repo naming conventions that bzlmod uses.

bazel query "@com_google_guava_guava//:*"
@com_google_guava_guava//:BUILD
@com_google_guava_guava//:rules_jvm_external~5.3~maven~com_google_guava_guava

I created https://github.com/bazelbuild/rules_jvm_external/issues/1016 in the hopes that someone can look into the name mangling issue.

ekohlwey avatar Jan 04 '24 00:01 ekohlwey