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

PHPz
Release: 2024-02-10 10:09:17
forward
1248 people have browsed it

`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"],
)
Copy after login

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"
Copy after login
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
    Copy after login
Then tell
  • 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:
  • # gazelle:exclude empty.go
    Copy after login
    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!

    Related labels:
    source:stackoverflow.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template