bazelbuild / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.18k stars 374 forks source link

Error with Go protobuf imports when using Gazelle #1488

Open mnadev opened 1 year ago

mnadev commented 1 year ago

What version of gazelle are you using?

v0.29.0

What version of rules_go are you using?

v0.37.0

What version of Bazel are you using?

bazel 6.1.1-homebrew

What operating system and processor architecture are you using?

Mac OS Monterey 12.4, 2.7 GHz Dual-Core Intel Core i5

What did you do?

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

WORKSPACE
\ proto
  - user.proto
  - user_service.proto
  - BUILD

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

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_proto_grpc//go:defs.bzl", "go_proto_library")

package(default_visibility = ["//visibility:public"])

proto_library(
    name = "user_proto",
    srcs = ["user.proto"],
    deps = [
        "@com_google_protobuf//:timestamp_proto",
    ],
)

proto_library(
    name = "user_service_proto",
    srcs = ["user_service.proto"],
    deps = [
        ":user_proto",
        "@com_google_protobuf//:empty_proto",
    ],
)

go_proto_library(
    name = "user_go_proto",
    importpath = "github.com/abcd/user/proto",
    protos = [":user_proto"],
)

go_proto_library(
    name = "user_service_go_proto",
    compilers = ["@io_bazel_rules_go//proto:go_grpc"],
    importpath = "github.com/abcd/userservice/proto",
    protos = [
        ":user_proto",
        ":user_service_proto",
    ],
)

This is my user.proto file:

syntax = "proto3";

import "google/protobuf/timestamp.proto";

package some_package;

option go_package = "github.com/abcd/user/proto";

message User {
 ...
}

And this is my user_service.proto file:

syntax = "proto3";

import "proto/user.proto";
import "google/protobuf/empty.proto";

package some_package;

option go_package = "github.com/abcd/userservice/proto";

service UserService {
...
}

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

What did you expect to see?

I expect the bazel build proto:all command to successfully build the go libraries.

What did you see instead?

Instead, I get an error stating that:

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
compilepkg: missing strict dependencies:
        /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"
No dependencies were provided.
Check that imports in Go sources match importpath attributes in deps.

I'm not really sure what's going on here. I've taken a look at issue#746, and it seems like what I have is correct but it's not correctly building.

mnadev commented 1 year ago

The issue was that I needed

   deps = [":user_go_proto"]

in the user_service_go_proto target. Should gazelle automatically update the dependencies for this?