When php editor Banana is dealing with compilation errors, sometimes the "go mod tidy" command complains that the protobuf package generated by bazel is missing. The solution to this problem is actually very simple. You only need to manually add the corresponding protobuf package dependency in the go.mod file. By executing the "go mod tidy" command to update the dependencies, compiling again will eliminate the problem of package loss. This method is simple and effective, and can help developers quickly solve compilation errors and improve development efficiency.
I have a .proto
protobuf definition file in the directory and I am using bazel to build a go library from it as shown below (using ## below #gazelle Generated
build.bazel file):
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") proto_library( name = "events_proto", srcs = ["events.proto"], visibility = ["//visibility:public"], deps = ["@com_google_protobuf//:timestamp_proto"], ) go_proto_library( name = "proto_go_proto", importpath = "github.com/acme/icoyote/proto", proto = ":events_proto", visibility = ["//visibility:public"], ) go_library( name = "proto", embed = [":proto_go_proto"], importpath = "github.com/acme/icoyote/proto", visibility = ["//visibility:public"], )
//icoyote/proto:proto, and when I run
go mod tidy in the module, it complains about not finding package
github. com/acme/icoyote/proto:
go: finding module for package github.com/acme/icoyote/proto github.com/acme/icoyote/cmd/icoyote imports github.com/acme/icoyote/proto: no matching versions for query "latest"
what do I do? SolutionThis happens because bazel
does use protoc in the
build file Generate
.go files under the
go_proto_library rule, but only write them to a temporary directory under
bazel - bin used by the
go_library rule , and
go mod tidy doesn't seem to look into
bazel-bin (probably because it's a symlink, but if so, those files are relative to
go.mod The paths to the locations are all wrong)
protoc yourself, and remove the
proto_library and
go_proto_library rules in the
build file , and then change the
go_library rules to build the generated files. This is sub-optimal because you have to manually rerun
protoc every time you change the
.proto file (if you put it into the
//go:generate directive , you must rerun
gogenerate).
to the directory containing the
.proto file. It should look like this:
package proto
to ignore
empty.go (so when you run
gazelle --fix it won't try to
go_library Rules added to the
build file). We do this by adding the following to the
build file:
# gazelle:exclude empty.go
go mod tidy.
protoc approach. Maybe there is a way to create a symlink to bazel under
bazel-bin and write out the location of the generated
.go file and force
go mod tidy to follow It does, but I haven't tried it yet. If you do this and it works, please share!
The above is the detailed content of `go mod tidy` complains that the protobuf package generated by bazel is missing. For more information, please follow other related articles on the PHP Chinese website!