对于瞪羚,如何使用WORKSPACE中定义的go_repository?

In my WORKSPACE, I have defined the go_repostitory for importpath golang.org/x/net.

go_repository(
   name = "org_golang_x_net",
   commit = "5ccada7d0a7ba9aeb5d3aca8d3501b4c2a509fec",
   importpath = "golang.org/x/net",
)

But when I run gazelle, the dependency added to BUILD.Bazel is not @org_golang_x_net//context:go_default_library. Instead, it is the following: //golang.org/x/net/context:go_default_library

Why does gazelle ignore the defined go_repository? Is there a way for it to consider deps defined in WORKSPACE?

I'm guessing from your previous question this is because you're using an empty prefix.

Gazelle resolves Go imports to Bazel labels in several steps:

  1. If there is a go_library in your workspace with a matching importpath, Gazelle will use the name of that library.
  2. If the import path is below the top-level prefix for your repository, the import will be considered "local", so you'll get a label like //golang.org/x/net/content:go_default_library.
    • Since you're using an empty prefix at the top level, this catch will catch every unknown import.
    • This should be improved. Gazelle's resolver should be aware of prefixes defined in different directories (currently, those are only used to determine the importpath attribute on generated rules). I've filed bazelbuild/bazel-gazelle#101 to fix that.
  3. If the import is not considered local, Gazelle will generate a label for an external repository or for the vendor directory, depending on the external mode.

For now, you may want to add a directive to your top-level build file like:

# gazelle:prefix __do_not_match__

This will basically disable the second case, since no import will start with that. You'll still have an empty prefix in your src directory (set with # gazelle:prefix in src/BUILD.bazel), so your libraries will still have the correct importpath directives. You might want to set that to more specific subdirectories though.