connectrpc / connect-go

The Go implementation of Connect: Protobuf RPC that works.
https://connectrpc.com
Apache License 2.0
2.94k stars 96 forks source link

bazel cannot find some connect types #656

Closed debkanchan closed 9 months ago

debkanchan commented 9 months ago

Describe the bug

When using connect-go with bazel, bazel throws the following error:

api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:21:19: undefined: connect.IsAtLeastVersion1_13_0
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:89:12: undefined: connect.WithSchema
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:95:12: undefined: connect.WithSchema
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:101:12: undefined: connect.WithSchema
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:107:12: undefined: connect.WithSchema
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:113:12: undefined: connect.WithSchema
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:119:12: undefined: connect.WithSchema
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:184:11: undefined: connect.WithSchema
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:190:11: undefined: connect.WithSchema
api/ride/driver/v1alpha1/v1alpha1connect/driver_service.connect.go:196:11: undefined: connect.WithSchema

But when the code is inspected manually it looks perfect

To Reproduce

Copy the files in buf module ride/driver

then generate files using buf generate

buf.gen.yaml

version: v1
plugins:
  - plugin: buf.build/protocolbuffers/go
    out: api
    opt:
      - paths=source_relative

  - plugin: buf.build/connectrpc/go
    out: api
    opt:
      - paths=source_relative

WORKSPACE

# Declares that this directory is the root of a Bazel workspace.
# See https://docs.bazel.build/versions/main/build-ref.html#workspace
workspace(
    # How this workspace would be referenced with absolute labels from another workspace
    name = "driver-service",
)

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

## GO
http_archive(
    name = "io_bazel_rules_go",
    sha256 = "d6ab6b57e48c09523e93050f13698f708428cfd5e619252e369d377af6597707",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.43.0/rules_go-v0.43.0.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.43.0/rules_go-v0.43.0.zip",
    ],
)

http_archive(
    name = "bazel_gazelle",
    sha256 = "b7387f72efb59f876e4daae42f1d3912d0d45563eac7cb23d1de0b094ab588cf",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.34.0/bazel-gazelle-v0.34.0.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.34.0/bazel-gazelle-v0.34.0.tar.gz",
    ],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

############################################################
# Define your own dependencies here using go_repository.
# Else, dependencies declared by rules_go/gazelle will be used.
# The first declaration of an external repository "wins".
############################################################

load("//:go_deps.bzl", "go_dependencies")

# gazelle:repository_macro go_deps.bzl%go_dependencies
go_dependencies()

go_rules_dependencies()

go_register_toolchains(version = "1.20.5")

gazelle_dependencies()

gazelle_dependencies(go_repository_default_config = "//:WORKSPACE")

# ## Buf.build

http_archive(
    name = "rules_buf",
    sha256 = "523a4e06f0746661e092d083757263a249fedca535bd6dd819a8c50de074731a",
    strip_prefix = "rules_buf-0.1.1",
    urls = [
        "https://github.com/bufbuild/rules_buf/archive/refs/tags/v0.1.1.zip",
    ],
)

load("@rules_buf//buf:repositories.bzl", "rules_buf_dependencies", "rules_buf_toolchains")

rules_buf_dependencies()

rules_buf_toolchains(version = "v1.28.1")

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
load("//:buf_deps.bzl", "buf_deps")

# gazelle:repository_macro buf_deps.bzl%buf_deps
buf_deps()

rules_proto_dependencies()

rules_proto_toolchains()

load("@rules_buf//gazelle/buf:repositories.bzl", "gazelle_buf_dependencies")

gazelle_buf_dependencies()

MODULE.bazel

bazel_dep(name = "rules_oci", version = "1.4.3")

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.pull(
    name = "distroless",
    digest = "sha256:6706c73aae2afaa8201d63cc3dda48753c09bcd6c300762251065c0f7e602b25",
    image = "gcr.io/distroless/static",
    platforms = [
        "linux/amd64",
        "linux/arm64/v8",
    ],
)

# For each oci.pull call, repeat the "name" here to expose them as dependencies.
use_repo(oci, "distroless")

BUILD

load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")

gazelle_binary(
    name = "gazelle-buf",
    languages = DEFAULT_LANGUAGES + [
        # Loads the Buf extension
        "@rules_buf//gazelle/buf:buf",
        # NOTE: This needs to be loaded after the proto language
    ],
)

# gazelle:prefix github.com/ride-app/driver-service
# gazelle:build_file_name BUILD.bazel
# gazelle:exclude infra
# gazelle:proto disable_global
gazelle(
    name = "gazelle",
    gazelle = ":gazelle-buf",
)

