Simplify label canonicalization
diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl
index b6f2537..32123b7 100644
--- a/internal/bzlmod/go_deps.bzl
+++ b/internal/bzlmod/go_deps.bzl
@@ -18,7 +18,7 @@
_GO_DEPS_HELPER_DEFS_BZL = """load("@bazel_gazelle//internal/bzlmod:go_helper.bzl", "go_helper")
def go(import_path):
- return go_helper(import_path, _GO_DEPS)
+ return go_helper(import_path, _GO_DEPS, lambda x: Label(x))
_GO_DEPS = {}
"""
@@ -59,19 +59,6 @@
def _noop(s):
pass
-def _to_canonical(repo_name):
- """Converts the apparent name of a repository defined by the go_deps
- extension to the canonical name of the repository, including the leading @.
-
- WARNING: This is a wild hack and should not be committed as is.
- """
-
- # Only repos that are use_repo-ed by gazelle itself can be canonicalized in
- # this way.
- go_deps_canonical = Label("@bazel_gazelle_go_repository_directives//foo").workspace_name
- base_canonical = go_deps_canonical[:go_deps_canonical.rfind("~") + 1]
- return "@" + base_canonical + repo_name
-
def _go_deps_impl(module_ctx):
module_resolutions = {}
root_versions = {}
@@ -177,7 +164,7 @@
_go_deps_helper(
name = "go_deps",
go_deps = {
- path: _to_canonical(module.repo_name)
+ path: module.repo_name
for path, module in module_resolutions.items()
},
)
diff --git a/internal/bzlmod/go_helper.bzl b/internal/bzlmod/go_helper.bzl
index c5fca84..275cb2f 100644
--- a/internal/bzlmod/go_helper.bzl
+++ b/internal/bzlmod/go_helper.bzl
@@ -18,14 +18,14 @@
name = unversioned_import_path[i + 1:]
return name.replace(".", "_")
-def go_helper(import_path, module_path_to_repo):
+def go_helper(import_path, module_path_to_repo, canonicalize):
split_pos = len(import_path)
for i in range(import_path.count("/") + 1):
module_path = import_path[:split_pos]
module_repo = module_path_to_repo.get(module_path, None)
if module_repo:
package_path = import_path[split_pos:].removeprefix("/")
- return "@{}//{}:{}".format(module_repo, package_path, _lib_name_from_import_path(import_path))
+ return canonicalize("@{}//{}:{}".format(module_repo, package_path, _lib_name_from_import_path(import_path)))
split_pos = module_path.rfind("/", 0, split_pos)
fail("""