Home  >  Article  >  Backend Development  >  `go mod tidy` complains that the protobuf package generated by bazel is missing

`go mod tidy` complains that the protobuf package generated by bazel is missing

PHPz
PHPzforward
2024-02-10 10:09:171249browse

`go mod tidy` 抱怨 bazel 生成的 protobuf 包丢失

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.

Question content

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"],
)

Some other code depends on

//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"
p>Any IDE without bazel integration (such as vscode, goland/intellij without bazel plugin) will also complain

what do I do?

Solution

This 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)

One option is to manually generate the go file by calling

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).

Instead, we can do the following:

    Add the file
  1. empty.go to the directory containing the .proto file. It should look like this:
  2. package proto
    Then tell
  1. ngazelle 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:
  2. # gazelle:exclude empty.go
This is enough to shut up

go mod tidy.

This will also make the IDE stop complaining about imports, although you will still get errors when referencing anything that should be in the package. If you don't want to abandon the IDE in favor of the good goland or intellij idea with the bazel plugin, you may have to resort to the manual

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete