bzlmod gazelle cannot produce a correct BUILD file for grpc.
What version of gazelle are you using?
0.40.0
What version of rules_go are you using?
0.50.1
What version of Bazel are you using?
8.0.0
Does this issue reproduce with the latest releases of all the above?
yes
What operating system and processor architecture are you using?
macos arm m2
What did you do?
Try to build my sample project https://github.com/gator1/rundoo with bazel.
Had a lot of trouble using WORKSPACE and decided to use all latest. I used bzlmod. Cannot make gazelle generate the right BUILD file, it produced reference to io_bazel_rules_go which bazel cannot find the repo.
my project rundoo was built with go build and works fine. It uses grpc where I think bazel/gazelle belmod has trouble with.
I follow this doc https://github.com/bazel-contrib/rules_go/blob/master/docs/go/core/bzlmod.md
BUILD file is
load("@gazelle//:def.bzl", "gazelle")
gazelle(name = "gazelle")
MODULE.bazel
bazel_dep(name = "gazelle", version = "0.40.0")
bazel_dep(name = "rules_go", version = "0.50.1")
bazel_dep(name = "rules_proto_grpc", version = "5.0.1")
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
# Download a suitable Go SDK.
go_sdk.download(version = "1.23.3")
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(go_deps, "com_github_data_dog_go_sqlmock", "com_github_jarcoal_httpmock", "com_github_lib_pq", "com_github_stretchr_testify", "org_golang_google_grpc", "org_golang_google_protobuf")
.bazelrc
common --enable_bzlmod
.bazelversion
8.0.0
Passed bazel run //:gazelle with warning
bazel run //:gazelle
INFO: Analyzed target //:gazelle (2 packages loaded, 105 targets configured).
INFO: Found 1 target...
Target //:gazelle up-to-date:
bazel-bin/gazelle-runner.bash
bazel-bin/gazelle
INFO: Elapsed time: 0.380s, Critical Path: 0.03s
INFO: 1 process: 67 action cache hit, 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/gazelle
gazelle: could not parse directive: app/registry //app/registry:go_default_library
expected gazelle:resolve source-language [import-language] import-string label
gazelle: finding module path for import app/log: go: -d flag is deprecated. -d=true is a no-op
go: malformed module path "app/log": missing dot in first path element
gazelle: finding module path for import app/log: go: -d flag is deprecated. -d=true is a no-op
go: malformed module path "app/log": missing dot in first path element
gazelle: finding module path for import app/log: go: -d flag is deprecated. -d=true is a no-op
go: malformed module path "app/log": missing dot in first path element
failed bazel build ...
bazel build ...
WARNING: Target pattern parsing failed.
ERROR: Skipping '...': error loading package under directory '': error loading package 'api/v1': Unable to find package for @@[unknown repo 'rules_proto' requested from @@ (did you mean 'rules_go'?)]//proto:defs.bzl: The repository '@@[unknown repo 'rules_proto' requested from @@ (did you mean 'rules_go'?)]' could not be resolved: No repository visible as '@rules_proto' from main repository.
ERROR: error loading package under directory '': error loading package 'api/v1': Unable to find package for @@[unknown repo 'rules_proto' requested from @@ (did you mean 'rules_go'?)]//proto:defs.bzl: The repository '@@[unknown repo 'rules_proto' requested from @@ (did you mean 'rules_go'?)]' could not be resolved: No repository visible as '@rules_proto' from main repository.
INFO: Elapsed time: 0.132s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
I pushed all the changes to https://github.com/gator1/rundoo. Please check its bzlmod branch, the main branch doesn't have bazel stuff.
### What did you expect to see?
bazel build ... works
### What did you see instead?
bazel build ...
WARNING: Target pattern parsing failed.
ERROR: Skipping '...': error loading package under directory '': error loading package 'log': Unable to find package for @@[unknown repo 'io_bazel_rules_go' requested from @@]//go:def.bzl: The repository '@@[unknown repo 'io_bazel_rules_go' requested from @@]' could not be resolved: No repository visible as '@io_bazel_rules_go' from main repository.
ERROR: error loading package under directory '': error loading package 'log': Unable to find package for @@[unknown repo 'io_bazel_rules_go' requested from @@]//go:def.bzl: The repository '@@[unknown repo 'io_bazel_rules_go' requested from @@]' could not be resolved: No repository visible as '@io_bazel_rules_go' from main repository.
INFO: Elapsed time: 0.125s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
It generated
go_proto_library(
name = "v1_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "github.com/gator1/rundoo/api/v1",
proto = ":v1_proto",
visibility = ["//visibility:public"],
)
in BUILD.bazel under app/api/v1.
I don't understand the relationship between io_bazel_rules_go and rules_go but I didn't have this problem when I tried WORKSPACE. WORKSPACE would have a few other issues.
forgot to mention: I also tried to add protobuf
bazel_dep(name = "gazelle", version = "0.40.0")
bazel_dep(name = "rules_go", version = "0.50.1")
bazel_dep(name = "protobuf", version = "29.2")
bazel_dep(name = "rules_proto_grpc", version = "5.0.1")
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
# Download a suitable Go SDK.
go_sdk.download(version = "1.23.3")
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(go_deps, "com_github_data_dog_go_sqlmock", "com_github_jarcoal_httpmock", "com_github_lib_pq", "com_github_stretchr_testify", "org_golang_google_grpc", "org_golang_google_protobuf")
got the same result.
found this: https://fzakaria.com/2024/11/28/bazel-knowledge-protobuf-is-the-worst-when-it-should-be-the-best.html
Is there a workaround?
@gator1 This also works:
Add explicit compiler directives to override Gazelle's default behavior in your root BUILD.bazel or per-package files:
# gazelle:go_grpc_compilers @rules_go//proto:go_grpc
# gazelle:go_proto_compilers @rules_go//proto:go_proto