Gazelle可以为外部Go项目运行吗?

I have a Bazel WORKSPACE from which I'd like to import an external Go git repository. I'm wondering if best-practise is to always have the Gazelle BUILD.bazel files or if my source WORKSPACE somehow can trigger Gazelle to generate the files so I don't need to check them in? The latter is this case makes sense if I'm not the owner of the upstream repo.

Is the external git repository a regular Go repo? Can "go get" build it? If so, the current best practice is to use go_repository in your WORKSPACE file. Something like this:

load("@io_bazel_rules_go//go:def.bzl", "go_repository")

go_repository(
    name = "org_golang_x_tools",
    importpath = "golang.org/x/tools",
    commit = "663269851cdddc898f963782f74ea574bcd5c814",
)

This will check out the repository and generate build files inside using Gazelle. It also handles Go import path redirection.

If the repository doesn't build cleanly this way, you can check it out, generate a single build file using Gazelle's -experimental_flat flag, modify it as needed, and use that with new_git_repository or new_http_archive. This workflow is pretty awkward right now, and we plan to simplify it a lot in the future.