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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building with Bazel not compatible when using Gazelle and disable_global #1847

Closed
rytswd opened this issue Dec 1, 2020 · 9 comments
Closed

Comments

@rytswd
Copy link

rytswd commented Dec 1, 2020

馃悰 Bug Report

(Migrated over from bazelbuild/rules_go#2738)

Bazel build with "disable_global" flag to have code generated files used as is (rather than compiling .proto file with Bazel) causes the Bazel build to fail due to dependency mismatch between @com_github_grpc_ecosystem_grpc_gateway_v2//runtime:go_default_library and @org_golang_google_grpc//:grpc.

This is caused by the fact grpc-gateway uses Bazel for its own build. As in the referenced ticket in bazelbuild/rules_go, there seems to be not much I could do to handle this dependency mismatch with Bazel.

To Reproduce

Import github.com/grpc-ecosystem/grpc-gateway/v2/runtime and run Gazelle with --build_file_proto_mode=disable_global.

I have put together the repro in a repo

Essentially, this should be it

package main

import (
	_ "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
)

func main() {
	//
}

Then tried running with bazel run simple-bazel

Expected behavior

No error

Actual Behavior

Building with Bazel for grpc-gateway dependent package would see the following error.

link: package conflict error: github.com/golang/protobuf/ptypes/timestamp: multiple copies of package passed to linker:
        @com_github_golang_protobuf//ptypes/timestamp:timestamp
        @io_bazel_rules_go//proto/wkt:timestamp_go_proto
Set "importmap" to different paths or use 'bazel cquery' to ensure only one

Bazel used is v3.7.0, with some rules dependencies mentioned in the above reference. The below is the cquery results.

$ bazel cquery "somepath(//..., @io_bazel_rules_go//proto/wkt:timestamp_go_proto)"
//timesvc:go_default_library (63cc040)
@com_github_grpc_ecosystem_grpc_gateway_v2//runtime:go_default_library (63cc040)
@io_bazel_rules_go//proto/wkt:timestamp_go_proto (63cc040)

$ bazel cquery "somepath(//..., @com_github_golang_protobuf//ptypes/timestamp:timestamp)"
//timesvc:go_default_library (63cc040)
@org_golang_google_grpc//:go_default_library (63cc040)
@org_golang_google_grpc//:grpc (63cc040)
@org_golang_google_grpc//internal/binarylog:binarylog (63cc040)
@com_github_golang_protobuf//ptypes:ptypes (63cc040)
@com_github_golang_protobuf//ptypes/timestamp:timestamp (63cc040)

Your Environment

Bazel build, using v2.0.1

go_repository(
    name = "com_github_grpc_ecosystem_grpc_gateway_v2",
    build_file_proto_mode = "disable_global",
    importpath = "github.com/grpc-ecosystem/grpc-gateway/v2",
    sum = "h1:X2vfSnm1WC8HEo0MBHZg2TcuDUHJj6kd1TmEAQncnSA=",
    version = "v2.0.1",
)
@johanbrandhorst
Copy link
Collaborator

Sorry about that, my bazel knowledge is sparse, what would be required from us to fix this issue? I'd be happy to review a fix.

@rytswd
Copy link
Author

rytswd commented Dec 4, 2020

I may need further input from Bazel experts, but I believe the baseline is that grpc-gateway doesn't need Bazel / Gazelle machinery for protobuf compilation.

Although this repo already contains generated code (i.e. .pb.go files), Bazel setup here tries to exclude those generated files and let Bazel handle the proto compilation on its own. This can be seen with the usage of go_proto_library in /runtime/internal/examplepb and other places.

As mentioned in https://golang.org/doc/articles/go_command.html, having generated code committed is perfectly reasonable to allow go get, and I assume that's the reason why grpc-gateway also includes those files.

I have all my generated code committed, and thus it was a natural choice to use those generated files during the build - and thus, we opted for using disable_global flag for Gazelle to disable Bazel based proto compilation to take place. But this would cause issues if Go code imports grpc-gateway and build using Bazel + Gazelle with pre-generated .pb.go as is.
When resolving the grpc-gateway dependency from my code, the predefined BUILD.bazel in grpc-gateway assumes Bazel user uses go_proto_library to compile proto files - and it conflicts with disable_global. At that point, there will be duplicated dependency for the Well Known Types, and Bazel would throw a conflict error @com_github_golang_protobuf//ptypes/timestamp:timestamp and @io_bazel_rules_go//proto/wkt:timestamp_go_proto.
[ Ref: More on disable_global flag - https://github.com/bazelbuild/bazel-gazelle/blob/master/README.rst ]

If the setup is the other way around (.pb.go used with disable_global flag in grpc-gateway, and Bazel user uses go_proto_library), it may work - but I'm not too certain about that, will test further later.

As to the change, following the Gazelle documentation https://github.com/bazelbuild/bazel-gazelle/blob/master/README.rst, I think we essentially need the following:

  • Disable Bazel to compile .proto file (/BUILD - 3c7bb66)
  • Run bazel run gazelle -- update-repos -from_file=go.mod --build_file_proto_mode=disable_global -to_macro=repositories.bzl%go_repositories (211a15f)
  • Remove go_proto_library usages, as Gazelle run won't delete them (cdb8a30)
  • Use already generated .pb.go files instead (aeeac8a)
  • Documentation update to hold the relevant flag (43365a4)

I have put together a branch for quick diff, and a draft PR.
#1859
I'm not 100% certain if this is the right approach, but hope this clarifies what I'm trying to achieve.

@johanbrandhorst
Copy link
Collaborator

We're definitely going to need thorough testing on what the consequences of this change are going to be for other bazel consumers. There's a part of me that wonders why this has never been a problem before we bazel consumers. The repository has had bazel support for several years and my bias is to keep it as it is and help you find a solution that can resolve your problems without changing this repository. If that's not possible, then we'll consider making these changes if we're certain it won't affect any existing users, but that is a very high bar to achieve. I'm sure you appreciate our position.

I'd be more comfortable with this if I could hear from someone with more bazel experience (CC: @achew22).

@rytswd
Copy link
Author

rytswd commented Dec 5, 2020

I understand your concern about this massive change, but as far as I could get input in the referenced issue from Bazel maintainer (bazelbuild/rules_go#2738), some sort of change is needed in this repo. We may need to ask for their help to find if there is any other possible approaches.

I'm surprised how this hasn't hit other Bazel users - would love to hear from the community if this has been an issue for anyone. Just for the record, I have tried integrating grpc-gateway in my code several times in the past year or so, and there were multiple issues which needed upstream changes in Bazel (or more precisely rules_go). Now, as those are cleared out of the way, the only blocker seems to be how the Bazel usage here is assumed to be one specific way. If it's specific for my use case (though the repro is quite easy), I may just work out with a fork repo instead.

Also, it's worth noting that there is a potential feature addition discussed in Gazelle side to ignore the existing Bazel files in bazelbuild/bazel-gazelle#941 - that support should surely remediate the issue I鈥檓 facing.

Additionally, I鈥檇 love to understand what/why Bazel is used here for, as I couldn鈥檛 see much benefit from quick scanning.

@jayconrod
Copy link

In bazelbuild/rules_go#2738, I suggested folks maintaining grpc-gateway may be able to provide better advice than I could -- I didn't mean to imply any change is needed here.

The core issue is that in rules_go, generated proto targets like @io_bazel_rules_go//proto/wkt:timestamp_go_proto are declared separately from their pre-generated versions like @com_github_golang_protobuf//ptypes/timestamp). Those targets conflict with each other, so any binary using them needs to consistently use one set or the other.

Gazelle and go_repository provide a few different proto modes to control whether proto targets are generated and whether imports of proto packages are resolved to the generated or pre-generated versions. In the default mode, Gazelle generates go_proto_library targets and resolves imports to them. In disable_global mode, Gazelles ignores protos and resolves imports to pre-generated Go packages. The proto mode needs to be used consistently, so if disable_global is used, it needs to be used in the main workspace and in all repositories.

If grpc-gateway depends on go_proto_library targets, it's probably not possible to support users of both modes using the same set of build files. So @johanbrandhorst, if you want to support both scenarios, you'd need to provide release artifacts or tagged versions with different build files.

Alternatively, @rytswd, you could alter the build files in your dependency using the patches attribute in either http_archive or go_repository.

@rytswd
Copy link
Author

rytswd commented Dec 7, 2020

@jayconrod Thank you for the details, and sorry for misunderstanding your earlier comment 馃槹
I'm not certain how widely disable_global mode is used in the community, but I'd personally love to see that supported.

In the meantime, let me try out the approach with the patches attribute.

@rytswd
Copy link
Author

rytswd commented Dec 7, 2020

I have got the patch to work for me. The following gives me the patch needed for disable_global users.

{
    # Create a directory to prepare a patch
    mkdir /tmp/grpc-gateway-patch/
    cd /tmp/grpc-gateway-patch/

    # Shallow clone based on version
    git clone --depth 1 --branch v2.0.1 https://github.com/grpc-ecosystem/grpc-gateway.git
    cd ./grpc-gateway

    # (Optional)
    # Remove bazel version if you need to run using different Bazel version
    rm .bazelversion

    # Add Gazelle directive to disable proto compilation
    echo '# gazelle:proto disable_global' >> BUILD

    # Run Gazelle update-repos command to update repositories.bzl with 
    # disable_global flag
    bazel run gazelle -- update-repos \
        -from_file=go.mod \
        -to_macro=repositories.bzl%go_repositories \
        --build_file_proto_mode=disable_global

    # Remove BUILD.bazel file with conflicting import
    rm runtime/BUILD.bazel
    rm runtime/internal/examplepb/BUILD.bazel

    # Run Gazelle fix command to regenerate BUILD.bazel based on diasble_global
    bazel run gazelle -- fix

    # Create a patch file for two files that causes the build error:
    #   - `repositories.bzl`
    #   - `runtime/BUILD.bazel`
    #   - `runtime/internal/examplepb/BUILD.bazel`
    git diff -u repositories.bzl runtime/BUILD.bazel runtime/internal/examplepb/BUILD.bazel > ../grpc-gateway.patch

    # Get back and clean up shallow clone
    cd ../
    rm -rf ./grpc-gateway
}

Ref: Generated grpc-gateway.patch patch file

Click to expand
diff --git a/repositories.bzl b/repositories.bzl
index e042a16..221f40e 100644
--- a/repositories.bzl
+++ b/repositories.bzl
@@ -3,18 +3,21 @@ load("@bazel_gazelle//:deps.bzl", "go_repository")
 def go_repositories():
     go_repository(
         name = "co_honnef_go_tools",
+        build_file_proto_mode = "disable_global",
         importpath = "honnef.co/go/tools",
         sum = "h1:UoveltGrhghAA7ePc+e+QYDHXrBps2PqFZiHkGR/xK8=",
         version = "v0.0.1-2020.1.4",
     )
     go_repository(
         name = "com_github_antihax_optional",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/antihax/optional",
         sum = "h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=",
         version = "v1.0.0",
     )
     go_repository(
         name = "com_github_burntsushi_toml",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/BurntSushi/toml",
         sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=",
         version = "v0.3.1",
@@ -22,12 +25,14 @@ def go_repositories():
 
     go_repository(
         name = "com_github_burntsushi_xgb",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/BurntSushi/xgb",
         sum = "h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=",
         version = "v0.0.0-20160522181843-27f122750802",
     )
     go_repository(
         name = "com_github_census_instrumentation_opencensus_proto",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/census-instrumentation/opencensus-proto",
         sum = "h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk=",
         version = "v0.2.1",
@@ -35,24 +40,28 @@ def go_repositories():
 
     go_repository(
         name = "com_github_chzyer_logex",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/chzyer/logex",
         sum = "h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=",
         version = "v1.1.10",
     )
     go_repository(
         name = "com_github_chzyer_readline",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/chzyer/readline",
         sum = "h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=",
         version = "v0.0.0-20180603132655-2972be24d48e",
     )
     go_repository(
         name = "com_github_chzyer_test",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/chzyer/test",
         sum = "h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=",
         version = "v0.0.0-20180213035817-a1ea475d72b1",
     )
     go_repository(
         name = "com_github_client9_misspell",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/client9/misspell",
         sum = "h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=",
         version = "v0.3.4",
@@ -60,6 +69,7 @@ def go_repositories():
 
     go_repository(
         name = "com_github_cncf_udpa_go",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/cncf/udpa/go",
         sum = "h1:WBZRG4aNOuI15bLRrCgN8fCq8E5Xuty6jGbmSNEvSsU=",
         version = "v0.0.0-20191209042840-269d4d468f6f",
@@ -67,24 +77,28 @@ def go_repositories():
 
     go_repository(
         name = "com_github_davecgh_go_spew",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/davecgh/go-spew",
         sum = "h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=",
         version = "v1.1.0",
     )
     go_repository(
         name = "com_github_envoyproxy_go_control_plane",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/envoyproxy/go-control-plane",
         sum = "h1:rEvIZUSZ3fx39WIi3JkQqQBitGwpELBIYWeBVh6wn+E=",
         version = "v0.9.4",
     )
     go_repository(
         name = "com_github_envoyproxy_protoc_gen_validate",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/envoyproxy/protoc-gen-validate",
         sum = "h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=",
         version = "v0.1.0",
     )
     go_repository(
         name = "com_github_ghodss_yaml",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/ghodss/yaml",
         sum = "h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=",
         version = "v1.0.0",
@@ -92,18 +106,21 @@ def go_repositories():
 
     go_repository(
         name = "com_github_go_gl_glfw",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/go-gl/glfw",
         sum = "h1:QbL/5oDUmRBzO9/Z7Seo6zf912W/a6Sr4Eu0G/3Jho0=",
         version = "v0.0.0-20190409004039-e6da0acd62b1",
     )
     go_repository(
         name = "com_github_go_gl_glfw_v3_3_glfw",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/go-gl/glfw/v3.3/glfw",
         sum = "h1:WtGNWLvXpe6ZudgnXrq0barxBImvnnJoMEhXAzcbM0I=",
         version = "v0.0.0-20200222043503-6f7a984d4dc4",
     )
     go_repository(
         name = "com_github_golang_glog",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/golang/glog",
         sum = "h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=",
         version = "v0.0.0-20160126235308-23def4e6c14b",
@@ -111,18 +128,21 @@ def go_repositories():
 
     go_repository(
         name = "com_github_golang_groupcache",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/golang/groupcache",
         sum = "h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=",
         version = "v0.0.0-20200121045136-8c9f03a8e57e",
     )
     go_repository(
         name = "com_github_golang_mock",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/golang/mock",
         sum = "h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=",
         version = "v1.4.4",
     )
     go_repository(
         name = "com_github_golang_protobuf",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/golang/protobuf",
         sum = "h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=",
         version = "v1.4.3",
@@ -130,12 +150,14 @@ def go_repositories():
 
     go_repository(
         name = "com_github_google_btree",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/google/btree",
         sum = "h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=",
         version = "v1.0.0",
     )
     go_repository(
         name = "com_github_google_go_cmp",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/google/go-cmp",
         sum = "h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=",
         version = "v0.5.2",
@@ -143,30 +165,35 @@ def go_repositories():
 
     go_repository(
         name = "com_github_google_martian",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/google/martian",
         sum = "h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=",
         version = "v2.1.0+incompatible",
     )
     go_repository(
         name = "com_github_google_martian_v3",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/google/martian/v3",
         sum = "h1:pMen7vLs8nvgEYhywH3KDWJIJTeEr2ULsVWHWYHQyBs=",
         version = "v3.0.0",
     )
     go_repository(
         name = "com_github_google_pprof",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/google/pprof",
         sum = "h1:Ak8CrdlwwXwAZxzS66vgPt4U8yUZX7JwLvVR58FN5jM=",
         version = "v0.0.0-20200708004538-1a94d8640e99",
     )
     go_repository(
         name = "com_github_google_renameio",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/google/renameio",
         sum = "h1:GOZbcHa3HfsPKPlmyPyN2KEohoMXOhdMbHrvbpl2QaA=",
         version = "v0.1.0",
     )
     go_repository(
         name = "com_github_google_uuid",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/google/uuid",
         sum = "h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=",
         version = "v1.1.2",
@@ -174,66 +201,77 @@ def go_repositories():
 
     go_repository(
         name = "com_github_googleapis_gax_go_v2",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/googleapis/gax-go/v2",
         sum = "h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=",
         version = "v2.0.5",
     )
     go_repository(
         name = "com_github_hashicorp_golang_lru",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/hashicorp/golang-lru",
         sum = "h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=",
         version = "v0.5.1",
     )
     go_repository(
         name = "com_github_ianlancetaylor_demangle",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/ianlancetaylor/demangle",
         sum = "h1:UDMh68UUwekSh5iP2OMhRRZJiiBccgV7axzUG8vi56c=",
         version = "v0.0.0-20181102032728-5e5cf60278f6",
     )
     go_repository(
         name = "com_github_jstemmer_go_junit_report",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/jstemmer/go-junit-report",
         sum = "h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfEUpgAwUN0o=",
         version = "v0.9.1",
     )
     go_repository(
         name = "com_github_kisielk_gotool",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/kisielk/gotool",
         sum = "h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg=",
         version = "v1.0.0",
     )
     go_repository(
         name = "com_github_kr_pretty",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/kr/pretty",
         sum = "h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=",
         version = "v0.1.0",
     )
     go_repository(
         name = "com_github_kr_pty",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/kr/pty",
         sum = "h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=",
         version = "v1.1.1",
     )
     go_repository(
         name = "com_github_kr_text",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/kr/text",
         sum = "h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=",
         version = "v0.1.0",
     )
     go_repository(
         name = "com_github_pmezard_go_difflib",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/pmezard/go-difflib",
         sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=",
         version = "v1.0.0",
     )
     go_repository(
         name = "com_github_prometheus_client_model",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/prometheus/client_model",
         sum = "h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=",
         version = "v0.0.0-20190812154241-14fe0d1b01d4",
     )
     go_repository(
         name = "com_github_rogpeppe_fastuuid",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/rogpeppe/fastuuid",
         sum = "h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s=",
         version = "v1.2.0",
@@ -241,30 +279,35 @@ def go_repositories():
 
     go_repository(
         name = "com_github_rogpeppe_go_internal",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/rogpeppe/go-internal",
         sum = "h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=",
         version = "v1.3.0",
     )
     go_repository(
         name = "com_github_stretchr_objx",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/stretchr/objx",
         sum = "h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=",
         version = "v0.1.0",
     )
     go_repository(
         name = "com_github_stretchr_testify",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/stretchr/testify",
         sum = "h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=",
         version = "v1.4.0",
     )
     go_repository(
         name = "com_github_yuin_goldmark",
+        build_file_proto_mode = "disable_global",
         importpath = "github.com/yuin/goldmark",
         sum = "h1:5tjfNdR2ki3yYQ842+eX2sQHeiwpKJ0RnHO4IYOc4V8=",
         version = "v1.1.32",
     )
     go_repository(
         name = "com_google_cloud_go",
+        build_file_proto_mode = "disable_global",
         importpath = "cloud.google.com/go",
         sum = "h1:Dg9iHVQfrhq82rUNu9ZxUDrJLaxFUe/HlCVaLyRruq8=",
         version = "v0.65.0",
@@ -272,36 +315,42 @@ def go_repositories():
 
     go_repository(
         name = "com_google_cloud_go_bigquery",
+        build_file_proto_mode = "disable_global",
         importpath = "cloud.google.com/go/bigquery",
         sum = "h1:PQcPefKFdaIzjQFbiyOgAqyx8q5djaE7x9Sqe712DPA=",
         version = "v1.8.0",
     )
     go_repository(
         name = "com_google_cloud_go_datastore",
+        build_file_proto_mode = "disable_global",
         importpath = "cloud.google.com/go/datastore",
         sum = "h1:/May9ojXjRkPBNVrq+oWLqmWCkr4OU5uRY29bu0mRyQ=",
         version = "v1.1.0",
     )
     go_repository(
         name = "com_google_cloud_go_pubsub",
+        build_file_proto_mode = "disable_global",
         importpath = "cloud.google.com/go/pubsub",
         sum = "h1:ukjixP1wl0LpnZ6LWtZJ0mX5tBmjp1f8Sqer8Z2OMUU=",
         version = "v1.3.1",
     )
     go_repository(
         name = "com_google_cloud_go_storage",
+        build_file_proto_mode = "disable_global",
         importpath = "cloud.google.com/go/storage",
         sum = "h1:STgFzyU5/8miMl0//zKh2aQeTyeaUH3WN9bSUiJ09bA=",
         version = "v1.10.0",
     )
     go_repository(
         name = "com_shuralyov_dmitri_gpu_mtl",
+        build_file_proto_mode = "disable_global",
         importpath = "dmitri.shuralyov.com/gpu/mtl",
         sum = "h1:VpgP7xuJadIUuKccphEpTJnWhS2jkQyMt6Y7pJCD7fY=",
         version = "v0.0.0-20190408044501-666a987793e9",
     )
     go_repository(
         name = "in_gopkg_check_v1",
+        build_file_proto_mode = "disable_global",
         importpath = "gopkg.in/check.v1",
         sum = "h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=",
         version = "v1.0.0-20180628173108-788fd7840127",
@@ -309,12 +358,14 @@ def go_repositories():
 
     go_repository(
         name = "in_gopkg_errgo_v2",
+        build_file_proto_mode = "disable_global",
         importpath = "gopkg.in/errgo.v2",
         sum = "h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=",
         version = "v2.1.0",
     )
     go_repository(
         name = "in_gopkg_yaml_v2",
+        build_file_proto_mode = "disable_global",
         importpath = "gopkg.in/yaml.v2",
         sum = "h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=",
         version = "v2.2.3",
@@ -322,48 +373,56 @@ def go_repositories():
 
     go_repository(
         name = "io_opencensus_go",
+        build_file_proto_mode = "disable_global",
         importpath = "go.opencensus.io",
         sum = "h1:LYy1Hy3MJdrCdMwwzxA/dRok4ejH+RwNGbuoD9fCjto=",
         version = "v0.22.4",
     )
     go_repository(
         name = "io_rsc_binaryregexp",
+        build_file_proto_mode = "disable_global",
         importpath = "rsc.io/binaryregexp",
         sum = "h1:HfqmD5MEmC0zvwBuF187nq9mdnXjXsSivRiXN7SmRkE=",
         version = "v0.2.0",
     )
     go_repository(
         name = "io_rsc_quote_v3",
+        build_file_proto_mode = "disable_global",
         importpath = "rsc.io/quote/v3",
         sum = "h1:9JKUTTIUgS6kzR9mK1YuGKv6Nl+DijDNIc0ghT58FaY=",
         version = "v3.1.0",
     )
     go_repository(
         name = "io_rsc_sampler",
+        build_file_proto_mode = "disable_global",
         importpath = "rsc.io/sampler",
         sum = "h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=",
         version = "v1.3.0",
     )
     go_repository(
         name = "org_golang_google_api",
+        build_file_proto_mode = "disable_global",
         importpath = "google.golang.org/api",
         sum = "h1:yfrXXP61wVuLb0vBcG6qaOoIoqYEzOQS8jum51jkv2w=",
         version = "v0.30.0",
     )
     go_repository(
         name = "org_golang_google_appengine",
+        build_file_proto_mode = "disable_global",
         importpath = "google.golang.org/appengine",
         sum = "h1:lMO5rYAqUxkmaj76jAkRUvt5JZgFymx/+Q5Mzfivuhc=",
         version = "v1.6.6",
     )
     go_repository(
         name = "org_golang_google_genproto",
+        build_file_proto_mode = "disable_global",
         importpath = "google.golang.org/genproto",
         sum = "h1:bFFRpT+e8JJVY7lMMfvezL1ZIwqiwmPl2bsE2yx4HqM=",
         version = "v0.0.0-20201019141844-1ed22bb0c154",
     )
     go_repository(
         name = "org_golang_google_grpc",
+        build_file_proto_mode = "disable_global",
         importpath = "google.golang.org/grpc",
         sum = "h1:DGeFlSan2f+WEtCERJ4J9GJWk15TxUi8QGagfI87Xyc=",
         version = "v1.33.1",
@@ -371,24 +430,28 @@ def go_repositories():
 
     go_repository(
         name = "org_golang_google_grpc_cmd_protoc_gen_go_grpc",
+        build_file_proto_mode = "disable_global",
         importpath = "google.golang.org/grpc/cmd/protoc-gen-go-grpc",
         sum = "h1:lQ+dE99pFsb8osbJB3oRfE5eW4Hx6a/lZQr8Jh+eoT4=",
         version = "v1.0.0",
     )
     go_repository(
         name = "org_golang_google_protobuf",
+        build_file_proto_mode = "disable_global",
         importpath = "google.golang.org/protobuf",
         sum = "h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=",
         version = "v1.25.0",
     )
     go_repository(
         name = "org_golang_x_crypto",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/crypto",
         sum = "h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=",
         version = "v0.0.0-20200622213623-75b288015ac9",
     )
     go_repository(
         name = "org_golang_x_exp",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/exp",
         sum = "h1:QE6XYQK6naiK1EPAe1g/ILLxN5RBoH5xkJk3CqlMI/Y=",
         version = "v0.0.0-20200224162631-6cc2880d07d6",
@@ -396,12 +459,14 @@ def go_repositories():
 
     go_repository(
         name = "org_golang_x_image",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/image",
         sum = "h1:+qEpEAPhDZ1o0x3tHzZTQDArnOixOzGD9HUJfcg0mb4=",
         version = "v0.0.0-20190802002840-cff245a6509b",
     )
     go_repository(
         name = "org_golang_x_lint",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/lint",
         sum = "h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=",
         version = "v0.0.0-20200302205851-738671d3881b",
@@ -409,42 +474,49 @@ def go_repositories():
 
     go_repository(
         name = "org_golang_x_mobile",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/mobile",
         sum = "h1:4+4C/Iv2U4fMZBiMCc98MG1In4gJY5YRhtpDNeDeHWs=",
         version = "v0.0.0-20190719004257-d2bd2a29d028",
     )
     go_repository(
         name = "org_golang_x_mod",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/mod",
         sum = "h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=",
         version = "v0.3.0",
     )
     go_repository(
         name = "org_golang_x_net",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/net",
         sum = "h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=",
         version = "v0.0.0-20200822124328-c89045814202",
     )
     go_repository(
         name = "org_golang_x_oauth2",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/oauth2",
         sum = "h1:ld7aEMNHoBnnDAX15v1T6z31v8HwR2A9FYOuAhWqkwc=",
         version = "v0.0.0-20200902213428-5d25da1a8d43",
     )
     go_repository(
         name = "org_golang_x_sync",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/sync",
         sum = "h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=",
         version = "v0.0.0-20200625203802-6e8e738ad208",
     )
     go_repository(
         name = "org_golang_x_sys",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/sys",
         sum = "h1:B6caxRw+hozq68X2MY7jEpZh/cr4/aHLv9xU8Kkadrw=",
         version = "v0.0.0-20200803210538-64077c9b5642",
     )
     go_repository(
         name = "org_golang_x_text",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/text",
         sum = "h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=",
         version = "v0.3.3",
@@ -452,12 +524,14 @@ def go_repositories():
 
     go_repository(
         name = "org_golang_x_time",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/time",
         sum = "h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=",
         version = "v0.0.0-20191024005414-555d28b269f0",
     )
     go_repository(
         name = "org_golang_x_tools",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/tools",
         sum = "h1:W07d4xkoAUSNOkOzdzXCdFGxT7o2rW4q8M34tB2i//k=",
         version = "v0.0.0-20200825202427-b303f430e36d",
@@ -465,6 +539,7 @@ def go_repositories():
 
     go_repository(
         name = "org_golang_x_xerrors",
+        build_file_proto_mode = "disable_global",
         importpath = "golang.org/x/xerrors",
         sum = "h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=",
         version = "v0.0.0-20200804184101-5ec99f83aff1",
diff --git a/runtime/BUILD.bazel b/runtime/BUILD.bazel
index 68e5f44..23a3dcb 100644
--- a/runtime/BUILD.bazel
+++ b/runtime/BUILD.bazel
@@ -1,7 +1,5 @@
 load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
 
-package(default_visibility = ["//visibility:public"])
-
 go_library(
     name = "go_default_library",
     srcs = [
@@ -23,15 +21,16 @@ go_library(
         "query.go",
     ],
     importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/runtime",
+    visibility = ["//visibility:public"],
     deps = [
         "//internal/httprule:go_default_library",
         "//utilities:go_default_library",
-        "@com_github_golang_protobuf//ptypes:go_default_library_gen",
-        "@go_googleapis//google/api:httpbody_go_proto",
-        "@io_bazel_rules_go//proto/wkt:duration_go_proto",
-        "@io_bazel_rules_go//proto/wkt:field_mask_go_proto",
-        "@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
-        "@io_bazel_rules_go//proto/wkt:wrappers_go_proto",
+        "@com_github_golang_protobuf//ptypes:go_default_library",
+        "@com_github_golang_protobuf//ptypes/duration:go_default_library",
+        "@com_github_golang_protobuf//ptypes/timestamp:go_default_library",
+        "@com_github_golang_protobuf//ptypes/wrappers:go_default_library",
+        "@org_golang_google_genproto//googleapis/api/httpbody:go_default_library",
+        "@org_golang_google_genproto//protobuf/field_mask:go_default_library",
         "@org_golang_google_grpc//codes:go_default_library",
         "@org_golang_google_grpc//grpclog:go_default_library",
         "@org_golang_google_grpc//metadata:go_default_library",
@@ -45,7 +44,6 @@ go_library(
 
 go_test(
     name = "go_default_test",
-    size = "small",
     srcs = [
         "context_test.go",
         "convert_test.go",
@@ -65,18 +63,18 @@ go_test(
     deps = [
         "//runtime/internal/examplepb:go_default_library",
         "//utilities:go_default_library",
-        "@com_github_golang_protobuf//ptypes:go_default_library_gen",
+        "@com_github_golang_protobuf//ptypes:go_default_library",
+        "@com_github_golang_protobuf//ptypes/duration:go_default_library",
+        "@com_github_golang_protobuf//ptypes/empty:go_default_library",
+        "@com_github_golang_protobuf//ptypes/struct:go_default_library",
+        "@com_github_golang_protobuf//ptypes/timestamp:go_default_library",
+        "@com_github_golang_protobuf//ptypes/wrappers:go_default_library",
         "@com_github_google_go_cmp//cmp:go_default_library",
         "@com_github_google_go_cmp//cmp/cmpopts:go_default_library",
-        "@go_googleapis//google/api:httpbody_go_proto",
-        "@go_googleapis//google/rpc:errdetails_go_proto",
-        "@go_googleapis//google/rpc:status_go_proto",
-        "@io_bazel_rules_go//proto/wkt:duration_go_proto",
-        "@io_bazel_rules_go//proto/wkt:empty_go_proto",
-        "@io_bazel_rules_go//proto/wkt:field_mask_go_proto",
-        "@io_bazel_rules_go//proto/wkt:struct_go_proto",
-        "@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
-        "@io_bazel_rules_go//proto/wkt:wrappers_go_proto",
+        "@org_golang_google_genproto//googleapis/api/httpbody:go_default_library",
+        "@org_golang_google_genproto//googleapis/rpc/errdetails:go_default_library",
+        "@org_golang_google_genproto//googleapis/rpc/status:go_default_library",
+        "@org_golang_google_genproto//protobuf/field_mask:go_default_library",
         "@org_golang_google_grpc//codes:go_default_library",
         "@org_golang_google_grpc//metadata:go_default_library",
         "@org_golang_google_grpc//status:go_default_library",
diff --git a/runtime/internal/examplepb/BUILD.bazel b/runtime/internal/examplepb/BUILD.bazel
index 5fee49c..1a219b7 100644
--- a/runtime/internal/examplepb/BUILD.bazel
+++ b/runtime/internal/examplepb/BUILD.bazel
@@ -1,43 +1,30 @@
-load("@rules_proto//proto:defs.bzl", "proto_library")
 load("@io_bazel_rules_go//go:def.bzl", "go_library")
-load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
 
-# gazelle:exclude non_standard_names_grpc.pb.go
-
-package(default_visibility = ["//visibility:public"])
-
-proto_library(
-    name = "examplepb_proto",
+go_library(
+    name = "go_default_library",
     srcs = [
-        "example.proto",
-        "non_standard_names.proto",
-        "proto2.proto",
-        "proto3.proto",
+        "example.pb.go",
+        "example_grpc.pb.go",
+        "non_standard_names.pb.go",
+        "non_standard_names_grpc.pb.go",
+        "proto2.pb.go",
+        "proto3.pb.go",
     ],
+    importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/runtime/internal/examplepb",
+    visibility = ["//runtime:__subpackages__"],
     deps = [
-        "@com_google_protobuf//:duration_proto",
-        "@com_google_protobuf//:empty_proto",
-        "@com_google_protobuf//:field_mask_proto",
-        "@com_google_protobuf//:struct_proto",
-        "@com_google_protobuf//:timestamp_proto",
-        "@com_google_protobuf//:wrappers_proto",
-        "@go_googleapis//google/api:annotations_proto",
-    ],
-)
-
-go_proto_library(
-    name = "examplepb_go_proto",
-    compilers = [
-        "//:go_apiv2",
-        "//:go_grpc",
+        "@com_github_golang_protobuf//proto:go_default_library",
+        "@com_github_golang_protobuf//ptypes/duration:go_default_library",
+        "@com_github_golang_protobuf//ptypes/empty:go_default_library",
+        "@com_github_golang_protobuf//ptypes/struct:go_default_library",
+        "@com_github_golang_protobuf//ptypes/timestamp:go_default_library",
+        "@com_github_golang_protobuf//ptypes/wrappers:go_default_library",
+        "@org_golang_google_genproto//googleapis/api/annotations:go_default_library",
+        "@org_golang_google_genproto//protobuf/field_mask:go_default_library",
+        "@org_golang_google_grpc//:go_default_library",
+        "@org_golang_google_grpc//codes:go_default_library",
+        "@org_golang_google_grpc//status:go_default_library",
+        "@org_golang_google_protobuf//reflect/protoreflect:go_default_library",
+        "@org_golang_google_protobuf//runtime/protoimpl:go_default_library",
     ],
-    importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/runtime/internal/examplepb",
-    proto = ":examplepb_proto",
-    deps = ["@go_googleapis//google/api:annotations_go_proto"],
-)
-
-go_library(
-    name = "go_default_library",
-    embed = [":examplepb_go_proto"],
-    importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/runtime/internal/examplepb",
 )

With the above file in place, I could use the following go_repository patches like below.

go_repository(
    name = "com_github_grpc_ecosystem_grpc_gateway_v2",
    build_file_proto_mode = "disable_global",
    importpath = "github.com/grpc-ecosystem/grpc-gateway/v2",
    patch_args = ["-p1"], # Added, Bazel complains without this
    patches = ["//bazel-patches:grpc-gateway.patch"], # Added, patch file assumed to be in //bazel-patches directory
    sum = "h1:X2vfSnm1WC8HEo0MBHZg2TcuDUHJj6kd1TmEAQncnSA=",
    version = "v2.0.1",
)

I have put together a working repo for this: https://github.com/rytswd/simple-bazel/tree/issue/bazelbuild/rules-go/grpc-gateway-timestamp-link-dupe

Although this isn't ideal as this patch needs to be fixed with each upgrade of grpc-gateway dependency, this is no longer a blocker for me.

@johanbrandhorst
Copy link
Collaborator

Thanks for looping back on this, do you think we can close this issue? I'm happy to discuss other strategies if this workaround turns out to be too painful for you, but as previously stated, this doesn't appear to be a common problem among bazel users.

@rytswd
Copy link
Author

rytswd commented Dec 7, 2020

Yes, certainly, I'm closing down the PR as well. I believe the above workaround can work for the Bazel users with similar use cases, and if it proves to affect many, we may be able to look into more permanent solutions. This could potentially be a part of documentation in the repo, but as there seems to be no good place for that, having this issue out here should suffice.

@rytswd rytswd closed this as completed Dec 7, 2020
rickystewart added a commit to rickystewart/cockroach that referenced this issue Jan 8, 2021
We used to use the checked-in `.pb.go` files in our Bazel build, but
these files should really be generated during the build (this is normal
Bazel style, and avoids the weird extra step of having to run
`make generate` and check in the `.pb.go` files whenever a `.proto` is
updated). Some specific implementation notes:

* Add a couple `go_proto_compiler`s for the `protoc-gen-gogoroach`
  plugin.

* We enable Gazelle handling of `.proto` files in `BUILD.bazel`, and add
  `.pb.gw.go` generation for those files that need it as well.

* In that file, we also add a bunch of `gazelle:resolve` and
  `gazelle:exclude` directives that fix some build issues. Generally,
  this is necessary for vendored protos, which Gazelle doesn't index
  correctly.

* Add a couple ad-hoc manual fixes to make compilation work (they can
  generally be found by searching for lines in `BUILD.bazel` files that
  are marked `keep`). Usually, this is necessary because of annotations,
  which Gazelle can't properly track the dependencies for.

* Do some extra dependency munging, which becomes necessary due to our
  usage of `rules_go`. First, move `go_repository()` declarations from
  `DEPS.bzl` to `WORKSPACE` so we can override the dependencies pulled
  in by `go_rules_dependencies()`. Finally, we have to patch some of the
  vendored code as well; the patches can be found in `build/patches`. We
  do this because Gazelle's default behavior would be to generate
  `proto_library()` and `go_proto_library()` declarations for every
  vendored `.proto` file, which is sometimes not what we want because it
  can introduce various compiler or linker issues. Where relevant, we
  use vendored pre-generated `.pb.go` files so we don't have to
  re-build those files ourselves. Elsewhere we need to replace the
  Gazelle-inferred dependencies on rules in
  `@io_bazel_rules_go//proto/wkt` with other Go libraries which can be
  safely linked. See [this thread](grpc-ecosystem/grpc-gateway#1847 (comment))
  for additional context.

Fixes cockroachdb#56067.

Release note: None
rickystewart added a commit to rickystewart/cockroach that referenced this issue Jan 8, 2021
We used to use the checked-in `.pb.go` files in our Bazel build, but
these files should really be generated during the build (this is normal
Bazel style, and avoids the weird extra step of having to run
`make generate` and check in the `.pb.go` files whenever a `.proto` is
updated). Some specific implementation notes:

* Add a couple `go_proto_compiler`s for the `protoc-gen-gogoroach`
  plugin.

* We enable Gazelle handling of `.proto` files in `BUILD.bazel`, and add
  `.pb.gw.go` generation for those files that need it as well.

* In that file, we also add a bunch of `gazelle:resolve` and
  `gazelle:exclude` directives that fix some build issues. Generally,
  this is necessary for vendored protos, which Gazelle doesn't index
  correctly.

* Add a couple ad-hoc manual fixes to make compilation work (they can
  generally be found by searching for lines in `BUILD.bazel` files that
  are marked `keep`). Usually, this is necessary because of annotations,
  which Gazelle can't properly track the dependencies for.

* Do some extra dependency munging, which becomes necessary due to our
  usage of `rules_go`. First, move `go_repository()` declarations from
  `DEPS.bzl` to `WORKSPACE` so we can override the dependencies pulled
  in by `go_rules_dependencies()`. Finally, we have to patch some of the
  vendored code as well; the patches can be found in `build/patches`. We
  do this because Gazelle's default behavior would be to generate
  `proto_library()` and `go_proto_library()` declarations for every
  vendored `.proto` file, which is sometimes not what we want because it
  can introduce various compiler or linker issues. Where relevant, we
  use vendored pre-generated `.pb.go` files so we don't have to
  re-build those files ourselves. Elsewhere we need to replace the
  Gazelle-inferred dependencies on rules in
  `@io_bazel_rules_go//proto/wkt` with other Go libraries which can be
  safely linked. See [this thread](grpc-ecosystem/grpc-gateway#1847 (comment))
  for additional context.

Fixes cockroachdb#56067.

Release note: None
craig bot pushed a commit to cockroachdb/cockroach that referenced this issue Jan 8, 2021
58660: build: generate .pb.go files in the bazel sandbox r=rickystewart a=rickystewart

We used to use the checked-in `.pb.go` files in our Bazel build, but
these files should really be generated during the build (this is normal
Bazel style, and avoids the weird extra step of having to run
`make generate` and check in the `.pb.go` files whenever a `.proto` is
updated). Some specific implementation notes:

* Add a couple `go_proto_compiler`s for the `protoc-gen-gogoroach`
  plugin.

* We enable Gazelle handling of `.proto` files in `BUILD.bazel`, and add
  `.pb.gw.go` generation for those files that need it as well.

* In that file, we also add a bunch of `gazelle:resolve` and
  `gazelle:exclude` directives that fix some build issues. Generally,
  this is necessary for vendored protos, which Gazelle doesn't index
  correctly.

* Add a couple ad-hoc manual fixes to make compilation work (they can
  generally be found by searching for lines in `BUILD.bazel` files that
  are marked `keep`). Usually, this is necessary because of annotations,
  which Gazelle can't properly track the dependencies for.

* Do some extra dependency munging, which becomes necessary due to our
  usage of `rules_go`. First, move `go_repository()` declarations from
  `DEPS.bzl` to `WORKSPACE` so we can override the dependencies pulled
  in by `go_rules_dependencies()`. Finally, we have to patch some of the
  vendored code as well; the patches can be found in `build/patches`. We
  do this because Gazelle's default behavior would be to generate
  `proto_library()` and `go_proto_library()` declarations for every
  vendored `.proto` file, which is sometimes not what we want because it
  can introduce various compiler or linker issues. Where relevant, we
  use vendored pre-generated `.pb.go` files so we don't have to
  re-build those files ourselves. Elsewhere we need to replace the
  Gazelle-inferred dependencies on rules in
  `@io_bazel_rules_go//proto/wkt` with other Go libraries which can be
  safely linked. See [this thread](grpc-ecosystem/grpc-gateway#1847 (comment))
  for additional context.

Fixes #56067.

Release note: None

Co-authored-by: Ricky Stewart <ricky@cockroachlabs.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 a pull request may close this issue.

3 participants