rules_go: rules_go (or gazelle?) fails to resolve google/rpc/code.proto in com_github_googleapis_gax_go_v2

Workaround / Solution

See mpawlowski’s solution or linzhp’s explanation.

What version of rules_go are you using?

0.41.0

What version of gazelle are you using?

0.32.0

What version of Bazel are you using?

6.2.0

Does this issue reproduce with the latest releases of all the above?

I didn’t try Bazel 6.2.1. rules_go and gazelle are latest. It does not reproduce with the previous releases, 0.40.1 and 0.31.1.

What operating system and processor architecture are you using?

Similar to Debian testing, x86.

Any other potentially useful information about your toolchain?

n/a

What did you do?

Updated to rules_go 0.41 and gazelle 0.32 and tried to build https://github.com/googlecloudrobotics/core.

What did you expect to see?

Build succeeds.

What did you see instead?

ERROR: /usr/local/google/home/rodrigoq/.cache/bazel/_bazel_rodrigoq/f3eae32d9daee2b06e08ee03f5238858/external/com_github_googleapis_gax_go_v2/apierror/internal/proto/BUILD.bazel:18:17: no such package '@com_github_googleapis_gax_go_v2//google/rpc': BUILD file not found in directory 'google/rpc' of external repository @com_github_googleapis_gax_go_v2. Add a BUILD file to a directory to mark it as a package. and referenced by '@com_github_googleapis_gax_go_v2//apierror/internal/proto:jsonerror_go_proto'

The error appears to be because code.proto is resolved incorrectly to //google/rpc:code_proto instead of to something inside @org_golang_google_genproto as the release notes suggest.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@drigz

FYI, I tried to do it the way fmeum suggested, but it didn’t work

As you mentioned in the description of this issue, com_github_googleapis_gax_go_v2 actually depends on //google/rpc:code_proto. 😃

So this is what would be necessary:

    go_repository(
        name = "com_github_googleapis_gax_go_v2",
        build_directives = [
            "gazelle:resolve proto go google/rpc/code.proto @org_golang_google_genproto_googleapis_rpc//code",  # keep
            "gazelle:resolve proto proto google/rpc/code.proto @googleapis//google/rpc:code_proto",  # keep
        ],
        importpath = "github.com/googleapis/gax-go/v2",
        sum = "h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas=",
        version = "v2.12.0",
    )

This is assuming that you have the googleapis archive using the name googleapis in your WORKSPACE:

http_archive(
    name = "googleapis",
    sha256 = "78aae8879967e273044bc786e691d9a16db385bd137454e80cd0b53476adfc2d",
    strip_prefix = "googleapis-c09efadc6785560333d967f0bd40f1d1c3232088",
    urls = ["https://github.com/googleapis/googleapis/archive/c09efadc6785560333d967f0bd40f1d1c3232088.tar.gz"],
)

load("@googleapis//:repository_rules.bzl", "switched_rules_by_language")

switched_rules_by_language(
    name = "com_google_googleapis_imports",
)

Also, build_file_generation = "on" does not seem necessary.

This did the trick for me

        build_directives = [
            "gazelle:resolve proto proto google/rpc/code.proto @googleapis//google/rpc:code_proto",  # keep
            "gazelle:resolve proto go    google/rpc/code.proto @org_golang_google_genproto_googleapis_rpc//code",  # keep
        ],

With Bzlmod, we have a registry of default build_directives which would free users from having to specify them manually.

For those using Bzlmod and struggling to resolve this manually (as I have been for many hours over the last couple days), adding the following to MODULE.bazel worked for our setup:

go_deps.gazelle_override(
    path = "github.com/googleapis/gax-go/v2",
    directives = [
        "gazelle:proto disable",
    ]
)

I suppose this might be a useful addition to the registry of default build directives.

Please read the release notes of rules_go 0.41 and Gazelle 0.32 for migration steps

The release notes overlook the case of a dependency on a third-party repo that depends on googleapis - unfortunately this probably affects a larger number of clueless maintainers like me.

all Go code importing generated code from Google APIs will depend on @org_golang_google_genproto, which is resolved by Go modules. For proto files importing Google APIs proto and generating Go code, users need to: […] Resolve the proto manually. If Gazelle is being used, directives like the following need to be added to a parent directory of the proto files […]

I don’t understand whether com_github_googleapis_gax_go_v2 is “importing Google APIs proto and generating Go code” or whether it should be. It was also not clear how/where to add these resolution directives. fmeum’s comment adds the missing info (thank you) and suggests that I should tell go_repository to generate Go code.

mpawlowski’s comment implies that the go_repository does not need to generate the Go code, but that Gazelle’s update-repos command is unable to guess this. This seems like a much simpler workaround that is less likely to break if the repo adds new dependencies on googleapis in future, but I (obviously) don’t understand everything that’s going on here.

@fmeum Is build_file_proto_mode = "disable_global" a good way to solve the problem? Or is there a reason the maintainers did not suggest it?