From f1b4e7b27038bc01c982ca5c4dde5218a325f0d6 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 1 Jul 2022 22:01:10 +0200 Subject: [PATCH] Use repo-relative labels everywhere This makes it possible to load gazelle under any repo name, not necessarily bazel_gazelle. This opens up the possibility to call the gazelle Bazel module "gazelle". --- BUILD.bazel | 8 +- cmd/gazelle/BUILD.bazel | 2 +- def.bzl | 8 +- deps.bzl | 8 +- internal/BUILD.bazel | 10 +- internal/go_repository.bzl | 2 +- internal/go_repository_config.bzl | 10 +- internal/go_repository_tools.bzl | 8 +- internal/go_repository_tools_srcs.bzl | 216 ++++++++++++------------- internal/list_repository_tools_srcs.go | 6 +- language/go/def.bzl | 2 +- 11 files changed, 140 insertions(+), 140 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index f3e838cb4..8eec8b56c 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -102,10 +102,10 @@ bzl_library( srcs = ["deps.bzl"], visibility = ["//visibility:public"], deps = [ - "@bazel_gazelle//internal:go_repository", - "@bazel_gazelle//internal:go_repository_cache", - "@bazel_gazelle//internal:go_repository_config", - "@bazel_gazelle//internal:go_repository_tools", + "//internal:go_repository", + "//internal:go_repository_cache", + "//internal:go_repository_config", + "//internal:go_repository_tools", "@bazel_tools//tools/build_defs/repo:git.bzl", ], ) diff --git a/cmd/gazelle/BUILD.bazel b/cmd/gazelle/BUILD.bazel index 5d9004a14..1cbaea794 100644 --- a/cmd/gazelle/BUILD.bazel +++ b/cmd/gazelle/BUILD.bazel @@ -1,5 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") -load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle_binary") +load("//:def.bzl", "DEFAULT_LANGUAGES", "gazelle_binary") gazelle_binary( name = "gazelle", diff --git a/def.bzl b/def.bzl index d7dea85e5..1b771d157 100644 --- a/def.bzl +++ b/def.bzl @@ -41,8 +41,8 @@ gazelle_binary = _gazelle_binary gazelle_generation_test = _gazelle_generation_test DEFAULT_LANGUAGES = [ - "@bazel_gazelle//language/proto:go_default_library", - "@bazel_gazelle//language/go:go_default_library", + Label("//language/proto:go_default_library"), + Label("//language/go:go_default_library"), ] def _gazelle_runner_impl(ctx): @@ -94,7 +94,7 @@ _gazelle_runner = rule( implementation = _gazelle_runner_impl, attrs = { "gazelle": attr.label( - default = "@bazel_gazelle//cmd/gazelle", + default = "//cmd/gazelle", executable = True, cfg = "host", ), @@ -119,7 +119,7 @@ _gazelle_runner = rule( "extra_args": attr.string_list(), "data": attr.label_list(allow_files = True), "_template": attr.label( - default = "@bazel_gazelle//internal:gazelle.bash.in", + default = "//internal:gazelle.bash.in", allow_single_file = True, ), }, diff --git a/deps.bzl b/deps.bzl index 2d9b93a12..c4fe10d0a 100644 --- a/deps.bzl +++ b/deps.bzl @@ -17,19 +17,19 @@ load( "git_repository", ) load( - "@bazel_gazelle//internal:go_repository.bzl", + "//internal:go_repository.bzl", _go_repository = "go_repository", ) load( - "@bazel_gazelle//internal:go_repository_cache.bzl", + "//internal:go_repository_cache.bzl", "go_repository_cache", ) load( - "@bazel_gazelle//internal:go_repository_tools.bzl", + "//internal:go_repository_tools.bzl", "go_repository_tools", ) load( - "@bazel_gazelle//internal:go_repository_config.bzl", + "//internal:go_repository_config.bzl", "go_repository_config", ) diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index e92b030e8..9ed1675e2 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel @@ -10,7 +10,7 @@ go_bazel_test( "runner_test.go", ], rule_files = [ - "@bazel_gazelle//:all_files", + "//:all_files", "@io_bazel_rules_go//:all_files", ], deps = ["//testtools"], @@ -72,7 +72,7 @@ bzl_library( visibility = ["//:__subpackages__"], deps = [ ":common", - "@bazel_gazelle//internal:go_repository_cache", + "//internal:go_repository_cache", "@bazel_tools//tools/build_defs/repo:utils.bzl", ], ) @@ -90,7 +90,7 @@ bzl_library( visibility = ["//:__subpackages__"], deps = [ ":common", - "@bazel_gazelle//internal:go_repository_cache", + "//internal:go_repository_cache", ], ) @@ -100,8 +100,8 @@ bzl_library( visibility = ["//:__subpackages__"], deps = [ ":common", - "@bazel_gazelle//internal:go_repository_cache", - "@bazel_gazelle//internal:go_repository_tools_srcs", + "//internal:go_repository_cache", + "//internal:go_repository_tools_srcs", ], ) diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl index 4e1b638a9..fc1224c71 100644 --- a/internal/go_repository.bzl +++ b/internal/go_repository.bzl @@ -13,7 +13,7 @@ # limitations under the License. load("//internal:common.bzl", "env_execute", "executable_extension") -load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env") +load("//internal:go_repository_cache.bzl", "read_cache_env") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "use_netrc") _DOC = """ diff --git a/internal/go_repository_config.bzl b/internal/go_repository_config.bzl index 834e684d2..4ff59d706 100644 --- a/internal/go_repository_config.bzl +++ b/internal/go_repository_config.bzl @@ -13,7 +13,7 @@ # limitations under the License. load("//internal:common.bzl", "env_execute", "executable_extension") -load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env") +load("//internal:go_repository_cache.bzl", "read_cache_env") def _go_repository_config_impl(ctx): # Locate and resolve configuration files. Gazelle reads directives and @@ -47,10 +47,10 @@ def _go_repository_config_impl(ctx): else: ctx.file( - "WORKSPACE", - "", - False, - ) + "WORKSPACE", + "", + False, + ) # add an empty build file so Bazel recognizes the config ctx.file( diff --git a/internal/go_repository_tools.bzl b/internal/go_repository_tools.bzl index 68b31fe1e..9f2d98c78 100644 --- a/internal/go_repository_tools.bzl +++ b/internal/go_repository_tools.bzl @@ -13,8 +13,8 @@ # limitations under the License. load("//internal:common.bzl", "env_execute", "executable_extension") -load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env") -load("@bazel_gazelle//internal:go_repository_tools_srcs.bzl", "GO_REPOSITORY_TOOLS_SRCS") +load("//internal:go_repository_cache.bzl", "read_cache_env") +load("//internal:go_repository_tools_srcs.bzl", "GO_REPOSITORY_TOOLS_SRCS") _GO_REPOSITORY_TOOLS_BUILD_FILE = """ package(default_visibility = ["//visibility:public"]) @@ -44,7 +44,7 @@ def _go_repository_tools_impl(ctx): go_tool = env["GOROOT"] + "/bin/go" + extension ctx.symlink( - ctx.path(Label("@bazel_gazelle//:WORKSPACE")).dirname, + ctx.path(Label("//:WORKSPACE")).dirname, "src/github.com/bazelbuild/bazel-gazelle", ) @@ -125,7 +125,7 @@ go_repository_tools = repository_rule( default = GO_REPOSITORY_TOOLS_SRCS, ), "_list_repository_tools_srcs": attr.label( - default = "@bazel_gazelle//internal:list_repository_tools_srcs.go", + default = "//internal:list_repository_tools_srcs.go", ), }, environ = [ diff --git a/internal/go_repository_tools_srcs.bzl b/internal/go_repository_tools_srcs.bzl index 6fd16d2fc..b68f07ecc 100644 --- a/internal/go_repository_tools_srcs.bzl +++ b/internal/go_repository_tools_srcs.bzl @@ -1,112 +1,112 @@ # Code generated by list_repository_tools_srcs.go; DO NOT EDIT. # regenerate with `go run internal/list_repository_tools_srcs.go -dir $PWD -generate internal/go_repository_tools_srcs.bzl` GO_REPOSITORY_TOOLS_SRCS = [ - "@bazel_gazelle//:BUILD.bazel", - "@bazel_gazelle//cmd:BUILD.bazel", - "@bazel_gazelle//cmd/autogazelle:BUILD.bazel", - "@bazel_gazelle//cmd/autogazelle:autogazelle.go", - "@bazel_gazelle//cmd/autogazelle:client_unix.go", - "@bazel_gazelle//cmd/autogazelle:server_unix.go", - "@bazel_gazelle//cmd/fetch_repo:BUILD.bazel", - "@bazel_gazelle//cmd/fetch_repo:fetch_repo.go", - "@bazel_gazelle//cmd/fetch_repo:module.go", - "@bazel_gazelle//cmd/fetch_repo:vcs.go", - "@bazel_gazelle//cmd/gazelle:BUILD.bazel", - "@bazel_gazelle//cmd/gazelle:diff.go", - "@bazel_gazelle//cmd/gazelle:fix-update.go", - "@bazel_gazelle//cmd/gazelle:fix.go", - "@bazel_gazelle//cmd/gazelle:gazelle.go", - "@bazel_gazelle//cmd/gazelle:langs.go", - "@bazel_gazelle//cmd/gazelle:metaresolver.go", - "@bazel_gazelle//cmd/gazelle:print.go", - "@bazel_gazelle//cmd/gazelle:update-repos.go", - "@bazel_gazelle//cmd/generate_repo_config:BUILD.bazel", - "@bazel_gazelle//cmd/generate_repo_config:generate_repo_config.go", - "@bazel_gazelle//cmd/move_labels:BUILD.bazel", - "@bazel_gazelle//cmd/move_labels:move_labels.go", - "@bazel_gazelle//config:BUILD.bazel", - "@bazel_gazelle//config:config.go", - "@bazel_gazelle//config:constants.go", - "@bazel_gazelle//flag:BUILD.bazel", - "@bazel_gazelle//flag:flag.go", - "@bazel_gazelle//internal:BUILD.bazel", - "@bazel_gazelle//internal/gazellebinarytest:BUILD.bazel", - "@bazel_gazelle//internal/gazellebinarytest:xlang.go", - "@bazel_gazelle//internal/generationtest:BUILD.bazel", - "@bazel_gazelle//internal/language:BUILD.bazel", - "@bazel_gazelle//internal/language/test_filegroup:BUILD.bazel", - "@bazel_gazelle//internal/language/test_filegroup:lang.go", - "@bazel_gazelle//internal:list_repository_tools_srcs.go", - "@bazel_gazelle//internal/version:BUILD.bazel", - "@bazel_gazelle//internal/version:version.go", - "@bazel_gazelle//internal/wspace:BUILD.bazel", - "@bazel_gazelle//internal/wspace:finder.go", - "@bazel_gazelle//label:BUILD.bazel", - "@bazel_gazelle//label:label.go", - "@bazel_gazelle//language:BUILD.bazel", - "@bazel_gazelle//language/go:BUILD.bazel", - "@bazel_gazelle//language/go:config.go", - "@bazel_gazelle//language/go:constants.go", - "@bazel_gazelle//language/go:dep.go", - "@bazel_gazelle//language/go:embed.go", - "@bazel_gazelle//language/go:fileinfo.go", - "@bazel_gazelle//language/go:fix.go", - "@bazel_gazelle//language/go/gen_std_package_list:BUILD.bazel", - "@bazel_gazelle//language/go/gen_std_package_list:gen_std_package_list.go", - "@bazel_gazelle//language/go:generate.go", - "@bazel_gazelle//language/go:godep.go", - "@bazel_gazelle//language/go:kinds.go", - "@bazel_gazelle//language/go:lang.go", - "@bazel_gazelle//language/go:modules.go", - "@bazel_gazelle//language/go:package.go", - "@bazel_gazelle//language/go:resolve.go", - "@bazel_gazelle//language/go:std_package_list.go", - "@bazel_gazelle//language/go:update.go", - "@bazel_gazelle//language/go:utils.go", - "@bazel_gazelle//language/go:work.go", - "@bazel_gazelle//language:lang.go", - "@bazel_gazelle//language/proto:BUILD.bazel", - "@bazel_gazelle//language/proto:config.go", - "@bazel_gazelle//language/proto:constants.go", - "@bazel_gazelle//language/proto:fileinfo.go", - "@bazel_gazelle//language/proto:fix.go", - "@bazel_gazelle//language/proto/gen:BUILD.bazel", - "@bazel_gazelle//language/proto/gen:gen_known_imports.go", - "@bazel_gazelle//language/proto/gen:update_proto_csv.go", - "@bazel_gazelle//language/proto:generate.go", - "@bazel_gazelle//language/proto:kinds.go", - "@bazel_gazelle//language/proto:known_go_imports.go", - "@bazel_gazelle//language/proto:known_imports.go", - "@bazel_gazelle//language/proto:known_proto_imports.go", - "@bazel_gazelle//language/proto:lang.go", - "@bazel_gazelle//language/proto:package.go", - "@bazel_gazelle//language/proto:resolve.go", - "@bazel_gazelle//language:update.go", - "@bazel_gazelle//merger:BUILD.bazel", - "@bazel_gazelle//merger:fix.go", - "@bazel_gazelle//merger:merger.go", - "@bazel_gazelle//pathtools:BUILD.bazel", - "@bazel_gazelle//pathtools:path.go", - "@bazel_gazelle//repo:BUILD.bazel", - "@bazel_gazelle//repo:remote.go", - "@bazel_gazelle//repo:repo.go", - "@bazel_gazelle//resolve:BUILD.bazel", - "@bazel_gazelle//resolve:config.go", - "@bazel_gazelle//resolve:index.go", - "@bazel_gazelle//rule:BUILD.bazel", - "@bazel_gazelle//rule:directives.go", - "@bazel_gazelle//rule:expr.go", - "@bazel_gazelle//rule:merge.go", - "@bazel_gazelle//rule:platform.go", - "@bazel_gazelle//rule:platform_strings.go", - "@bazel_gazelle//rule:rule.go", - "@bazel_gazelle//rule:sort_labels.go", - "@bazel_gazelle//rule:types.go", - "@bazel_gazelle//rule:value.go", - "@bazel_gazelle//testtools:BUILD.bazel", - "@bazel_gazelle//testtools:config.go", - "@bazel_gazelle//testtools:files.go", - "@bazel_gazelle//walk:BUILD.bazel", - "@bazel_gazelle//walk:config.go", - "@bazel_gazelle//walk:walk.go", + Label("//BUILD.bazel"), + Label("//cmd:BUILD.bazel"), + Label("//cmd/autogazelle:BUILD.bazel"), + Label("//cmd/autogazelle:autogazelle.go"), + Label("//cmd/autogazelle:client_unix.go"), + Label("//cmd/autogazelle:server_unix.go"), + Label("//cmd/fetch_repo:BUILD.bazel"), + Label("//cmd/fetch_repo:fetch_repo.go"), + Label("//cmd/fetch_repo:module.go"), + Label("//cmd/fetch_repo:vcs.go"), + Label("//cmd/gazelle:BUILD.bazel"), + Label("//cmd/gazelle:diff.go"), + Label("//cmd/gazelle:fix-update.go"), + Label("//cmd/gazelle:fix.go"), + Label("//cmd/gazelle:gazelle.go"), + Label("//cmd/gazelle:langs.go"), + Label("//cmd/gazelle:metaresolver.go"), + Label("//cmd/gazelle:print.go"), + Label("//cmd/gazelle:update-repos.go"), + Label("//cmd/generate_repo_config:BUILD.bazel"), + Label("//cmd/generate_repo_config:generate_repo_config.go"), + Label("//cmd/move_labels:BUILD.bazel"), + Label("//cmd/move_labels:move_labels.go"), + Label("//config:BUILD.bazel"), + Label("//config:config.go"), + Label("//config:constants.go"), + Label("//flag:BUILD.bazel"), + Label("//flag:flag.go"), + Label("//internal:BUILD.bazel"), + Label("//internal/gazellebinarytest:BUILD.bazel"), + Label("//internal/gazellebinarytest:xlang.go"), + Label("//internal/generationtest:BUILD.bazel"), + Label("//internal/language:BUILD.bazel"), + Label("//internal/language/test_filegroup:BUILD.bazel"), + Label("//internal/language/test_filegroup:lang.go"), + Label("//internal:list_repository_tools_srcs.go"), + Label("//internal/version:BUILD.bazel"), + Label("//internal/version:version.go"), + Label("//internal/wspace:BUILD.bazel"), + Label("//internal/wspace:finder.go"), + Label("//label:BUILD.bazel"), + Label("//label:label.go"), + Label("//language:BUILD.bazel"), + Label("//language/go:BUILD.bazel"), + Label("//language/go:config.go"), + Label("//language/go:constants.go"), + Label("//language/go:dep.go"), + Label("//language/go:embed.go"), + Label("//language/go:fileinfo.go"), + Label("//language/go:fix.go"), + Label("//language/go/gen_std_package_list:BUILD.bazel"), + Label("//language/go/gen_std_package_list:gen_std_package_list.go"), + Label("//language/go:generate.go"), + Label("//language/go:godep.go"), + Label("//language/go:kinds.go"), + Label("//language/go:lang.go"), + Label("//language/go:modules.go"), + Label("//language/go:package.go"), + Label("//language/go:resolve.go"), + Label("//language/go:std_package_list.go"), + Label("//language/go:update.go"), + Label("//language/go:utils.go"), + Label("//language/go:work.go"), + Label("//language:lang.go"), + Label("//language/proto:BUILD.bazel"), + Label("//language/proto:config.go"), + Label("//language/proto:constants.go"), + Label("//language/proto:fileinfo.go"), + Label("//language/proto:fix.go"), + Label("//language/proto/gen:BUILD.bazel"), + Label("//language/proto/gen:gen_known_imports.go"), + Label("//language/proto/gen:update_proto_csv.go"), + Label("//language/proto:generate.go"), + Label("//language/proto:kinds.go"), + Label("//language/proto:known_go_imports.go"), + Label("//language/proto:known_imports.go"), + Label("//language/proto:known_proto_imports.go"), + Label("//language/proto:lang.go"), + Label("//language/proto:package.go"), + Label("//language/proto:resolve.go"), + Label("//language:update.go"), + Label("//merger:BUILD.bazel"), + Label("//merger:fix.go"), + Label("//merger:merger.go"), + Label("//pathtools:BUILD.bazel"), + Label("//pathtools:path.go"), + Label("//repo:BUILD.bazel"), + Label("//repo:remote.go"), + Label("//repo:repo.go"), + Label("//resolve:BUILD.bazel"), + Label("//resolve:config.go"), + Label("//resolve:index.go"), + Label("//rule:BUILD.bazel"), + Label("//rule:directives.go"), + Label("//rule:expr.go"), + Label("//rule:merge.go"), + Label("//rule:platform.go"), + Label("//rule:platform_strings.go"), + Label("//rule:rule.go"), + Label("//rule:sort_labels.go"), + Label("//rule:types.go"), + Label("//rule:value.go"), + Label("//testtools:BUILD.bazel"), + Label("//testtools:config.go"), + Label("//testtools:files.go"), + Label("//walk:BUILD.bazel"), + Label("//walk:config.go"), + Label("//walk:walk.go"), ] diff --git a/internal/list_repository_tools_srcs.go b/internal/list_repository_tools_srcs.go index b3dc25db9..3c6a74cd5 100644 --- a/internal/list_repository_tools_srcs.go +++ b/internal/list_repository_tools_srcs.go @@ -76,9 +76,9 @@ func main() { base == "BUILD.bazel" || base == "BUILD") { label := filepath.ToSlash(path) if i := strings.LastIndexByte(label, '/'); i >= 0 { - label = "@bazel_gazelle//" + label[:i] + ":" + label[i+1:] + label = fmt.Sprintf(`Label("//%s:%s")`, label[:i], label[i+1:]) } else { - label = "@bazel_gazelle//:" + label + label = fmt.Sprintf(`Label("//%s")`, label) } labels = append(labels, label) } @@ -93,7 +93,7 @@ func main() { fmt.Fprintln(buf, "# regenerate with `go run internal/list_repository_tools_srcs.go -dir $PWD -generate internal/go_repository_tools_srcs.bzl`") fmt.Fprintln(buf, "GO_REPOSITORY_TOOLS_SRCS = [") for _, label := range labels { - fmt.Fprintf(buf, "\t%q,\n", label) + fmt.Fprintf(buf, "\t%s,\n", label) } fmt.Fprintln(buf, "]") diff --git a/language/go/def.bzl b/language/go/def.bzl index 798609bb4..332d82734 100644 --- a/language/go/def.bzl +++ b/language/go/def.bzl @@ -18,7 +18,7 @@ std_package_list = rule( attrs = { "out": attr.output(mandatory = True), "_gen_std_package_list": attr.label( - default = "@bazel_gazelle//language/go/gen_std_package_list", + default = "//language/go/gen_std_package_list", cfg = "host", executable = True, ),