gazelle(
    name = "gazelle-update-repos-go",
    args = [
        "-from_file=go.mod",
        "-to_macro=go_deps.bzl%go_dependencies",
        "-prune",
    ],
    command = "update-repos",
    gazelle = ":gazelle-buf",
)

gazelle(
    name = "gazelle-update-repos-buf",
    args = [
        # This can also be `buf.yaml` and `buf.lock`.
        "--from_file=buf.work.yaml",
        # This is optional but recommended, if absent gazelle
        # will add the rules directly to WORKSPACE
        "-to_macro=buf_deps.bzl%buf_deps",
        # Deletes outdated repo rules
        "-prune",
    ],
    command = "update-repos",
    gazelle = ":gazelle-buf",
)

Finally run

bazel build //...

Environment (please complete the following information):

module github.com/ride-app/driver-service

go 1.20

require (
    buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.31.0-20231115204500-e097f827e652.2
    buf.build/gen/go/ride/wallet/connectrpc/go v1.13.0-20230924153639-d33e5416a9c2.1
    buf.build/gen/go/ride/wallet/protocolbuffers/go v1.31.0-20230924153639-d33e5416a9c2.2
    cloud.google.com/go/firestore v1.14.0
    connectrpc.com/connect v1.13.0
    firebase.google.com/go/v4 v4.13.0
    github.com/MicahParks/keyfunc/v2 v2.1.0
    github.com/bufbuild/protovalidate-go v0.4.3
    github.com/golang-jwt/jwt/v5 v5.2.0
    github.com/google/wire v0.5.0
    github.com/ilyakaznacheev/cleanenv v1.5.0
    github.com/mmcloughlin/geohash v0.10.0
    github.com/onsi/ginkgo/v2 v2.13.2
    github.com/onsi/gomega v1.30.0
    go.uber.org/mock v0.3.0
    go.uber.org/zap v1.26.0
    golang.org/x/net v0.19.0
    google.golang.org/genproto v0.0.0-20231212172506-995d672761c0
    google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0
    google.golang.org/grpc v1.60.0
    google.golang.org/protobuf v1.31.0
)

require (
    buf.build/gen/go/envoyproxy/protoc-gen-validate/protocolbuffers/go v1.31.0-20231130202533-71881f09a0c5.2 // indirect
    cloud.google.com/go v0.111.0 // indirect
    cloud.google.com/go/compute v1.23.3 // indirect
    cloud.google.com/go/compute/metadata v0.2.3 // indirect
    cloud.google.com/go/iam v1.1.5 // indirect
    cloud.google.com/go/longrunning v0.5.4 // indirect
    cloud.google.com/go/storage v1.36.0 // indirect
    github.com/BurntSushi/toml v1.3.2 // indirect
    github.com/MicahParks/keyfunc v1.9.0 // indirect
    github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
    github.com/felixge/httpsnoop v1.0.4 // indirect
    github.com/go-logr/logr v1.3.0 // indirect
    github.com/go-logr/stdr v1.2.2 // indirect
    github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
    github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
    github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
    github.com/golang/protobuf v1.5.3 // indirect
    github.com/google/cel-go v0.18.2 // indirect
    github.com/google/go-cmp v0.6.0 // indirect
    github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
    github.com/google/s2a-go v0.1.7 // indirect
    github.com/google/uuid v1.5.0 // indirect
    github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
    github.com/googleapis/gax-go/v2 v2.12.0 // indirect
    github.com/joho/godotenv v1.5.1 // indirect
    github.com/stoewer/go-strcase v1.3.0 // indirect
    go.opencensus.io v0.24.0 // indirect
    go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
    go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
    go.opentelemetry.io/otel v1.21.0 // indirect
    go.opentelemetry.io/otel/metric v1.21.0 // indirect
    go.opentelemetry.io/otel/trace v1.21.0 // indirect
    go.uber.org/multierr v1.11.0 // indirect
    golang.org/x/crypto v0.16.0 // indirect
    golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect
    golang.org/x/mod v0.14.0 // indirect
    golang.org/x/oauth2 v0.15.0 // indirect
    golang.org/x/sync v0.5.0 // indirect
    golang.org/x/sys v0.15.0 // indirect
    golang.org/x/text v0.14.0 // indirect
    golang.org/x/time v0.5.0 // indirect
    golang.org/x/tools v0.16.0 // indirect
    golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
    google.golang.org/api v0.154.0 // indirect
    google.golang.org/appengine v1.6.8 // indirect
    google.golang.org/appengine/v2 v2.0.5 // indirect
    google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
    olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)
akshayjshah commented 9 months ago

What's in go_deps.bzl? It looks like the version of the Connect runtime compiled into your application doesn't match go.mod.

debkanchan commented 9 months ago

You are right that was the problem. Thanks @akshayjshah