使用bazel-gazelle时,Go proto导入出现错误。

huangapple go评论127阅读模式
英文:

Error with Go proto imports when using bazel-gazelle

问题

我有一个仓库,其中我使用了gazelle和proto build规则。我的仓库结构如下:

  1. WORKSPACE
  2. \ proto
  3. - user.proto
  4. - user_service.proto
  5. - BUILD

这是我在proto文件夹中的BUILD文件:

  1. load("@rules_proto//proto:defs.bzl", "proto_library")
  2. load("@rules_proto_grpc//go:defs.bzl", "go_proto_library")
  3. package(default_visibility = ["//visibility:public"])
  4. proto_library(
  5. name = "user_proto",
  6. srcs = ["user.proto"],
  7. deps = [
  8. "@com_google_protobuf//:timestamp_proto",
  9. ],
  10. )
  11. proto_library(
  12. name = "user_service_proto",
  13. srcs = ["user_service.proto"],
  14. deps = [
  15. ":user_proto",
  16. "@com_google_protobuf//:empty_proto",
  17. ],
  18. )
  19. go_proto_library(
  20. name = "user_go_proto",
  21. importpath = "github.com/abcd/user/proto",
  22. protos = [":user_proto"],
  23. )
  24. go_proto_library(
  25. name = "user_service_go_proto",
  26. compilers = ["@io_bazel_rules_go//proto:go_grpc"],
  27. importpath = "github.com/abcd/userservice/proto",
  28. protos = [
  29. ":user_proto",
  30. ":user_service_proto",
  31. ],
  32. )

这是我的user.proto文件:

  1. syntax = "proto3";
  2. import "google/protobuf/timestamp.proto";
  3. package some_package;
  4. option go_package = "github.com/abcd/user/proto";
  5. message User {
  6. ...
  7. }

这是我的user_service.proto文件:

  1. syntax = "proto3";
  2. import "proto/user.proto";
  3. import "google/protobuf/empty.proto";
  4. package some_package;
  5. option go_package = "github.com/abcd/userservice/proto";
  6. service UserService {
  7. ...
  8. }

然后我运行bazel build proto:all,期望go库能够成功构建,但实际上它们没有成功构建。

相反,我收到了一个错误,指出:

  1. Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
  2. compilepkg: missing strict dependencies:
  3. /private/var/tmp/_bazel_mohammednadeem/2fc3c21998c67019e72a996be754820f/sandbox/darwin-sandbox/581/execroot/__main__/bazel-out/darwin-fastbuild/bin/proto/user_service_go_proto_pb/github.com/abcd/userservice/proto/proto/user_service.pb.go: import of "github.com/abcd/user/proto"
  4. No dependencies were provided.
  5. Check that imports in Go sources match importpath attributes in deps.

我不太确定发生了什么。我的导入路径看起来是正确的,并且我在proto文件中设置了正确的option go_package

英文:

I have a repository where I'm using gazelle and proto build rules. My repository is structured like this:

  1. WORKSPACE
  2. \ proto
  3. - user.proto
  4. - user_service.proto
  5. - BUILD

This is the BUILD file I have inside my proto folder:

  1. load("@rules_proto//proto:defs.bzl", "proto_library")
  2. load("@rules_proto_grpc//go:defs.bzl", "go_proto_library")
  3. package(default_visibility = ["//visibility:public"])
  4. proto_library(
  5. name = "user_proto",
  6. srcs = ["user.proto"],
  7. deps = [
  8. "@com_google_protobuf//:timestamp_proto",
  9. ],
  10. )
  11. proto_library(
  12. name = "user_service_proto",
  13. srcs = ["user_service.proto"],
  14. deps = [
  15. ":user_proto",
  16. "@com_google_protobuf//:empty_proto",
  17. ],
  18. )
  19. go_proto_library(
  20. name = "user_go_proto",
  21. importpath = "github.com/abcd/user/proto",
  22. protos = [":user_proto"],
  23. )
  24. go_proto_library(
  25. name = "user_service_go_proto",
  26. compilers = ["@io_bazel_rules_go//proto:go_grpc"],
  27. importpath = "github.com/abcd/userservice/proto",
  28. protos = [
  29. ":user_proto",
  30. ":user_service_proto",
  31. ],
  32. )

This is my user.proto file:

  1. syntax = "proto3";
  2. import "google/protobuf/timestamp.proto";
  3. package some_package;
  4. option go_package = "github.com/abcd/user/proto";
  5. message User {
  6. ...
  7. }

And this is my user_service.proto file:

  1. syntax = "proto3";
  2. import "proto/user.proto";
  3. import "google/protobuf/empty.proto";
  4. package some_package;
  5. option go_package = "github.com/abcd/userservice/proto";
  6. service UserService {
  7. ...
  8. }

I then run bazel build proto:all and expect the go libraries to build successfully, but they do not.

Instead, I get an error stating that:

  1. Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
  2. compilepkg: missing strict dependencies:
  3. /private/var/tmp/_bazel_mohammednadeem/2fc3c21998c67019e72a996be754820f/sandbox/darwin-sandbox/581/execroot/__main__/bazel-out/darwin-fastbuild/bin/proto/user_service_go_proto_pb/github.com/abcd/userservice/proto/proto/user_service.pb.go: import of "github.com/abcd/user/proto"
  4. No dependencies were provided.
  5. Check that imports in Go sources match importpath attributes in deps.

I'm not really sure what's going on here. My importpaths look correct and I have the correct option go_package set in the proto file.

答案1

得分: 1

问题是我需要在user_service_go_proto目标中添加以下内容:

  1. deps = [" :user_go_proto "]
英文:

The issue was that I needed

  1. deps = [":user_go_proto"]

in the user_service_go_proto target.

huangapple
  • 本文由 发表于 2023年3月27日 05:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75850760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定