bazel-contrib / 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.2k stars 380 forks source link

Inform gazelle of a known import #816

Closed nikunjy closed 4 years ago

nikunjy commented 4 years ago

What version of gazelle are you using?

 http_archive(
        name = "bazel_gazelle",
        sha256 = "bfd86b3cbe855d6c16c6fce60d76bd51f5c8dbc9cfcaef7a2bb5c1aafd0710e8",
        urls = [
            "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.0/bazel-gazelle-v0.21.0.tar.gz",
            "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.0/bazel-gazelle-v0.21.0.tar.gz",
        ],
    )

What version of rules_go are you using?

    http_archive(
        name = "io_bazel_rules_go",
        sha256 = "87f0fb9747854cb76a0a82430adccb6269f7d394237104a4523b51061c469171",
        urls = [
            "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.1/rules_go-v0.23.1.tar.gz",
            "https://github.com/bazelbuild/rules_go/releases/download/v0.23.1/rules_go-v0.23.1.tar.gz",
        ],
    )

What version of Bazel are you using?

Build label: 3.1.0-homebrew

Does this issue reproduce with the latest releases of all the above?

Yes

What operating system and processor architecture are you using?

MacOS

What did you do?

I want to import this file https://github.com/brexhq/protobuf-elixir/blob/brex-head/src/brex_elixirpb.proto
This repo is not using bazel but I want to import the proto in a DIFFERENT repository which is using Bazel.

I imported it using new_git_repository with a build_file that looks like this

new_git_repository(
    name = "protobuf_elixir",
    remote = "https://github.com/brexhq/protobuf-elixir",
    commit = "50b5d4677da94348ce4b3bad5c82fe18d08c0fc8",
    shallow_since = "2020-05-05",
    build_file = "@//:proto.BUILD",
)

contents of proto.BUILD

proto_library(
    name = "brex_elixirpb_proto",
    srcs = [
        "src/brex_elixirpb.proto",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "@com_google_protobuf//:descriptor_proto",
    ],
    strip_import_prefix = "src",
    import_prefix = "protobuf_elixir",
)

Now I can import that file in my other repository using import protobuf_elixir/brex_elixirpb.proto

I can manually write the BUILD.bazel to depend on that proto_target and it works.

What did you expect to see?

I want to inform gazelle to understand that import protobuf_elixir/brex_elixirpb.proto means add a dep of "@protobuf_elixir//:brex_elixirpb_proto"

I have looked at the knownImports and I don't know if that is the thing that I should extend using a flag or something else

What did you see instead?

Gazelle added a dep of "//protobuf_elixir:protobuf_elixir_proto" instead

jayconrod commented 4 years ago

Could you try adding this directive to the root build file in your repository? It should do what you want:

# gazelle:resolve proto protobuf_elixir/brex_elixirpb.proto @protobuf_elixir//:brex_elixirpb_proto

Directives is the reference on this and all other directives.

nikunjy commented 4 years ago

Thank you so much @jayconrod that works like a charm. I should have looked there.