From 29343aaa28a0726c0de83fb11d4a82edba419b64 Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Wed, 19 Aug 2020 17:30:34 -0400 Subject: [PATCH] language/go: merge new Go naming convention to master (#863) With this change, Gazelle supports multiple naming conventions for Go targets. In new projects, new targets will be named after their import paths by default. Existing projects can continue to use their current naming convention (which will be detected automatically in most cases), or they may migrate by adding this directive to their root build file, then running Gazelle. # gazelle:go_naming_convention import go_repository uses the import_alias convention by default, which generates targets using the import convention and aliases using the go_default_library convention. Thanks to @tomlu for implementing nearly all of this! Fixes #5 --- BUILD.bazel | 7 +- README.rst | 35 ++ WORKSPACE | 10 + cmd/autogazelle/BUILD.bazel | 24 +- cmd/fetch_repo/BUILD.bazel | 8 +- cmd/gazelle/BUILD.bazel | 38 +- cmd/gazelle/diff_test.go | 6 +- cmd/gazelle/fix_test.go | 12 +- cmd/gazelle/integration_test.go | 120 +++-- cmd/generate_repo_config/BUILD.bazel | 14 +- cmd/move_labels/BUILD.bazel | 16 +- config/BUILD.bazel | 18 +- deps.bzl | 9 +- flag/BUILD.bazel | 8 +- internal/BUILD.bazel | 2 +- internal/gazelle_binary.bzl | 7 +- internal/gazellebinarytest/BUILD.bazel | 30 +- .../gazellebinarytest/gazellebinary_test.go | 2 +- internal/go_repository.bzl | 10 + internal/go_repository_test.go | 1 + internal/language/test_filegroup/BUILD.bazel | 20 +- internal/version/BUILD.bazel | 12 +- internal/wspace/BUILD.bazel | 12 +- label/BUILD.bazel | 14 +- language/BUILD.bazel | 16 +- language/go/BUILD.bazel | 58 ++- language/go/config.go | 211 +++++++- language/go/config_test.go | 4 + language/go/fix.go | 117 +++++ language/go/fix_test.go | 460 +++++++++++++++++- language/go/gen_std_package_list/BUILD.bazel | 4 +- language/go/generate.go | 47 +- language/go/generate_test.go | 15 +- language/go/kinds.go | 4 + language/go/package.go | 65 ++- language/go/resolve.go | 44 +- language/go/resolve_test.go | 102 +++- language/go/testdata/allcgolib/BUILD.want | 6 +- language/go/testdata/bin/BUILD.want | 4 +- .../go/testdata/bin_with_tests/BUILD.want | 8 +- language/go/testdata/cgolib/BUILD.want | 6 +- .../cgolib_with_build_tags/BUILD.want | 6 +- .../go/testdata/default_visibility/BUILD.want | 6 +- .../default_visibility/cmd/BUILD.want | 4 +- language/go/testdata/filter_pbgo/BUILD.want | 2 +- .../go/testdata/gen_and_exclude/BUILD.want | 2 +- language/go/testdata/importmap/BUILD.want | 6 +- language/go/testdata/lib/BUILD.want | 6 +- .../go/testdata/lib/internal/deep/BUILD.want | 2 +- .../lib/internal/go_visibility/BUILD.want | 2 +- .../testdata/lib/relativeimporter/BUILD.want | 2 +- .../go/testdata/main_test_only/BUILD.want | 2 +- .../naming_convention/import/bin/BUILD.old | 1 + .../naming_convention/import/bin/BUILD.want | 29 ++ .../naming_convention/import/bin/bin_test.go | 26 + .../naming_convention/import/bin/lib.go | 21 + .../naming_convention/import/bin/main.go | 25 + .../naming_convention/import/lib/BUILD.old | 1 + .../naming_convention/import/lib/BUILD.want | 16 + .../naming_convention/import/lib/lib.go | 21 + .../naming_convention/import/lib/lib_test.go | 26 + .../import_alias/bin/BUILD.old | 1 + .../import_alias/bin/BUILD.want | 29 ++ .../import_alias/bin/bin_test.go | 26 + .../naming_convention/import_alias/bin/lib.go | 21 + .../import_alias/bin/main.go | 25 + .../import_alias/lib/BUILD.old | 1 + .../import_alias/lib/BUILD.want | 22 + .../naming_convention/import_alias/lib/lib.go | 23 + .../import_alias/lib/lib_test.go | 26 + .../naming_convention/versions/v2/BUILD.old | 1 + .../naming_convention/versions/v2/BUILD.want | 16 + .../naming_convention/versions/v2/lib.go | 21 + .../naming_convention/versions/v2/lib_test.go | 26 + language/go/testdata/platforms/BUILD.want | 6 +- .../proto_package_mode_extras/BUILD.want | 6 +- language/go/testdata/protos/BUILD.want | 2 +- .../protos_explicit_default/BUILD.want | 2 +- language/go/testdata/protos_gogo/BUILD.want | 2 +- .../protos_gogo_subdir_reset/BUILD.want | 2 +- .../protos_gogo_subdir_reset/sub/BUILD.want | 2 +- language/go/testdata/service/BUILD.want | 2 +- language/go/testdata/service_gogo/BUILD.want | 2 +- .../service_gogo_subdir_reset/BUILD.want | 2 +- .../service_gogo_subdir_reset/sub/BUILD.want | 2 +- .../testdata/tests_import_testdata/BUILD.want | 2 +- .../testdata/tests_with_testdata/BUILD.want | 2 +- language/proto/BUILD.bazel | 48 +- language/proto/gen/BUILD.bazel | 6 +- merger/BUILD.bazel | 22 +- pathtools/BUILD.bazel | 12 +- repo/BUILD.bazel | 24 +- repository.rst | 8 + resolve/BUILD.bazel | 16 +- rule/BUILD.bazel | 14 +- testtools/BUILD.bazel | 14 +- walk/BUILD.bazel | 30 +- 97 files changed, 1937 insertions(+), 341 deletions(-) create mode 100644 language/go/testdata/naming_convention/import/bin/BUILD.old create mode 100644 language/go/testdata/naming_convention/import/bin/BUILD.want create mode 100644 language/go/testdata/naming_convention/import/bin/bin_test.go create mode 100644 language/go/testdata/naming_convention/import/bin/lib.go create mode 100644 language/go/testdata/naming_convention/import/bin/main.go create mode 100644 language/go/testdata/naming_convention/import/lib/BUILD.old create mode 100644 language/go/testdata/naming_convention/import/lib/BUILD.want create mode 100644 language/go/testdata/naming_convention/import/lib/lib.go create mode 100644 language/go/testdata/naming_convention/import/lib/lib_test.go create mode 100644 language/go/testdata/naming_convention/import_alias/bin/BUILD.old create mode 100644 language/go/testdata/naming_convention/import_alias/bin/BUILD.want create mode 100644 language/go/testdata/naming_convention/import_alias/bin/bin_test.go create mode 100644 language/go/testdata/naming_convention/import_alias/bin/lib.go create mode 100644 language/go/testdata/naming_convention/import_alias/bin/main.go create mode 100644 language/go/testdata/naming_convention/import_alias/lib/BUILD.old create mode 100644 language/go/testdata/naming_convention/import_alias/lib/BUILD.want create mode 100644 language/go/testdata/naming_convention/import_alias/lib/lib.go create mode 100644 language/go/testdata/naming_convention/import_alias/lib/lib_test.go create mode 100644 language/go/testdata/naming_convention/versions/v2/BUILD.old create mode 100644 language/go/testdata/naming_convention/versions/v2/BUILD.want create mode 100644 language/go/testdata/naming_convention/versions/v2/lib.go create mode 100644 language/go/testdata/naming_convention/versions/v2/lib_test.go diff --git a/BUILD.bazel b/BUILD.bazel index dd8f79e11..3a5df0507 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -6,6 +6,7 @@ load("//:def.bzl", "gazelle", "gazelle_binary") # gazelle:exclude third_party # gazelle:exclude .bazelci # gazelle:exclude .github +# gazelle:go_naming_convention import_alias gazelle( name = "gazelle", command = "fix", @@ -15,9 +16,9 @@ gazelle( gazelle_binary( name = "gazelle_local", languages = [ - "//language/proto:go_default_library", - "//language/go:go_default_library", - "//internal/language/test_filegroup:go_default_library", + "//language/proto", + "//language/go", + "//internal/language/test_filegroup", ], ) diff --git a/README.rst b/README.rst index 972a54cd2..f7075b10a 100644 --- a/README.rst +++ b/README.rst @@ -332,6 +332,17 @@ The following flags are accepted: | See `Predefined plugins`_ for available options; commonly used options include | | ``@io_bazel_rules_go//proto:gofast_grpc`` and ``@io_bazel_rules_go//proto:gogofaster_grpc``. | +--------------------------------------------------------------+----------------------------------------+ +| :flag:`-go_naming_convention` | | ++--------------------------------------------------------------+----------------------------------------+ +| Controls the names of generated Go targets. Equivalent to the | +| ``# gazelle:go_naming_convention`` directive. | ++--------------------------------------------------------------+----------------------------------------+ +| :flag:`-go_naming_convention_extern` | | ++--------------------------------------------------------------+----------------------------------------+ +| Controls the default naming convention used when resolving libraries in | +| external repositories with unknown naming conventions. Equivalent to the | +| ``# gazelle:go_naming_convention_extern`` directive. | ++--------------------------------------------------------------+----------------------------------------+ | :flag:`-go_prefix example.com/repo` | | +--------------------------------------------------------------+----------------------------------------+ | A prefix of import paths for libraries in the repository that corresponds to | @@ -576,6 +587,30 @@ The following directives are recognized: | ``@io_bazel_rules_go//proto:gofast_grpc`` and | | ``@io_bazel_rules_go//proto:gogofaster_grpc``. | +---------------------------------------------------+----------------------------------------+ +| :direc:`# gazelle:go_naming_convention` | n/a | ++---------------------------------------------------+----------------------------------------+ +| Controls the names of generated Go targets. By default, library targets are named | +| ``go_default_library`` and test targets ``go_default_test``. | +| | +| Valid values are: | +| | +| * ``go_default_library``: Library targets are named ``go_default_library``, test targets | +| are named ``go_default_test``. | +| * ``import``: Library and test targets are named after the last segment of their import | +| path. | +| For example, ``example.repo/foo`` is named ``foo``, and the test target is ``foo_test``. | +| Major version suffixes like ``/v2`` are dropped. | +| For a main package with a binary ``foobin``, the names are instead ``foobin_lib`` and | +| ``foobin_test``. | +| * ``import_alias``: Same as ``import``, but an ``alias`` target is generated named | +| ``go_default_library`` to ensure backwards compatibility. | ++---------------------------------------------------+----------------------------------------+ +| :direc:`# gazelle:go_naming_convention_extern` | n/a | ++---------------------------------------------------+----------------------------------------+ +| Controls the default naming convention used when resolving libraries in | +| external repositories with unknown naming conventions. Accepts the same values | +| as ``go_naming_convention``. | ++---------------------------------------------------+----------------------------------------+ | :direc:`# gazelle:go_proto_compilers` | ``@io_bazel_rules_go//proto:go_proto`` | +---------------------------------------------------+----------------------------------------+ | The protocol buffers compiler(s) to use for building go bindings. | diff --git a/WORKSPACE b/WORKSPACE index f55641822..e81a508fd 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -22,3 +22,13 @@ go_register_toolchains(nogo = "@bazel_gazelle//:nogo") load("//:deps.bzl", "gazelle_dependencies") gazelle_dependencies() + +# gazelle:repository go_repository name=org_golang_x_sync importpath=golang.org/x/sync +# gazelle:repository go_repository name=org_golang_x_sys importpath=golang.org/x/sys +# gazelle:repository go_repository name=org_golang_x_xerrors importpath=golang.org/x/xerrors +# gazelle:repository go_repository name=com_github_bazelbuild_buildtools importpath=github.com/bazelbuild/buildtools build_naming_convention=go_default_library +# gazelle:repository go_repository name=com_github_fsnotify_fsnotify importpath=github.com/fsnotify/fsnotify +# gazelle:repository go_repository name=com_github_pelletier_go_toml importpath=github.com/pelletier/go-toml +# gazelle:repository go_repository name=com_github_pmezard_go_difflib importpath=github.com/pmezard/go-difflib +# gazelle:repository go_repository name=com_github_bmatcuk_doublestar importpath=github.com/bmatcuk/doublestar +# gazelle:repository go_repository name=com_github_google_go_cmp importpath=github.com/google/go-cmp diff --git a/cmd/autogazelle/BUILD.bazel b/cmd/autogazelle/BUILD.bazel index bdf1e2bbc..6eefb4ae7 100644 --- a/cmd/autogazelle/BUILD.bazel +++ b/cmd/autogazelle/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_library( - name = "go_default_library", + name = "autogazelle_lib", srcs = [ "autogazelle.go", "client_unix.go", @@ -11,34 +11,34 @@ go_library( visibility = ["//visibility:private"], deps = select({ "@io_bazel_rules_go//go/platform:android": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:darwin": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:dragonfly": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:freebsd": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:ios": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:linux": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:nacl": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:netbsd": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:openbsd": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "@io_bazel_rules_go//go/platform:solaris": [ - "@com_github_fsnotify_fsnotify//:go_default_library", + "@com_github_fsnotify_fsnotify//:fsnotify", ], "//conditions:default": [], }), @@ -46,7 +46,7 @@ go_library( go_binary( name = "autogazelle", - embed = [":go_default_library"], + embed = [":autogazelle_lib"], visibility = ["//visibility:public"], ) diff --git a/cmd/fetch_repo/BUILD.bazel b/cmd/fetch_repo/BUILD.bazel index 8830f4be8..87803c4db 100644 --- a/cmd/fetch_repo/BUILD.bazel +++ b/cmd/fetch_repo/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") go_library( - name = "go_default_library", + name = "fetch_repo_lib", srcs = [ "fetch_repo.go", "module.go", @@ -14,14 +14,14 @@ go_library( go_binary( name = "fetch_repo", - embed = [":go_default_library"], + embed = [":fetch_repo_lib"], visibility = ["//visibility:public"], ) go_test( - name = "go_default_test", + name = "fetch_repo_test", srcs = ["fetch_repo_test.go"], - embed = [":go_default_library"], + embed = [":fetch_repo_lib"], deps = ["@org_golang_x_tools//go/vcs:go_default_library"], ) diff --git a/cmd/gazelle/BUILD.bazel b/cmd/gazelle/BUILD.bazel index 3270492d6..b15e6ad08 100644 --- a/cmd/gazelle/BUILD.bazel +++ b/cmd/gazelle/BUILD.bazel @@ -23,7 +23,7 @@ gazelle_binary( ) go_library( - name = "go_default_library", + name = "gazelle_lib", # keep srcs = [ "diff.go", @@ -38,25 +38,25 @@ go_library( tags = ["manual"], visibility = ["//visibility:public"], deps = [ - "//config:go_default_library", - "//flag:go_default_library", - "//internal/wspace:go_default_library", - "//label:go_default_library", - "//language:go_default_library", - "//language/go:go_default_library", - "//language/proto:go_default_library", - "//merger:go_default_library", - "//repo:go_default_library", - "//resolve:go_default_library", - "//rule:go_default_library", - "//walk:go_default_library", + "//config", + "//flag", + "//internal/wspace", + "//label", + "//language", + "//language/go", + "//language/proto", + "//merger", + "//repo", + "//resolve", + "//rule", + "//walk", "@com_github_bazelbuild_buildtools//build:go_default_library", - "@com_github_pmezard_go_difflib//difflib:go_default_library", + "@com_github_pmezard_go_difflib//difflib", ], ) go_test( - name = "go_default_test", + name = "gazelle_test", size = "small", srcs = [ "diff_test.go", @@ -66,11 +66,11 @@ go_test( ], args = ["-go_sdk=go_sdk"], data = ["@go_sdk//:files"], - embed = [":go_default_library"], + embed = [":gazelle_lib"], deps = [ - "//config:go_default_library", - "//internal/wspace:go_default_library", - "//testtools:go_default_library", + "//config", + "//internal/wspace", + "//testtools", "@io_bazel_rules_go//go/tools/bazel:go_default_library", ], ) diff --git a/cmd/gazelle/diff_test.go b/cmd/gazelle/diff_test.go index 6a6b58886..ed5a755ea 100644 --- a/cmd/gazelle/diff_test.go +++ b/cmd/gazelle/diff_test.go @@ -55,7 +55,7 @@ func TestDiffExisting(t *testing.T) { # gazelle:prefix example.com/hello +go_library( -+ name = "go_default_library", ++ name = "hello", + srcs = ["hello.go"], + importpath = "example.com/hello", + visibility = ["//visibility:public"], @@ -91,7 +91,7 @@ func TestDiffNew(t *testing.T) { +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( -+ name = "go_default_library", ++ name = "hello", + srcs = ["hello.go"], + importpath = "example.com/hello", + visibility = ["//visibility:public"], @@ -138,7 +138,7 @@ func TestDiffReadWriteDir(t *testing.T) { # gazelle:prefix example.com/hello + +go_library( -+ name = "go_default_library", ++ name = "hello", + srcs = ["hello.go"], + importpath = "example.com/hello", + visibility = ["//visibility:public"], diff --git a/cmd/gazelle/fix_test.go b/cmd/gazelle/fix_test.go index 10482dda2..ecd7e2371 100644 --- a/cmd/gazelle/fix_test.go +++ b/cmd/gazelle/fix_test.go @@ -257,13 +257,13 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_binary( name = "hello", - embed = [":go_default_library"], + embed = [":repo_lib"], pure = "on", visibility = ["//visibility:public"], ) go_library( - name = "go_default_library", + name = "repo_lib", srcs = ["hello.go"], importpath = "example.com/repo", visibility = ["//visibility:private"], @@ -291,7 +291,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") # src build file go_library( - name = "go_default_library", + name = "repo_lib", srcs = ["hello.go"], importpath = "example.com/repo", visibility = ["//visibility:private"], @@ -299,7 +299,7 @@ go_library( go_binary( name = "repo", - embed = [":go_default_library"], + embed = [":repo_lib"], visibility = ["//visibility:public"], ) `, @@ -324,13 +324,13 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_binary( name = "hello", - embed = [":go_default_library"], + embed = [":repo_lib"], pure = "on", visibility = ["//visibility:public"], ) go_library( - name = "go_default_library", + name = "repo_lib", srcs = ["hello.go"], importpath = "example.com/repo", visibility = ["//visibility:private"], diff --git a/cmd/gazelle/integration_test.go b/cmd/gazelle/integration_test.go index a24587c8c..074907ad2 100644 --- a/cmd/gazelle/integration_test.go +++ b/cmd/gazelle/integration_test.go @@ -524,11 +524,11 @@ import _ "golang.org/x/baz" Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "foo", srcs = ["a.go"], importpath = "example.com/foo", visibility = ["//visibility:public"], - deps = ["//vendor/golang.org/x/bar:go_default_library"], + deps = ["//vendor/golang.org/x/bar"], ) `, }, { @@ -536,12 +536,12 @@ go_library( Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "bar", srcs = ["bar.go"], importmap = "example.com/foo/vendor/golang.org/x/bar", importpath = "golang.org/x/bar", visibility = ["//visibility:public"], - deps = ["//vendor/golang.org/x/baz:go_default_library"], + deps = ["//vendor/golang.org/x/baz"], ) `, }, @@ -898,11 +898,11 @@ import ( load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "bar", srcs = ["bar.go"], importpath = "bar", visibility = ["//visibility:public"], - deps = ["//foo:go_default_library"], + deps = ["//foo"], ) `, }}) @@ -1007,7 +1007,7 @@ import _ "example.com/foo" load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "foo", srcs = ["foo.go"], importmap = "example.com/repo/sub/vendor/example.com/foo", importpath = "example.com/foo", @@ -1020,11 +1020,11 @@ go_library( load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "bar", srcs = ["bar.go"], importpath = "example.com/repo/sub/bar", visibility = ["//visibility:public"], - deps = ["//sub/vendor/example.com/foo:go_default_library"], + deps = ["//sub/vendor/example.com/foo"], ) `, }, @@ -1140,11 +1140,11 @@ import _ "example.com/bar" load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "foo", srcs = ["foo.go"], importpath = "example.com/foo", visibility = ["//visibility:public"], - deps = ["@custom_repo//:go_default_library"], + deps = ["@custom_repo//:bar"], ) `, }, @@ -1179,6 +1179,7 @@ import _ "example.com/bar" extDir := filepath.Join(dir, "ext") args := []string{ "-go_prefix=example.com/foo", + "-go_naming_convention=import_alias", "-mode=fix", "-repo_root=" + extDir, "-repo_config=" + filepath.Join(dir, "main", "WORKSPACE"), @@ -1194,11 +1195,17 @@ import _ "example.com/bar" load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "foo", srcs = ["foo.go"], importpath = "example.com/foo", visibility = ["//visibility:public"], - deps = ["@custom_repo//:go_default_library"], + deps = ["@custom_repo//:bar"], +) + +alias( + name = "go_default_library", + actual = ":foo", + visibility = ["//visibility:public"], ) `, }, @@ -2286,7 +2293,7 @@ import _ "example.com/bar" load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "bar", srcs = ["bar.go"], importpath = "example.com/bar", visibility = ["//visibility:public"], @@ -2312,11 +2319,11 @@ go_library( load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "foo", srcs = ["foo.go"], importpath = "example.com/repo/foo", visibility = ["//visibility:public"], - deps = ["//vendor/example.com/bar:go_default_library"], + deps = ["//vendor/example.com/bar"], ) `, }}) @@ -2351,7 +2358,7 @@ import ( # this should be ignored because -index=false go_library( - name = "go_default_library", + name = "baz", srcs = ["baz.go"], importpath = "example.com/dep/baz", visibility = ["//visibility:public"], @@ -2382,11 +2389,11 @@ go_library( Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "foo", srcs = ["foo.go"], importpath = "example.com/repo/foo", visibility = ["//visibility:public"], - deps = ["//vendor/example.com/dep/baz:go_default_library"], + deps = ["//vendor/example.com/dep/baz"], ) `, }, @@ -2434,13 +2441,13 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") # gazelle:prefix example.com/sub go_library( - name = "go_default_library", + name = "sub", srcs = ["sub.go"], importpath = "example.com/sub", visibility = ["//visibility:public"], deps = [ - "//sub/missing:go_default_library", - "//vendor/example.com/external:go_default_library", + "//sub/missing", + "//vendor/example.com/external", ], ) `, @@ -2604,8 +2611,11 @@ func TestMapKind(t *testing.T) { { Path: "WORKSPACE", }, { - Path: "BUILD.bazel", - Content: "# gazelle:prefix example.com/mapkind", + Path: "BUILD.bazel", + Content: ` +# gazelle:prefix example.com/mapkind +# gazelle:go_naming_convention go_default_library +`, }, { Path: "root_lib.go", Content: `package mapkind`, @@ -2694,6 +2704,7 @@ go_library( load("@io_bazel_rules_go//go:def.bzl", "go_library") # gazelle:prefix example.com/mapkind +# gazelle:go_naming_convention go_default_library go_library( name = "go_default_library", @@ -2860,7 +2871,7 @@ func TestMinimalModuleCompatibilityAliases(t *testing.T) { load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "foo", srcs = ["foo.go"], importpath = "example.com/foo/v2", importpath_aliases = ["example.com/foo"], @@ -2873,7 +2884,7 @@ go_library( load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "bar", srcs = ["bar.go"], importpath = "example.com/foo/v2/bar", importpath_aliases = ["example.com/foo/bar"], @@ -2920,7 +2931,7 @@ go_repository( load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "version", srcs = ["version.go"], importpath = "example.com/m/internal/version", visibility = [ @@ -2930,9 +2941,9 @@ go_library( ) go_test( - name = "go_default_test", + name = "version_test", srcs = ["version_test.go"], - embed = [":go_default_library"], + embed = [":version"], ) `, }}) @@ -3141,7 +3152,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "foo", embed = [":foo_go_proto"], importpath = "example.com/foo", visibility = ["//visibility:public"], @@ -3352,7 +3363,7 @@ proto_library( ) go_library( - name = "go_default_library", + name = "proto", srcs = ["foo.pb.go"], importpath = "example.com/proto", visibility = ["//visibility:public"], @@ -3361,6 +3372,53 @@ go_library( }) } +func TestGoMainLibraryRemoved(t *testing.T) { + files := []testtools.FileSpec{ + { + Path: "WORKSPACE", + }, + { + Path: "BUILD.bazel", + Content: ` +# gazelle:prefix example.com +# gazelle:go_naming_convention import +`, + }, + { + Path: "cmd/foo/BUILD.bazel", + Content: `load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + +go_library( + name = "foo_lib", + srcs = ["foo.go"], + importpath = "example.com/cmd/foo", + visibility = ["//visibility:private"], +) + +go_binary( + name = "foo", + embed = [":foo_lib"], + visibility = ["//visibility:public"], +) +`, + }, + } + dir, cleanup := testtools.CreateFiles(t, files) + defer cleanup() + + args := []string{"update"} + if err := runGazelle(dir, args); err != nil { + t.Fatal(err) + } + + testtools.CheckFiles(t, dir, []testtools.FileSpec{ + { + Path: "cmd/foo/BUILD.bazel", + Content: "", + }, + }) +} + func TestUpdateReposOldBoilerplateNewRepo(t *testing.T) { files := []testtools.FileSpec{ { diff --git a/cmd/generate_repo_config/BUILD.bazel b/cmd/generate_repo_config/BUILD.bazel index 93f8d0c37..e6fe0d380 100644 --- a/cmd/generate_repo_config/BUILD.bazel +++ b/cmd/generate_repo_config/BUILD.bazel @@ -1,27 +1,27 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") go_library( - name = "go_default_library", + name = "generate_repo_config_lib", srcs = ["generate_repo_config.go"], importpath = "github.com/bazelbuild/bazel-gazelle/cmd/generate_repo_config", visibility = ["//visibility:private"], deps = [ - "//repo:go_default_library", - "//rule:go_default_library", + "//repo", + "//rule", ], ) go_binary( name = "generate_repo_config", - embed = [":go_default_library"], + embed = [":generate_repo_config_lib"], visibility = ["//visibility:public"], ) go_test( - name = "go_default_test", + name = "generate_repo_config_test", srcs = ["generate_repo_config_test.go"], - embed = [":go_default_library"], - deps = ["//testtools:go_default_library"], + embed = [":generate_repo_config_lib"], + deps = ["//testtools"], ) filegroup( diff --git a/cmd/move_labels/BUILD.bazel b/cmd/move_labels/BUILD.bazel index e424a42e6..120f12486 100644 --- a/cmd/move_labels/BUILD.bazel +++ b/cmd/move_labels/BUILD.bazel @@ -1,29 +1,29 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") go_library( - name = "go_default_library", + name = "move_labels_lib", srcs = ["move_labels.go"], importpath = "github.com/bazelbuild/bazel-gazelle/cmd/move_labels", visibility = ["//visibility:private"], deps = [ - "//internal/wspace:go_default_library", - "//label:go_default_library", - "//pathtools:go_default_library", + "//internal/wspace", + "//label", + "//pathtools", "@com_github_bazelbuild_buildtools//build:go_default_library", ], ) go_binary( name = "move_labels", - embed = [":go_default_library"], + embed = [":move_labels_lib"], visibility = ["//visibility:public"], ) go_test( - name = "go_default_test", + name = "move_labels_test", srcs = ["move_labels_test.go"], - embed = [":go_default_library"], - deps = ["//testtools:go_default_library"], + embed = [":move_labels_lib"], + deps = ["//testtools"], ) filegroup( diff --git a/config/BUILD.bazel b/config/BUILD.bazel index f9b404a04..cc301bf3f 100644 --- a/config/BUILD.bazel +++ b/config/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "config", srcs = [ "config.go", "constants.go", @@ -9,16 +9,16 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/config", visibility = ["//visibility:public"], deps = [ - "//internal/wspace:go_default_library", - "//rule:go_default_library", + "//internal/wspace", + "//rule", ], ) go_test( - name = "go_default_test", + name = "config_test", srcs = ["config_test.go"], - embed = [":go_default_library"], - deps = ["//rule:go_default_library"], + embed = [":config"], + deps = ["//rule"], ) filegroup( @@ -32,3 +32,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":config", + visibility = ["//visibility:public"], +) diff --git a/deps.bzl b/deps.bzl index c32f78e3f..41b3fdb1c 100644 --- a/deps.bzl +++ b/deps.bzl @@ -28,13 +28,6 @@ load( "@bazel_gazelle//internal:go_repository_config.bzl", "go_repository_config", ) -load( - "@bazel_gazelle//internal:overlay_repository.bzl", - # Load overlay git_repository and http_archive in order to re-export. - # These may be removed at some point in the future. - "git_repository", - "http_archive", -) load( "@bazel_tools//tools/build_defs/repo:git.bzl", _tools_git_repository = "git_repository", @@ -152,7 +145,7 @@ def gazelle_dependencies( _maybe( go_repository, - name = "com_github_google_go_cmp", + name = "org_golang_x_xerrors", importpath = "golang.org/x/xerrors", sum = "h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=", version = "v0.0.0-20191204190536-9bdfabe68543", diff --git a/flag/BUILD.bazel b/flag/BUILD.bazel index fa552f6df..6820783c6 100644 --- a/flag/BUILD.bazel +++ b/flag/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "flag", srcs = ["flag.go"], importpath = "github.com/bazelbuild/bazel-gazelle/flag", visibility = ["//visibility:public"], @@ -16,3 +16,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":flag", + visibility = ["//visibility:public"], +) diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index 3481f4f68..5be959e39 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel @@ -4,11 +4,11 @@ load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test") go_bazel_test( name = "go_repository_test", srcs = ["go_repository_test.go"], - deps = ["//testtools:go_default_library"], rule_files = [ "@bazel_gazelle//:all_files", "@io_bazel_rules_go//:all_files", ], + deps = ["//testtools"], ) # TODO(jayconrod): test fetch_repo error cases. diff --git a/internal/gazelle_binary.bzl b/internal/gazelle_binary.bzl index 15630972c..5bbe23647 100644 --- a/internal/gazelle_binary.bzl +++ b/internal/gazelle_binary.bzl @@ -17,11 +17,6 @@ load( "GoArchive", "go_context", ) -load( - "@io_bazel_rules_go//go/platform:list.bzl", - "GOARCH", - "GOOS", -) load( "@io_bazel_rules_go//go/private:rules/transition.bzl", "go_transition_rule", @@ -95,7 +90,7 @@ _gazelle_binary_kwargs = { ), "_go_context_data": attr.label(default = "@io_bazel_rules_go//:go_context_data"), "_srcs": attr.label( - default = "//cmd/gazelle:go_default_library", + default = "//cmd/gazelle:gazelle_lib", ), }, "executable": True, diff --git a/internal/gazellebinarytest/BUILD.bazel b/internal/gazellebinarytest/BUILD.bazel index 7375e6f73..c03a40c04 100644 --- a/internal/gazellebinarytest/BUILD.bazel +++ b/internal/gazellebinarytest/BUILD.bazel @@ -5,34 +5,34 @@ gazelle_binary( name = "gazelle_go_x", # keep languages = [ - "//language/go:go_default_library", - ":go_default_library", + "//language/go", + ":gazellebinarytest", ], ) go_library( - name = "go_default_library", + name = "gazellebinarytest", srcs = ["xlang.go"], importpath = "github.com/bazelbuild/bazel-gazelle/internal/gazellebinarytest", visibility = ["//:__subpackages__"], deps = [ - "//config:go_default_library", - "//label:go_default_library", - "//language:go_default_library", - "//repo:go_default_library", - "//resolve:go_default_library", - "//rule:go_default_library", + "//config", + "//label", + "//language", + "//repo", + "//resolve", + "//rule", ], ) go_test( - name = "go_default_test", + name = "gazellebinarytest_test", srcs = ["gazellebinary_test.go"], data = [":gazelle_go_x"], - embed = [":go_default_library"], + embed = [":gazellebinarytest"], rundir = ".", deps = [ - "//testtools:go_default_library", + "//testtools", "@io_bazel_rules_go//go/tools/bazel:go_default_library", ], ) @@ -47,3 +47,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":gazellebinarytest", + visibility = ["//:__subpackages__"], +) diff --git a/internal/gazellebinarytest/gazellebinary_test.go b/internal/gazellebinarytest/gazellebinary_test.go index c0cbb4867..6f5688f4f 100644 --- a/internal/gazellebinarytest/gazellebinary_test.go +++ b/internal/gazellebinarytest/gazellebinary_test.go @@ -55,7 +55,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") # gazelle:prefix example.com/test go_library( - name = "go_default_library", + name = "test", srcs = ["foo.go"], importpath = "example.com/test", visibility = ["//visibility:public"], diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl index 81ab913d1..217d05c67 100644 --- a/internal/go_repository.bzl +++ b/internal/go_repository.bzl @@ -174,6 +174,8 @@ def _go_repository_impl(ctx): cmd.extend(["-external", ctx.attr.build_external]) if ctx.attr.build_file_proto_mode: cmd.extend(["-proto", ctx.attr.build_file_proto_mode]) + if ctx.attr.build_naming_convention: + cmd.extend(["-go_naming_convention", ctx.attr.build_naming_convention]) cmd.extend(ctx.attr.build_extra_args) cmd.append(ctx.path("")) result = env_execute(ctx, cmd, environment = env, timeout = _GO_REPOSITORY_TIMEOUT) @@ -237,6 +239,14 @@ go_repository = repository_rule( "off", ], ), + "build_naming_convention": attr.string( + values = [ + "go_default_library", + "import", + "import_alias", + ], + default = "import_alias", + ), "build_tags": attr.string_list(), "build_file_proto_mode": attr.string( values = [ diff --git a/internal/go_repository_test.go b/internal/go_repository_test.go index 635b8180b..ab41b7d64 100644 --- a/internal/go_repository_test.go +++ b/internal/go_repository_test.go @@ -44,6 +44,7 @@ go_repository( commit = "30136e27e2ac8d167177e8a583aa4c3fea5be833", patches = ["@bazel_gazelle//internal:repository_rules_test_errors.patch"], patch_args = ["-p1"], + build_naming_convention = "go_default_library", ) go_repository( diff --git a/internal/language/test_filegroup/BUILD.bazel b/internal/language/test_filegroup/BUILD.bazel index a066a788c..4a11093cd 100644 --- a/internal/language/test_filegroup/BUILD.bazel +++ b/internal/language/test_filegroup/BUILD.bazel @@ -1,17 +1,17 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "test_filegroup", srcs = ["lang.go"], importpath = "github.com/bazelbuild/bazel-gazelle/internal/language/test_filegroup", visibility = ["//visibility:public"], deps = [ - "//config:go_default_library", - "//label:go_default_library", - "//language:go_default_library", - "//repo:go_default_library", - "//resolve:go_default_library", - "//rule:go_default_library", + "//config", + "//label", + "//language", + "//repo", + "//resolve", + "//rule", ], ) @@ -24,3 +24,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":test_filegroup", + visibility = ["//:__subpackages__"], +) diff --git a/internal/version/BUILD.bazel b/internal/version/BUILD.bazel index 7d39e6ee8..2d358d8d7 100644 --- a/internal/version/BUILD.bazel +++ b/internal/version/BUILD.bazel @@ -1,16 +1,16 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "version", srcs = ["version.go"], importpath = "github.com/bazelbuild/bazel-gazelle/internal/version", visibility = ["//:__subpackages__"], ) go_test( - name = "go_default_test", + name = "version_test", srcs = ["version_test.go"], - embed = [":go_default_library"], + embed = [":version"], ) filegroup( @@ -23,3 +23,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":version", + visibility = ["//:__subpackages__"], +) diff --git a/internal/wspace/BUILD.bazel b/internal/wspace/BUILD.bazel index 003808204..7cd120185 100644 --- a/internal/wspace/BUILD.bazel +++ b/internal/wspace/BUILD.bazel @@ -1,17 +1,17 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "wspace", srcs = ["finder.go"], importpath = "github.com/bazelbuild/bazel-gazelle/internal/wspace", visibility = ["//visibility:public"], ) go_test( - name = "go_default_test", + name = "wspace_test", size = "small", srcs = ["finder_test.go"], - embed = [":go_default_library"], + embed = [":wspace"], ) filegroup( @@ -24,3 +24,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":wspace", + visibility = ["//:__subpackages__"], +) diff --git a/label/BUILD.bazel b/label/BUILD.bazel index 7a657c2a7..a55908508 100644 --- a/label/BUILD.bazel +++ b/label/BUILD.bazel @@ -1,17 +1,17 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "label", srcs = ["label.go"], importpath = "github.com/bazelbuild/bazel-gazelle/label", visibility = ["//visibility:public"], - deps = ["//pathtools:go_default_library"], + deps = ["//pathtools"], ) go_test( - name = "go_default_test", + name = "label_test", srcs = ["label_test.go"], - embed = [":go_default_library"], + embed = [":label"], ) filegroup( @@ -24,3 +24,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":label", + visibility = ["//visibility:public"], +) diff --git a/language/BUILD.bazel b/language/BUILD.bazel index d3f5f132a..bdf238449 100644 --- a/language/BUILD.bazel +++ b/language/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "language", srcs = [ "lang.go", "update.go", @@ -9,10 +9,10 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/language", visibility = ["//visibility:public"], deps = [ - "//config:go_default_library", - "//repo:go_default_library", - "//resolve:go_default_library", - "//rule:go_default_library", + "//config", + "//repo", + "//resolve", + "//rule", ], ) @@ -28,3 +28,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":language", + visibility = ["//visibility:public"], +) diff --git a/language/go/BUILD.bazel b/language/go/BUILD.bazel index ff8faea48..264e9699c 100644 --- a/language/go/BUILD.bazel +++ b/language/go/BUILD.bazel @@ -12,7 +12,7 @@ std_package_list( ) go_library( - name = "go_default_library", + name = "go", srcs = [ "config.go", "constants.go", @@ -32,24 +32,24 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/language/go", visibility = ["//visibility:public"], deps = [ - "//config:go_default_library", - "//flag:go_default_library", - "//internal/version:go_default_library", - "//label:go_default_library", - "//language:go_default_library", - "//language/proto:go_default_library", - "//pathtools:go_default_library", - "//repo:go_default_library", - "//resolve:go_default_library", - "//rule:go_default_library", + "//config", + "//flag", + "//internal/version", + "//label", + "//language", + "//language/proto", + "//pathtools", + "//repo", + "//resolve", + "//rule", "@com_github_bazelbuild_buildtools//build:go_default_library", - "@com_github_pelletier_go_toml//:go_default_library", - "@org_golang_x_sync//errgroup:go_default_library", + "@com_github_pelletier_go_toml//:go-toml", + "@org_golang_x_sync//errgroup", ], ) go_test( - name = "go_default_test", + name = "go_test", srcs = [ "config_test.go", "fileinfo_go_test.go", @@ -61,19 +61,19 @@ go_test( "update_import_test.go", ], data = glob(["testdata/**"]), - embed = [":go_default_library"], + embed = [":go"], deps = [ - "//config:go_default_library", - "//label:go_default_library", - "//language:go_default_library", - "//language/proto:go_default_library", - "//merger:go_default_library", - "//pathtools:go_default_library", - "//repo:go_default_library", - "//resolve:go_default_library", - "//rule:go_default_library", - "//testtools:go_default_library", - "//walk:go_default_library", + "//config", + "//label", + "//language", + "//language/proto", + "//merger", + "//pathtools", + "//repo", + "//resolve", + "//rule", + "//testtools", + "//walk", "@com_github_bazelbuild_buildtools//build:go_default_library", "@org_golang_x_tools//go/vcs:go_default_library", ], @@ -111,3 +111,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":go", + visibility = ["//visibility:public"], +) diff --git a/language/go/config.go b/language/go/config.go index 77fdc035e..98126b141 100644 --- a/language/go/config.go +++ b/language/go/config.go @@ -77,6 +77,13 @@ type goConfig struct { // goGenerateProto indicates whether to generate go_proto_library goGenerateProto bool + // goNamingConvention controls the name of generated targets + goNamingConvention namingConvention + + // goNamingConventionExternal controls the default naming convention for + // imports in external repositories with unknown naming conventions. + goNamingConventionExternal namingConvention + // goProtoCompilers is the protocol buffers compiler(s) to use for go code. goProtoCompilers []string @@ -104,6 +111,10 @@ type goConfig struct { // resolved differently (also depending on goRepositoryMode). moduleMode bool + // map between external repo names and their `build_naming_convention` + // attribute. + repoNamingConvention map[string]namingConvention + // submodules is a list of modules which have the current module's path // as a prefix of their own path. This affects visibility attributes // in internal packages. @@ -237,6 +248,74 @@ func (f tagsFlag) String() string { return "" } +type namingConventionFlag struct { + nc *namingConvention +} + +func (f namingConventionFlag) Set(value string) error { + if nc, err := namingConventionFromString(value); err != nil { + return err + } else { + *f.nc = nc + return nil + } +} + +func (f *namingConventionFlag) String() string { + if f == nil || f.nc == nil { + return "naming_convention" + } + return f.nc.String() +} + +// namingConvention determines how go targets are named. +type namingConvention int + +const ( + // Try to detect the naming convention in use. + unknownNamingConvention namingConvention = iota + + // 'go_default_library' and 'go_default_test' + goDefaultLibraryNamingConvention + + // For an import path that ends with foo, the go_library rules target is + // named 'foo', the go_test is named 'foo_test'. + // For a main package, the go_binary takes the 'foo' name, the library + // is named 'foo_lib', and the go_test is named 'foo_test'. + importNamingConvention + + // Same as importNamingConvention, but generate alias rules for libraries that have + // the legacy 'go_default_library' name. + importAliasNamingConvention +) + +func (nc namingConvention) String() string { + switch nc { + case goDefaultLibraryNamingConvention: + return "go_default_library" + case importNamingConvention: + return "import" + case importAliasNamingConvention: + return "import_alias" + } + return "" +} + +func namingConventionFromString(s string) (namingConvention, error) { + switch s { + case "": + return unknownNamingConvention, nil + case "go_default_library": + return goDefaultLibraryNamingConvention, nil + case "import": + return importNamingConvention, nil + case "import_alias": + return importAliasNamingConvention, nil + default: + return unknownNamingConvention, fmt.Errorf("unknown naming convention %q", s) + } +} + type moduleRepo struct { repoName, modulePath string } @@ -250,6 +329,8 @@ func (*goLang) KnownDirectives() []string { "build_tags", "go_generate_proto", "go_grpc_compilers", + "go_naming_convention", + "go_naming_convention_external", "go_proto_compilers", "go_visibility", "importmap_prefix", @@ -291,6 +372,14 @@ func (*goLang) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) { "go_repository_module_mode", false, "set when gazelle is invoked by go_repository in module mode") + fs.Var( + &namingConventionFlag{&gc.goNamingConvention}, + "go_naming_convention", + "controls generated library names. One of (go_default_library, import, import_alias)") + fs.Var( + &namingConventionFlag{&gc.goNamingConventionExternal}, + "go_naming_convention_external", + "controls naming convention used when resolving libraries in external repositories with unknown conventions") case "update-repos": fs.StringVar(&gc.buildDirectivesAttr, @@ -377,6 +466,24 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.` log.Printf("Found RULES_GO_VERSION %s. Minimum compatible version is %s.\n%s", gc.rulesGoVersion, minimumRulesGoVersion, message) } } + repoNamingConvention := map[string]namingConvention{} + for _, repo := range c.Repos { + if repo.Kind() == "go_repository" { + if attr := repo.AttrString("build_naming_convention"); attr == "" { + // No naming convention specified. + // go_repsitory uses importAliasNamingConvention by default, so we + // could use whichever name. + // resolveExternal should take that as a signal to follow the current + // naming convention to avoid churn. + repoNamingConvention[repo.Name()] = importAliasNamingConvention + } else if nc, err := namingConventionFromString(attr); err != nil { + log.Printf("in go_repository named %q: %v", repo.Name(), err) + } else { + repoNamingConvention[repo.Name()] = nc + } + } + } + gc.repoNamingConvention = repoNamingConvention } if !gc.moduleMode { @@ -412,13 +519,28 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.` } gc.preprocessTags() gc.setBuildTags(d.Value) - case "go_generate_proto": + case "go_generate_proto": if goGenerateProto, err := strconv.ParseBool(d.Value); err == nil { gc.goGenerateProto = goGenerateProto } else { log.Printf("parsing go_generate_proto: %v", err) } + + case "go_naming_convention": + if nc, err := namingConventionFromString(d.Value); err == nil { + gc.goNamingConvention = nc + } else { + log.Print(err) + } + + case "go_naming_convention_external": + if nc, err := namingConventionFromString(d.Value); err == nil { + gc.goNamingConventionExternal = nc + } else { + log.Print(err) + } + case "go_grpc_compilers": // Special syntax (empty value) to reset directive. if d.Value == "" { @@ -450,6 +572,7 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.` setPrefix(d.Value) } } + if !gc.prefixSet { for _, r := range f.Rules { switch r.Kind() { @@ -472,6 +595,10 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.` } } } + + if gc.goNamingConvention == unknownNamingConvention { + gc.goNamingConvention = detectNamingConvention(c, f) + } } // checkPrefix checks that a string may be used as a prefix. We forbid local @@ -522,3 +649,85 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.` } var errRulesGoRepoNotFound = errors.New(config.RulesGoRepoName + " external repository not found") + +// detectNamingConvention attempts to detect the naming convention in use by +// reading build files in subdirectories of the repository root directory. +// +// If detectNamingConvention can't detect the naming convention (for example, +// because no build files are found or multiple naming conventions are found), +// importNamingConvention is returned. +func detectNamingConvention(c *config.Config, rootFile *rule.File) namingConvention { + if !c.IndexLibraries { + // Indexing is disabled, which usually means speed is important and I/O + // should be minimized. Let's not open extra files or directories. + return importNamingConvention + } + + detectInFile := func(f *rule.File) namingConvention { + for _, r := range f.Rules { + // NOTE: map_kind is not supported. c.KindMap will not be accurate in + // subdirectories. + kind := r.Kind() + name := r.Name() + if kind != "alias" && name == defaultLibName { + // Assume any kind of rule with the name "go_default_library" is some + // kind of go library. The old version of go_proto_library used this + // name, and it's possible with map_kind as well. + return goDefaultLibraryNamingConvention + } else if isGoLibrary(kind) && name == path.Base(r.AttrString("importpath")) { + return importNamingConvention + } + } + return unknownNamingConvention + } + + detectInDir := func(dir, rel string) namingConvention { + var f *rule.File + for _, name := range c.ValidBuildFileNames { + fpath := filepath.Join(dir, name) + data, err := ioutil.ReadFile(fpath) + if err != nil { + continue + } + f, err = rule.LoadData(fpath, rel, data) + if err != nil { + continue + } + } + if f == nil { + return unknownNamingConvention + } + return detectInFile(f) + } + + nc := unknownNamingConvention + if rootFile != nil { + if rootNC := detectInFile(rootFile); rootNC != unknownNamingConvention { + return rootNC + } + } + + infos, err := ioutil.ReadDir(c.RepoRoot) + if err != nil { + return importNamingConvention + } + for _, info := range infos { + if !info.IsDir() { + continue + } + dirName := info.Name() + dirNC := detectInDir(filepath.Join(c.RepoRoot, dirName), dirName) + if dirNC == unknownNamingConvention { + continue + } + if nc != unknownNamingConvention && dirNC != nc { + // Subdirectories use different conventions. Return the default. + return importNamingConvention + } + nc = dirNC + } + if nc == unknownNamingConvention { + return importNamingConvention + } + return nc +} diff --git a/language/go/config_test.go b/language/go/config_test.go index d2a2aba4d..2196077fd 100644 --- a/language/go/config_test.go +++ b/language/go/config_test.go @@ -64,6 +64,7 @@ func TestCommandLine(t *testing.T) { t, "-build_tags=foo,bar", "-go_prefix=example.com/repo", + "-go_naming_convention=import_alias", "-external=vendored", "-repo_root=.") gc := getGoConfig(c) @@ -78,6 +79,9 @@ func TestCommandLine(t *testing.T) { if gc.depMode != vendorMode { t.Errorf("got dep mode %v; want %v", gc.depMode, vendorMode) } + if gc.goNamingConvention != importAliasNamingConvention { + t.Errorf("got naming convention %v; want %v", gc.goNamingConvention, importAliasNamingConvention) + } } func TestDirectives(t *testing.T) { diff --git a/language/go/fix.go b/language/go/fix.go index 88d99963e..0cadfdc5a 100644 --- a/language/go/fix.go +++ b/language/go/fix.go @@ -32,6 +32,123 @@ func (_ *goLang) Fix(c *config.Config, f *rule.File) { squashXtest(c, f) removeLegacyProto(c, f) removeLegacyGazelle(c, f) + migrateNamingConvention(c, f) +} + +// migrateNamingConvention renames rules according to go_naming_convention +// directives. +func migrateNamingConvention(c *config.Config, f *rule.File) { + // Determine old and new names for go_library and go_test. + nc := getGoConfig(c).goNamingConvention + importPath := InferImportPath(c, f.Pkg) + if importPath == "" { + return + } + var pkgName string // unknown unless there's a binary + if fileContainsGoBinary(c, f) { + pkgName = "main" + } + libName := libNameByConvention(nc, importPath, pkgName) + testName := testNameByConvention(nc, importPath) + var migrateLibName, migrateTestName string + switch nc { + case goDefaultLibraryNamingConvention: + migrateLibName = libNameByConvention(importNamingConvention, importPath, pkgName) + migrateTestName = testNameByConvention(importNamingConvention, importPath) + case importNamingConvention, importAliasNamingConvention: + migrateLibName = defaultLibName + migrateTestName = defaultTestName + default: + return + } + + // Check whether the new names are in use. If there are rules with both old + // and new names, there will be a conflict. + var haveLib, haveMigrateLib, haveTest, haveMigrateTest bool + for _, r := range f.Rules { + switch { + case r.Name() == libName: + haveLib = true + case r.Kind() == "go_library" && r.Name() == migrateLibName && r.AttrString("importpath") == importPath: + haveMigrateLib = true + case r.Name() == testName: + haveTest = true + case r.Kind() == "go_test" && r.Name() == migrateTestName && strListAttrContains(r, "embed", ":"+migrateLibName): + haveMigrateTest = true + } + } + if haveLib && haveMigrateLib { + log.Printf("%[1]s: Tried to rename %[2]s to %[3]s, but %[3]s already exists.", f.Path, migrateLibName, libName) + } + if haveTest && haveMigrateTest { + log.Printf("%[1]s: Tried to rename %[2]s to %[3]s, but %[3]s already exists.", f.Path, migrateTestName, testName) + } + shouldMigrateLib := haveMigrateLib && !haveLib + shouldMigrateTest := haveMigrateTest && !haveTest + + // Rename the targets and stuff in the same file that refers to them. + for _, r := range f.Rules { + // TODO(jayconrod): support map_kind directive. + // We'll need to move the metaresolver from resolve.RuleIndex to config.Config so we can access it from here. + switch r.Kind() { + case "go_binary": + if haveMigrateLib && shouldMigrateLib { + replaceInStrListAttr(r, "embed", ":"+migrateLibName, ":"+libName) + } + case "go_library": + if r.Name() == migrateLibName && shouldMigrateLib { + r.SetName(libName) + } + case "go_test": + if r.Name() == migrateTestName && shouldMigrateTest { + r.SetName(testName) + } + if shouldMigrateLib { + replaceInStrListAttr(r, "embed", ":"+migrateLibName, ":"+libName) + } + } + } +} + +// fileContainsGoBinary returns whether the file has a go_binary rule. +func fileContainsGoBinary(c *config.Config, f *rule.File) bool { + if f == nil { + return false + } + for _, r := range f.Rules { + kind := r.Kind() + if mappedKind, ok := c.KindMap[kind]; ok { + kind = mappedKind.KindName + } + if kind == "go_binary" { + return true + } + } + return false +} + +func replaceInStrListAttr(r *rule.Rule, attr, old, new string) { + items := r.AttrStrings(attr) + changed := false + for i := range items { + if items[i] == old { + changed = true + items[i] = new + } + } + if changed { + r.SetAttr(attr, items) + } +} + +func strListAttrContains(r *rule.Rule, attr, s string) bool { + items := r.AttrStrings(attr) + for _, item := range items { + if item == s { + return true + } + } + return false } // migrateLibraryEmbed converts "library" attributes to "embed" attributes, diff --git a/language/go/fix_test.go b/language/go/fix_test.go index dfc9ff210..24e4ce981 100644 --- a/language/go/fix_test.go +++ b/language/go/fix_test.go @@ -24,13 +24,464 @@ import ( ) type fixTestCase struct { - desc, old, want string + desc, old, want string + namingConvention namingConvention } func TestFixFile(t *testing.T) { for _, tc := range []fixTestCase{ - // migrateLibraryEmbed tests + // migrateNamingConvention tests { + desc: "go_naming_convention=go_default_library -> import for lib", + namingConvention: importNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + }, { + desc: "go_naming_convention=go_default_library -> import for bin", + namingConvention: importNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_binary( + name = "foo", + embed = [":go_default_library"], +) + +go_library( + name = "go_default_library", + importpath = "example.com/foo", + srcs = ["foo.go"], +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_binary( + name = "foo", + embed = [":foo_lib"], +) + +go_library( + name = "foo_lib", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo_lib"], +) +`, + }, { + desc: "go_naming_convention=go_default_library -> import conflict", + namingConvention: importNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load(":build_defs.bzl", "x_binary") + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", + visibility = ["//visibility:private"], +) + +x_binary( + name = "foo", +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load(":build_defs.bzl", "x_binary") + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", + visibility = ["//visibility:private"], +) + +x_binary( + name = "foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + }, { + desc: "go_naming_convention=import -> go_default_library for lib", + namingConvention: goDefaultLibraryNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + }, { + desc: "go_naming_convention=import -> go_default_library for bin", + namingConvention: goDefaultLibraryNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_binary( + name = "foo", + embed = [":foo_lib"], +) + +go_library( + name = "foo_lib", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo_lib"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_binary( + name = "foo", + embed = [":go_default_library"], +) + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + }, { + desc: "go_naming_convention=go_default_library -> import_alias for lib", + namingConvention: importAliasNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", + visibility = ["//visibility:private"], +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", + visibility = ["//visibility:private"], +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + }, { + desc: "go_naming_convention=import_alias -> go_default_library for lib", + namingConvention: goDefaultLibraryNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + }, { + desc: "go_naming_convention=go_default_library -> import_alias for bin", + namingConvention: importAliasNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_binary( + name = "foo", + embed = [":go_default_library"], +) + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_binary( + name = "foo", + embed = [":foo_lib"], +) + +go_library( + name = "foo_lib", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo_lib"], +) +`, + }, { + desc: "go_naming_convention=import -> import_alias for lib", + namingConvention: importAliasNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + }, { + desc: "go_naming_convention import_alias -> import for lib", + namingConvention: importNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + }, { + desc: "go_naming_convention=import -> import for lib", + namingConvention: importNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + }, { + desc: "go_naming_convention go_default_library -> go_default_library for lib", + namingConvention: goDefaultLibraryNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", + visibility = ["//visibility:private"], +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["foo.go"], + importpath = "example.com/foo", + visibility = ["//visibility:private"], +) + +go_test( + name = "go_default_test", + srcs = ["foo_test.go"], + embed = [":go_default_library"], +) +`, + }, { + desc: "go_naming_convention=import_alias -> import_alias for lib", + namingConvention: importAliasNamingConvention, + old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "foo", + srcs = ["foo.go"], + importpath = "example.com/foo", +) + +go_test( + name = "foo_test", + srcs = ["foo_test.go"], + embed = [":foo"], +) +`, + }, { + // migrateLibraryEmbed tests desc: "library migrated to embed", old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") @@ -408,7 +859,10 @@ go_proto_library(name = "foo_proto") } { t.Run(tc.desc, func(t *testing.T) { testFix(t, tc, func(f *rule.File) { - c, langs, _ := testConfig(t) + c, langs, _ := testConfig(t, + "-go_naming_convention="+tc.namingConvention.String(), + "-go_prefix=example.com/foo", + ) c.ShouldFix = true for _, lang := range langs { lang.Fix(c, f) diff --git a/language/go/gen_std_package_list/BUILD.bazel b/language/go/gen_std_package_list/BUILD.bazel index 8587f797a..45624c6a2 100644 --- a/language/go/gen_std_package_list/BUILD.bazel +++ b/language/go/gen_std_package_list/BUILD.bazel @@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_binary( name = "gen_std_package_list", - embed = [":go_default_library"], + embed = [":gen_std_package_list_lib"], # TODO(bazelbuild/rules_go#2302): needs to be public in order to use as # a default value in std_package_list, which is defined with go_rule. # std_package_list should not be defined with go_rule. @@ -10,7 +10,7 @@ go_binary( ) go_library( - name = "go_default_library", + name = "gen_std_package_list_lib", srcs = ["gen_std_package_list.go"], importpath = "github.com/bazelbuild/bazel-gazelle/language/go/gen_std_package_list", visibility = ["//visibility:private"], diff --git a/language/go/generate.go b/language/go/generate.go index 30c3e370e..3d371da6b 100644 --- a/language/go/generate.go +++ b/language/go/generate.go @@ -130,7 +130,7 @@ func (gl *goLang) GenerateRules(args language.GenerateArgs) language.GenerateRes // if a go_proto_library rule already exists for this // proto package, treat it as if the proto package // doesn't exist. - pkg = emptyPackage(c, args.Dir, args.Rel) + pkg = emptyPackage(c, args.Dir, args.Rel, args.File) break } pkg = &goPackage{ @@ -142,7 +142,7 @@ func (gl *goLang) GenerateRules(args language.GenerateArgs) language.GenerateRes break } } else { - pkg = emptyPackage(c, args.Dir, args.Rel) + pkg = emptyPackage(c, args.Dir, args.Rel, args.File) } } else { log.Print(err) @@ -270,6 +270,9 @@ func (gl *goLang) GenerateRules(args language.GenerateArgs) language.GenerateRes if r := g.maybeGenerateExtraLib(lib, pkg); r != nil { rules = append(rules, r) } + if r := g.maybeGenerateAlias(pkg, libName); r != nil { + rules = append(rules, r) + } rules = append(rules, g.generateBin(pkg, libName), g.generateTest(pkg, libName)) @@ -379,9 +382,18 @@ func selectPackage(c *config.Config, dir string, packageMap map[string]*goPackag return nil, err } -func emptyPackage(c *config.Config, dir, rel string) *goPackage { +func emptyPackage(c *config.Config, dir, rel string, f *rule.File) *goPackage { + var pkgName string + if fileContainsGoBinary(c, f) { + // If the file contained a go_binary, its library may have a "_lib" suffix. + // Set the package name to "main" so that we generate an empty library rule + // with that name. + pkgName = "main" + } else { + pkgName = defaultPackageName(c, dir) + } pkg := &goPackage{ - name: defaultPackageName(c, dir), + name: pkgName, dir: dir, rel: rel, } @@ -452,7 +464,9 @@ func (g *generator) generateProto(mode proto.Mode, target protoTarget, importPat } func (g *generator) generateLib(pkg *goPackage, embed string) *rule.Rule { - goLibrary := rule.NewRule("go_library", defaultLibName) + gc := getGoConfig(g.c) + name := libNameByConvention(gc.goNamingConvention, pkg.importPath, pkg.name) + goLibrary := rule.NewRule("go_library", name) if !pkg.library.sources.hasGo() && embed == "" { return goLibrary // empty } @@ -468,8 +482,25 @@ func (g *generator) generateLib(pkg *goPackage, embed string) *rule.Rule { return goLibrary } +func (g *generator) maybeGenerateAlias(pkg *goPackage, libName string) *rule.Rule { + if pkg.isCommand() || libName == "" { + return nil + } + gc := getGoConfig(g.c) + if gc.goNamingConvention == goDefaultLibraryNamingConvention { + return nil + } + alias := rule.NewRule("alias", defaultLibName) + alias.SetAttr("visibility", g.commonVisibility(pkg.importPath)) + if gc.goNamingConvention == importAliasNamingConvention { + alias.SetAttr("actual", ":"+libName) + } + return alias +} + func (g *generator) generateBin(pkg *goPackage, library string) *rule.Rule { - name := pathtools.RelBaseName(pkg.rel, getGoConfig(g.c).prefix, g.c.RepoRoot) + gc := getGoConfig(g.c) + name := binName(pkg.rel, gc.prefix, g.c.RepoRoot) goBinary := rule.NewRule("go_binary", name) if !pkg.isCommand() || pkg.binary.sources.isEmpty() && library == "" { return goBinary // empty @@ -480,7 +511,9 @@ func (g *generator) generateBin(pkg *goPackage, library string) *rule.Rule { } func (g *generator) generateTest(pkg *goPackage, library string) *rule.Rule { - goTest := rule.NewRule("go_test", defaultTestName) + gc := getGoConfig(g.c) + name := testNameByConvention(gc.goNamingConvention, pkg.importPath) + goTest := rule.NewRule("go_test", name) if !pkg.test.sources.hasGo() { return goTest // empty } diff --git a/language/go/generate_test.go b/language/go/generate_test.go index 9d89e8d29..0581ba318 100644 --- a/language/go/generate_test.go +++ b/language/go/generate_test.go @@ -95,6 +95,7 @@ func TestGenerateRules(t *testing.T) { t.Fatalf("error reading %s: %v", wantPath, err) } want := string(wantBytes) + want = strings.ReplaceAll(want, "\r\n", "\n") if got != want { t.Errorf("GenerateRules %q: got:\n%s\nwant:\n%s", rel, got, want) @@ -104,7 +105,7 @@ func TestGenerateRules(t *testing.T) { } func TestGenerateRulesEmpty(t *testing.T) { - c, langs, _ := testConfig(t) + c, langs, _ := testConfig(t, "-go_prefix=example.com/repo") goLang := langs[1].(*goLang) res := goLang.GenerateRules(language.GenerateArgs{ Config: c, @@ -124,11 +125,11 @@ filegroup(name = "go_default_library_protos") go_proto_library(name = "foo_go_proto") -go_library(name = "go_default_library") +go_library(name = "foo") go_binary(name = "foo") -go_test(name = "go_default_test") +go_test(name = "foo_test") `) if got != want { t.Errorf("got:\n%s\nwant:\n%s", got, want) @@ -150,7 +151,7 @@ func TestGenerateRulesEmptyLegacyProto(t *testing.T) { } func TestGenerateRulesEmptyPackageProto(t *testing.T) { - c, langs, _ := testConfig(t, "-proto=package") + c, langs, _ := testConfig(t, "-proto=package", "-go_prefix=example.com/repo") oldContent := []byte(` proto_library( name = "dead_proto", @@ -186,11 +187,11 @@ filegroup(name = "go_default_library_protos") go_proto_library(name = "foo_go_proto") -go_library(name = "go_default_library") +go_library(name = "foo") go_binary(name = "foo") -go_test(name = "go_default_test") +go_test(name = "foo_test") `) if got != want { t.Errorf("got:\n%s\nwant:\n%s", got, want) @@ -265,7 +266,7 @@ func prebuiltProtoRules() []*rule.Rule { proto.Package{ Name: "foo", Files: map[string]proto.FileInfo{ - "foo.proto": proto.FileInfo{}, + "foo.proto": {}, }, Imports: map[string]bool{}, Options: map[string]string{}, diff --git a/language/go/kinds.go b/language/go/kinds.go index e5b85b9e3..910c17651 100644 --- a/language/go/kinds.go +++ b/language/go/kinds.go @@ -18,6 +18,10 @@ package golang import "github.com/bazelbuild/bazel-gazelle/rule" var goKinds = map[string]rule.KindInfo{ + "alias": { + NonEmptyAttrs: map[string]bool{"actual": true}, + MergeableAttrs: map[string]bool{"actual": true}, + }, "filegroup": { NonEmptyAttrs: map[string]bool{"srcs": true}, MergeableAttrs: map[string]bool{"srcs": true}, diff --git a/language/go/package.go b/language/go/package.go index 0f47b0f9a..806365d84 100644 --- a/language/go/package.go +++ b/language/go/package.go @@ -25,6 +25,7 @@ import ( "github.com/bazelbuild/bazel-gazelle/config" "github.com/bazelbuild/bazel-gazelle/language/proto" + "github.com/bazelbuild/bazel-gazelle/pathtools" "github.com/bazelbuild/bazel-gazelle/rule" ) @@ -79,6 +80,9 @@ const ( platformSet ) +// Matches a package version, eg. the end segment of 'example.com/foo/v1' +var pkgVersionRe = regexp.MustCompile("^v[0-9]+$") + // addFile adds the file described by "info" to a target in the package "p" if // the file is buildable. // @@ -156,14 +160,63 @@ func (pkg *goPackage) inferImportPath(c *config.Config) error { return fmt.Errorf("%s: go prefix is not set, so importpath can't be determined for rules. Set a prefix with a '# gazelle:prefix' comment or with -go_prefix on the command line", pkg.dir) } pkg.importPath = InferImportPath(c, pkg.rel) + return nil +} - if pkg.rel == gc.prefixRel { - pkg.importPath = gc.prefix - } else { - fromPrefixRel := strings.TrimPrefix(pkg.rel, gc.prefixRel+"/") - pkg.importPath = path.Join(gc.prefix, fromPrefixRel) +// libNameFromImportPath returns a a suitable go_library name based on the import path. +// Major version suffixes (eg. "v1") are dropped. +func libNameFromImportPath(dir string) string { + i := strings.LastIndexAny(dir, "/\\") + if i < 0 { + return dir + } + name := dir[i+1:] + if pkgVersionRe.MatchString(name) { + dir := dir[:i] + i = strings.LastIndexAny(dir, "/\\") + if i >= 0 { + name = dir[i+1:] + } } - return nil + return name +} + +// libNameByConvention returns a suitable name for a go_library using the given +// naming convention, the import path, and the package name. +func libNameByConvention(nc namingConvention, imp string, pkgName string) string { + if nc == goDefaultLibraryNamingConvention { + return defaultLibName + } + name := libNameFromImportPath(imp) + isCommand := pkgName == "main" + if name == "" { + if isCommand { + name = "lib" + } else { + name = pkgName + } + } else if isCommand { + name += "_lib" + } + return name +} + +// testNameByConvention returns a suitable name for a go_test using the given +// naming convention and the import path. +func testNameByConvention(nc namingConvention, imp string) string { + if nc == goDefaultLibraryNamingConvention { + return defaultTestName + } + libName := libNameFromImportPath(imp) + if libName == "" { + libName = "lib" + } + return libName + "_test" +} + +// binName returns a suitable name for a go_binary. +func binName(rel, prefix, repoRoot string) string { + return pathtools.RelBaseName(rel, prefix, repoRoot) } func InferImportPath(c *config.Config, rel string) string { diff --git a/language/go/resolve.go b/language/go/resolve.go index 339f6ff0f..5c0a8ebe8 100644 --- a/language/go/resolve.go +++ b/language/go/resolve.go @@ -161,14 +161,15 @@ func ResolveGo(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, im // current repo if pathtools.HasPrefix(imp, gc.prefix) { pkg := path.Join(gc.prefixRel, pathtools.TrimPrefix(imp, gc.prefix)) - return label.New("", pkg, defaultLibName), nil + libName := libNameByConvention(gc.goNamingConvention, imp, "") + return label.New("", pkg, libName), nil } } if gc.depMode == externalMode { - return resolveExternal(gc.moduleMode, rc, imp) + return resolveExternal(c, rc, imp) } else { - return resolveVendored(rc, imp) + return resolveVendored(gc, imp) } } @@ -233,7 +234,7 @@ func resolveWithIndexGo(c *config.Config, ix *resolve.RuleIndex, imp string, fro var modMajorRex = regexp.MustCompile(`/v\d+(?:/|$)`) -func resolveExternal(moduleMode bool, rc *repo.RemoteCache, imp string) (label.Label, error) { +func resolveExternal(c *config.Config, rc *repo.RemoteCache, imp string) (label.Label, error) { // If we're in module mode, use "go list" to find the module path and // repository name. Otherwise, use special cases (for github.com, golang.org) // or send a GET with ?go-get=1 to find the root. If the path contains @@ -242,6 +243,8 @@ func resolveExternal(moduleMode bool, rc *repo.RemoteCache, imp string) (label.L // Eventually module mode will be the only mode. But for now, it's expensive // and not the common case, especially when known repositories aren't // listed in WORKSPACE (which is currently the case within go_repository). + gc := getGoConfig(c) + moduleMode := gc.moduleMode if !moduleMode { moduleMode = pathWithoutSemver(imp) != "" } @@ -266,11 +269,35 @@ func resolveExternal(moduleMode bool, rc *repo.RemoteCache, imp string) (label.L pkg = pathtools.TrimPrefix(impWithoutSemver, prefix) } - return label.New(repo, pkg, defaultLibName), nil + // Determine what naming convention is used by the repository. + // If there is no known repository, it's probably declared in an http_archive + // somewhere like go_rules_dependencies, so use the old naming convention, + // unless the user has explicitly told us otherwise. + // If the repository uses the import_alias convention (default for + // go_repository), use the convention from the current directory unless the + // user has told us otherwise. + nc := gc.repoNamingConvention[repo] + if nc == unknownNamingConvention { + if gc.goNamingConventionExternal != unknownNamingConvention { + nc = gc.goNamingConventionExternal + } else { + nc = goDefaultLibraryNamingConvention + } + } else if nc == importAliasNamingConvention { + if gc.goNamingConventionExternal != unknownNamingConvention { + nc = gc.goNamingConventionExternal + } else { + nc = gc.goNamingConvention + } + } + + name := libNameByConvention(nc, imp, "") + return label.New(repo, pkg, name), nil } -func resolveVendored(rc *repo.RemoteCache, imp string) (label.Label, error) { - return label.New("", path.Join("vendor", imp), defaultLibName), nil +func resolveVendored(gc *goConfig, imp string) (label.Label, error) { + name := libNameByConvention(gc.goNamingConvention, imp, "") + return label.New("", path.Join("vendor", imp), name), nil } func resolveProto(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, imp string, from label.Label) (label.Label, error) { @@ -299,7 +326,8 @@ func resolveProto(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, if from.Pkg == "vendor" || strings.HasPrefix(from.Pkg, "vendor/") { rel = path.Join("vendor", rel) } - return label.New("", rel, defaultLibName), nil + libName := libNameByConvention(getGoConfig(c).goNamingConvention, imp, "") + return label.New("", rel, libName), nil } func resolveGoTool(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, imp string, from label.Label) (label.Label, error) { diff --git a/language/go/resolve_test.go b/language/go/resolve_test.go index 91e7ac8da..29264ff24 100644 --- a/language/go/resolve_test.go +++ b/language/go/resolve_test.go @@ -35,11 +35,12 @@ func TestResolveGo(t *testing.T) { rel, content string } type testCase struct { - desc string - index []buildFile - old buildFile - want string - skipIndex bool + desc string + index []buildFile + old buildFile + want string + skipIndex bool + namingConvention namingConvention } for _, tc := range []testCase{ { @@ -461,8 +462,8 @@ go_binary( go_binary( name = "bin", deps = [ - ":go_default_library", - "//sub:go_default_library", + ":resolve", + "//sub", ], ) `, @@ -531,7 +532,22 @@ go_binary( want: ` go_binary( name = "bin", - deps = ["//vendor/example.com/outside/prefix:go_default_library"], + deps = ["//vendor/example.com/outside/prefix"], +) +`, + }, { + desc: "vendor with go_naming_convention=import", + namingConvention: importNamingConvention, + old: buildFile{content: ` +go_binary( + name = "bin", + _imports = ["example.com/outside/prefix"], +) +`}, + want: ` +go_binary( + name = "bin", + deps = ["//vendor/example.com/outside/prefix"], ) `, }, { @@ -559,7 +575,7 @@ go_binary( want: ` go_binary( name = "bin", - deps = ["//vendor/example.com/foo:go_default_library"], + deps = ["//vendor/example.com/foo"], )`, }, { desc: "proto_override", @@ -919,6 +935,7 @@ go_proto_library( c, langs, cexts := testConfig( t, "-go_prefix=example.com/repo/resolve", + fmt.Sprintf("-go_naming_convention=%s", tc.namingConvention), "-external=vendored", fmt.Sprintf("-index=%v", !tc.skipIndex)) mrslv := make(mapResolver) exts := make([]interface{}, 0, len(langs)) @@ -1067,19 +1084,46 @@ func TestResolveExternal(t *testing.T) { ix.Finish() gl := langs[1].(*goLang) for _, tc := range []struct { - desc, importpath string - repos []repo.Repo - moduleMode bool - want string + desc, importpath string + repos []repo.Repo + moduleMode bool + namingConvention namingConvention + namingConventionExternal namingConvention + repoNamingConvention map[string]namingConvention + want string }{ { desc: "top", importpath: "example.com/repo", want: "@com_example_repo//:go_default_library", + }, { + desc: "top_import_naming_convention", + namingConvention: goDefaultLibraryNamingConvention, + repoNamingConvention: map[string]namingConvention{ + "com_example_repo": importNamingConvention, + }, + importpath: "example.com/repo", + want: "@com_example_repo//:repo", + }, { + desc: "top_import_alias_naming_convention", + namingConvention: goDefaultLibraryNamingConvention, + repoNamingConvention: map[string]namingConvention{ + "com_example_repo": importAliasNamingConvention, + }, + importpath: "example.com/repo", + want: "@com_example_repo//:go_default_library", }, { desc: "sub", importpath: "example.com/repo/lib", want: "@com_example_repo//lib:go_default_library", + }, { + desc: "sub_import_alias_naming_convention", + namingConvention: importNamingConvention, + repoNamingConvention: map[string]namingConvention{ + "com_example_repo": importAliasNamingConvention, + }, + importpath: "example.com/repo/lib", + want: "@com_example_repo//lib", }, { desc: "custom_repo", repos: []repo.Repo{{ @@ -1088,6 +1132,35 @@ func TestResolveExternal(t *testing.T) { }}, importpath: "example.com/repo/lib", want: "@custom_repo_name//lib:go_default_library", + }, { + desc: "custom_repo_import_naming_convention", + repos: []repo.Repo{{ + Name: "custom_repo_name", + GoPrefix: "example.com/repo", + }}, + repoNamingConvention: map[string]namingConvention{ + "custom_repo_name": importNamingConvention, + }, + importpath: "example.com/repo/lib", + want: "@custom_repo_name//lib", + }, { + desc: "custom_repo_naming_convention_extern_import", + repos: []repo.Repo{{ + Name: "custom_repo_name", + GoPrefix: "example.com/repo", + }}, + namingConventionExternal: importNamingConvention, + importpath: "example.com/repo/lib", + want: "@custom_repo_name//lib", + }, { + desc: "custom_repo_naming_convention_extern_default", + repos: []repo.Repo{{ + Name: "custom_repo_name", + GoPrefix: "example.com/repo", + }}, + namingConventionExternal: goDefaultLibraryNamingConvention, + importpath: "example.com/repo/lib", + want: "@custom_repo_name//lib:go_default_library", }, { desc: "qualified", importpath: "example.com/repo.git/lib", @@ -1145,6 +1218,9 @@ func TestResolveExternal(t *testing.T) { } { t.Run(tc.desc, func(t *testing.T) { gc.moduleMode = tc.moduleMode + gc.goNamingConvention = tc.namingConvention + gc.goNamingConventionExternal = tc.namingConventionExternal + gc.repoNamingConvention = tc.repoNamingConvention rc := testRemoteCache(tc.repos) r := rule.NewRule("go_library", "x") imports := rule.PlatformStrings{Generic: []string{tc.importpath}} diff --git a/language/go/testdata/allcgolib/BUILD.want b/language/go/testdata/allcgolib/BUILD.want index 29f9dd58a..730e3789d 100644 --- a/language/go/testdata/allcgolib/BUILD.want +++ b/language/go/testdata/allcgolib/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "allcgolib", srcs = [ "foo.c", "foo.go", @@ -16,8 +16,8 @@ go_library( ) go_test( - name = "go_default_test", + name = "allcgolib_test", srcs = ["foo_test.go"], _gazelle_imports = ["testing"], - embed = [":go_default_library"], + embed = [":allcgolib"], ) diff --git a/language/go/testdata/bin/BUILD.want b/language/go/testdata/bin/BUILD.want index 6f03dfca5..51ae87700 100644 --- a/language/go/testdata/bin/BUILD.want +++ b/language/go/testdata/bin/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_library( - name = "go_default_library", + name = "bin_lib", srcs = ["main.go"], _gazelle_imports = [ "example.com/repo/lib", @@ -14,6 +14,6 @@ go_library( go_binary( name = "bin", _gazelle_imports = [], - embed = [":go_default_library"], + embed = [":bin_lib"], visibility = ["//visibility:public"], ) diff --git a/language/go/testdata/bin_with_tests/BUILD.want b/language/go/testdata/bin_with_tests/BUILD.want index decc3eeeb..2dd826d1d 100644 --- a/language/go/testdata/bin_with_tests/BUILD.want +++ b/language/go/testdata/bin_with_tests/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") go_library( - name = "go_default_library", + name = "bin_with_tests_lib", srcs = ["main.go"], _gazelle_imports = [ "example.com/repo/lib", @@ -14,13 +14,13 @@ go_library( go_binary( name = "bin_with_tests", _gazelle_imports = [], - embed = [":go_default_library"], + embed = [":bin_with_tests_lib"], visibility = ["//visibility:public"], ) go_test( - name = "go_default_test", + name = "bin_with_tests_test", srcs = ["bin_test.go"], _gazelle_imports = ["testing"], - embed = [":go_default_library"], + embed = [":bin_with_tests_lib"], ) diff --git a/language/go/testdata/cgolib/BUILD.want b/language/go/testdata/cgolib/BUILD.want index fdfce5336..85f192c54 100644 --- a/language/go/testdata/cgolib/BUILD.want +++ b/language/go/testdata/cgolib/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "cgolib", srcs = [ "asm.S", "foo.c", @@ -25,8 +25,8 @@ go_library( ) go_test( - name = "go_default_test", + name = "cgolib_test", srcs = ["foo_test.go"], _gazelle_imports = ["testing"], - embed = [":go_default_library"], + embed = [":cgolib"], ) diff --git a/language/go/testdata/cgolib_with_build_tags/BUILD.want b/language/go/testdata/cgolib_with_build_tags/BUILD.want index ea09c8e13..ca21cdf92 100644 --- a/language/go/testdata/cgolib_with_build_tags/BUILD.want +++ b/language/go/testdata/cgolib_with_build_tags/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "cgolib_with_build_tags", srcs = [ "asm_linux.S", "asm_other.S", @@ -90,8 +90,8 @@ go_library( ) go_test( - name = "go_default_test", + name = "cgolib_with_build_tags_test", srcs = ["foo_test.go"], _gazelle_imports = ["testing"], - embed = [":go_default_library"], + embed = [":cgolib_with_build_tags"], ) diff --git a/language/go/testdata/default_visibility/BUILD.want b/language/go/testdata/default_visibility/BUILD.want index 324a907f9..199f1e6df 100644 --- a/language/go/testdata/default_visibility/BUILD.want +++ b/language/go/testdata/default_visibility/BUILD.want @@ -16,7 +16,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "default_visibility", srcs = ["lib.go"], _gazelle_imports = [], embed = [":default_visibility_go_proto"], @@ -24,8 +24,8 @@ go_library( ) go_test( - name = "go_default_test", + name = "default_visibility_test", srcs = ["a_test.go"], _gazelle_imports = [], - embed = [":go_default_library"], + embed = [":default_visibility"], ) diff --git a/language/go/testdata/default_visibility/cmd/BUILD.want b/language/go/testdata/default_visibility/cmd/BUILD.want index 0844ac38d..cd846f144 100644 --- a/language/go/testdata/default_visibility/cmd/BUILD.want +++ b/language/go/testdata/default_visibility/cmd/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_library( - name = "go_default_library", + name = "cmd_lib", srcs = ["main.go"], _gazelle_imports = [], importpath = "example.com/repo/default_visibility/cmd", @@ -10,5 +10,5 @@ go_library( go_binary( name = "cmd", _gazelle_imports = [], - embed = [":go_default_library"], + embed = [":cmd_lib"], ) diff --git a/language/go/testdata/filter_pbgo/BUILD.want b/language/go/testdata/filter_pbgo/BUILD.want index 42d584f4e..4f781df3c 100644 --- a/language/go/testdata/filter_pbgo/BUILD.want +++ b/language/go/testdata/filter_pbgo/BUILD.want @@ -18,7 +18,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "filter_pbgo", srcs = ["b.pb.go"], _gazelle_imports = [], embed = [":filter_pbgo_go_proto"], diff --git a/language/go/testdata/gen_and_exclude/BUILD.want b/language/go/testdata/gen_and_exclude/BUILD.want index 0cfb1a0ae..490a47ed6 100644 --- a/language/go/testdata/gen_and_exclude/BUILD.want +++ b/language/go/testdata/gen_and_exclude/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "gen_and_exclude", srcs = [ "gen.go", "gen_linux.go", diff --git a/language/go/testdata/importmap/BUILD.want b/language/go/testdata/importmap/BUILD.want index 32c02e68e..356e7e5fa 100644 --- a/language/go/testdata/importmap/BUILD.want +++ b/language/go/testdata/importmap/BUILD.want @@ -19,7 +19,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "importmap", srcs = ["extra.go"], _gazelle_imports = [], embed = [":hello_go_proto"], @@ -29,8 +29,8 @@ go_library( ) go_test( - name = "go_default_test", + name = "importmap_test", srcs = ["extra_test.go"], _gazelle_imports = [], - embed = [":go_default_library"], + embed = [":importmap"], ) diff --git a/language/go/testdata/lib/BUILD.want b/language/go/testdata/lib/BUILD.want index bb448d306..ad456bab9 100644 --- a/language/go/testdata/lib/BUILD.want +++ b/language/go/testdata/lib/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "lib", srcs = [ "asm.h", "asm.s", @@ -19,7 +19,7 @@ go_library( ) go_test( - name = "go_default_test", + name = "lib_test", srcs = [ "lib_external_test.go", "lib_test.go", @@ -28,5 +28,5 @@ go_test( "example.com/repo/lib", "testing", ], - embed = [":go_default_library"], + embed = [":lib"], ) diff --git a/language/go/testdata/lib/internal/deep/BUILD.want b/language/go/testdata/lib/internal/deep/BUILD.want index 604c0a47c..3e4c80217 100644 --- a/language/go/testdata/lib/internal/deep/BUILD.want +++ b/language/go/testdata/lib/internal/deep/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "deep", srcs = ["thought.go"], _gazelle_imports = [], importpath = "example.com/repo/lib/internal/deep", diff --git a/language/go/testdata/lib/internal/go_visibility/BUILD.want b/language/go/testdata/lib/internal/go_visibility/BUILD.want index b3b3242ca..8d6fc1776 100644 --- a/language/go/testdata/lib/internal/go_visibility/BUILD.want +++ b/language/go/testdata/lib/internal/go_visibility/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "go_visibility", srcs = ["thought.go"], _gazelle_imports = [], importpath = "example.com/repo/lib/internal/go_visibility", diff --git a/language/go/testdata/lib/relativeimporter/BUILD.want b/language/go/testdata/lib/relativeimporter/BUILD.want index a0254cbdd..5a8e9df52 100644 --- a/language/go/testdata/lib/relativeimporter/BUILD.want +++ b/language/go/testdata/lib/relativeimporter/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "relativeimporter", srcs = ["importer.go"], _gazelle_imports = [ "../internal/deep", diff --git a/language/go/testdata/main_test_only/BUILD.want b/language/go/testdata/main_test_only/BUILD.want index a6d5d6f67..7dd520bd9 100644 --- a/language/go/testdata/main_test_only/BUILD.want +++ b/language/go/testdata/main_test_only/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test") go_test( - name = "go_default_test", + name = "main_test_only_test", srcs = ["foo_test.go"], _gazelle_imports = [], ) diff --git a/language/go/testdata/naming_convention/import/bin/BUILD.old b/language/go/testdata/naming_convention/import/bin/BUILD.old new file mode 100644 index 000000000..9ded1d54c --- /dev/null +++ b/language/go/testdata/naming_convention/import/bin/BUILD.old @@ -0,0 +1 @@ +# gazelle:go_naming_convention import \ No newline at end of file diff --git a/language/go/testdata/naming_convention/import/bin/BUILD.want b/language/go/testdata/naming_convention/import/bin/BUILD.want new file mode 100644 index 000000000..361b8e8a6 --- /dev/null +++ b/language/go/testdata/naming_convention/import/bin/BUILD.want @@ -0,0 +1,29 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") + +go_library( + name = "bin_lib", + srcs = [ + "lib.go", + "main.go", + ], + _gazelle_imports = [ + "example.com/repo/naming_convention/import/bin", + "fmt", + ], + importpath = "example.com/repo/naming_convention/import/bin", + visibility = ["//visibility:private"], +) + +go_binary( + name = "bin", + _gazelle_imports = [], + embed = [":bin_lib"], + visibility = ["//visibility:public"], +) + +go_test( + name = "bin_test", + srcs = ["bin_test.go"], + _gazelle_imports = ["testing"], + embed = [":bin_lib"], +) diff --git a/language/go/testdata/naming_convention/import/bin/bin_test.go b/language/go/testdata/naming_convention/import/bin/bin_test.go new file mode 100644 index 000000000..233090048 --- /dev/null +++ b/language/go/testdata/naming_convention/import/bin/bin_test.go @@ -0,0 +1,26 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "testing" +) + +func TestAnswer(t *testing.T) { + if got, want := Answer(), 42; got != want { + t.Errorf("Answer() = %d; want %d", got, want) + } +} diff --git a/language/go/testdata/naming_convention/import/bin/lib.go b/language/go/testdata/naming_convention/import/bin/lib.go new file mode 100644 index 000000000..c9bdde1c2 --- /dev/null +++ b/language/go/testdata/naming_convention/import/bin/lib.go @@ -0,0 +1,21 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +// Answer returns the ultimate answer to life, the universe and everything. +func Answer() int { + return 42 +} diff --git a/language/go/testdata/naming_convention/import/bin/main.go b/language/go/testdata/naming_convention/import/bin/main.go new file mode 100644 index 000000000..a535e5a57 --- /dev/null +++ b/language/go/testdata/naming_convention/import/bin/main.go @@ -0,0 +1,25 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + "example.com/repo/naming_convention/import/bin" +) + +func main() { + fmt.Printf("Answer: %d", bin.Answer()) +} diff --git a/language/go/testdata/naming_convention/import/lib/BUILD.old b/language/go/testdata/naming_convention/import/lib/BUILD.old new file mode 100644 index 000000000..9ded1d54c --- /dev/null +++ b/language/go/testdata/naming_convention/import/lib/BUILD.old @@ -0,0 +1 @@ +# gazelle:go_naming_convention import \ No newline at end of file diff --git a/language/go/testdata/naming_convention/import/lib/BUILD.want b/language/go/testdata/naming_convention/import/lib/BUILD.want new file mode 100644 index 000000000..a9a7c5534 --- /dev/null +++ b/language/go/testdata/naming_convention/import/lib/BUILD.want @@ -0,0 +1,16 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "lib", + srcs = ["lib.go"], + _gazelle_imports = [], + importpath = "example.com/repo/naming_convention/import/lib", + visibility = ["//visibility:public"], +) + +go_test( + name = "lib_test", + srcs = ["lib_test.go"], + _gazelle_imports = ["testing"], + embed = [":lib"], +) diff --git a/language/go/testdata/naming_convention/import/lib/lib.go b/language/go/testdata/naming_convention/import/lib/lib.go new file mode 100644 index 000000000..9c41e7826 --- /dev/null +++ b/language/go/testdata/naming_convention/import/lib/lib.go @@ -0,0 +1,21 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lib + +// Answer returns the ultimate answer to life, the universe and everything. +func Answer() int { + return 42 +} diff --git a/language/go/testdata/naming_convention/import/lib/lib_test.go b/language/go/testdata/naming_convention/import/lib/lib_test.go new file mode 100644 index 000000000..d88361f18 --- /dev/null +++ b/language/go/testdata/naming_convention/import/lib/lib_test.go @@ -0,0 +1,26 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lib + +import ( + "testing" +) + +func TestAnswer(t *testing.T) { + if got, want := Answer(), 42; got != want { + t.Errorf("Answer() = %d; want %d", got, want) + } +} diff --git a/language/go/testdata/naming_convention/import_alias/bin/BUILD.old b/language/go/testdata/naming_convention/import_alias/bin/BUILD.old new file mode 100644 index 000000000..9ded1d54c --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/bin/BUILD.old @@ -0,0 +1 @@ +# gazelle:go_naming_convention import \ No newline at end of file diff --git a/language/go/testdata/naming_convention/import_alias/bin/BUILD.want b/language/go/testdata/naming_convention/import_alias/bin/BUILD.want new file mode 100644 index 000000000..2623e9a76 --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/bin/BUILD.want @@ -0,0 +1,29 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") + +go_library( + name = "bin_lib", + srcs = [ + "lib.go", + "main.go", + ], + _gazelle_imports = [ + "example.com/repo/naming_convention/import_alias/bin", + "fmt", + ], + importpath = "example.com/repo/naming_convention/import_alias/bin", + visibility = ["//visibility:private"], +) + +go_binary( + name = "bin", + _gazelle_imports = [], + embed = [":bin_lib"], + visibility = ["//visibility:public"], +) + +go_test( + name = "bin_test", + srcs = ["bin_test.go"], + _gazelle_imports = ["testing"], + embed = [":bin_lib"], +) diff --git a/language/go/testdata/naming_convention/import_alias/bin/bin_test.go b/language/go/testdata/naming_convention/import_alias/bin/bin_test.go new file mode 100644 index 000000000..233090048 --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/bin/bin_test.go @@ -0,0 +1,26 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "testing" +) + +func TestAnswer(t *testing.T) { + if got, want := Answer(), 42; got != want { + t.Errorf("Answer() = %d; want %d", got, want) + } +} diff --git a/language/go/testdata/naming_convention/import_alias/bin/lib.go b/language/go/testdata/naming_convention/import_alias/bin/lib.go new file mode 100644 index 000000000..c9bdde1c2 --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/bin/lib.go @@ -0,0 +1,21 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +// Answer returns the ultimate answer to life, the universe and everything. +func Answer() int { + return 42 +} diff --git a/language/go/testdata/naming_convention/import_alias/bin/main.go b/language/go/testdata/naming_convention/import_alias/bin/main.go new file mode 100644 index 000000000..d1f370bb3 --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/bin/main.go @@ -0,0 +1,25 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + "example.com/repo/naming_convention/import_alias/bin" +) + +func main() { + fmt.Printf("Answer: %d", bin.Answer()) +} diff --git a/language/go/testdata/naming_convention/import_alias/lib/BUILD.old b/language/go/testdata/naming_convention/import_alias/lib/BUILD.old new file mode 100644 index 000000000..388f23013 --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/lib/BUILD.old @@ -0,0 +1 @@ +# gazelle:go_naming_convention import_alias \ No newline at end of file diff --git a/language/go/testdata/naming_convention/import_alias/lib/BUILD.want b/language/go/testdata/naming_convention/import_alias/lib/BUILD.want new file mode 100644 index 000000000..64074ff2c --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/lib/BUILD.want @@ -0,0 +1,22 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "lib", + srcs = ["lib.go"], + _gazelle_imports = [], + importpath = "example.com/repo/naming_convention/import_alias/lib", + visibility = ["//visibility:public"], +) + +alias( + name = "go_default_library", + actual = ":lib", + visibility = ["//visibility:public"], +) + +go_test( + name = "lib_test", + srcs = ["lib_test.go"], + _gazelle_imports = ["testing"], + embed = [":lib"], +) diff --git a/language/go/testdata/naming_convention/import_alias/lib/lib.go b/language/go/testdata/naming_convention/import_alias/lib/lib.go new file mode 100644 index 000000000..f4a3c179d --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/lib/lib.go @@ -0,0 +1,23 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package libbbbb has a name that doens't match its import path. It shouldn't +// appear in the build file. +package libbbbb + +// Answer returns the ultimate answer to life, the universe and everything. +func Answer() int { + return 42 +} diff --git a/language/go/testdata/naming_convention/import_alias/lib/lib_test.go b/language/go/testdata/naming_convention/import_alias/lib/lib_test.go new file mode 100644 index 000000000..d0e0f5556 --- /dev/null +++ b/language/go/testdata/naming_convention/import_alias/lib/lib_test.go @@ -0,0 +1,26 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package libbbbb + +import ( + "testing" +) + +func TestAnswer(t *testing.T) { + if got, want := Answer(), 42; got != want { + t.Errorf("Answer() = %d; want %d", got, want) + } +} diff --git a/language/go/testdata/naming_convention/versions/v2/BUILD.old b/language/go/testdata/naming_convention/versions/v2/BUILD.old new file mode 100644 index 000000000..9ded1d54c --- /dev/null +++ b/language/go/testdata/naming_convention/versions/v2/BUILD.old @@ -0,0 +1 @@ +# gazelle:go_naming_convention import \ No newline at end of file diff --git a/language/go/testdata/naming_convention/versions/v2/BUILD.want b/language/go/testdata/naming_convention/versions/v2/BUILD.want new file mode 100644 index 000000000..181bc08a7 --- /dev/null +++ b/language/go/testdata/naming_convention/versions/v2/BUILD.want @@ -0,0 +1,16 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "versions", + srcs = ["lib.go"], + _gazelle_imports = [], + importpath = "example.com/repo/naming_convention/versions/v2", + visibility = ["//visibility:public"], +) + +go_test( + name = "versions_test", + srcs = ["lib_test.go"], + _gazelle_imports = ["testing"], + embed = [":versions"], +) diff --git a/language/go/testdata/naming_convention/versions/v2/lib.go b/language/go/testdata/naming_convention/versions/v2/lib.go new file mode 100644 index 000000000..d2a5305f4 --- /dev/null +++ b/language/go/testdata/naming_convention/versions/v2/lib.go @@ -0,0 +1,21 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package versions + +// Answer returns the ultimate answer to life, the universe and everything. +func Answer() int { + return 42 +} diff --git a/language/go/testdata/naming_convention/versions/v2/lib_test.go b/language/go/testdata/naming_convention/versions/v2/lib_test.go new file mode 100644 index 000000000..aead926f1 --- /dev/null +++ b/language/go/testdata/naming_convention/versions/v2/lib_test.go @@ -0,0 +1,26 @@ +/* Copyright 2020 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package versions + +import ( + "testing" +) + +func TestAnswer(t *testing.T) { + if got, want := Answer(), 42; got != want { + t.Errorf("Answer() = %d; want %d", got, want) + } +} diff --git a/language/go/testdata/platforms/BUILD.want b/language/go/testdata/platforms/BUILD.want index 10265e914..5198b5583 100644 --- a/language/go/testdata/platforms/BUILD.want +++ b/language/go/testdata/platforms/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "platforms", srcs = [ "cgo_generic.c", "cgo_generic.go", @@ -52,11 +52,11 @@ go_library( ) go_test( - name = "go_default_test", + name = "platforms_test", srcs = [ "generic_test.go", "suffix_linux_test.go", ], _gazelle_imports = [], - embed = [":go_default_library"], + embed = [":platforms"], ) diff --git a/language/go/testdata/proto_package_mode_extras/BUILD.want b/language/go/testdata/proto_package_mode_extras/BUILD.want index d23b2eaac..b4c2ae1b2 100644 --- a/language/go/testdata/proto_package_mode_extras/BUILD.want +++ b/language/go/testdata/proto_package_mode_extras/BUILD.want @@ -41,15 +41,15 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "proto_package_mode_extras", _gazelle_imports = [], embed = [":foo_go_proto"], importpath = "example.com/repo/proto_package_mode_extras", ) go_test( - name = "go_default_test", + name = "proto_package_mode_extras_test", srcs = ["foo_test.go"], _gazelle_imports = [], - embed = [":go_default_library"], + embed = [":proto_package_mode_extras"], ) diff --git a/language/go/testdata/protos/BUILD.want b/language/go/testdata/protos/BUILD.want index fb798e856..8251f96c7 100644 --- a/language/go/testdata/protos/BUILD.want +++ b/language/go/testdata/protos/BUILD.want @@ -24,7 +24,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "protos", srcs = ["extra.go"], _gazelle_imports = [], embed = [":protos_go_proto"], diff --git a/language/go/testdata/protos_explicit_default/BUILD.want b/language/go/testdata/protos_explicit_default/BUILD.want index bf3bb89c8..fa6560c51 100644 --- a/language/go/testdata/protos_explicit_default/BUILD.want +++ b/language/go/testdata/protos_explicit_default/BUILD.want @@ -25,7 +25,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "protos_explicit_default", srcs = ["extra.go"], _gazelle_imports = [], embed = [":protos_explicit_default_go_proto"], diff --git a/language/go/testdata/protos_gogo/BUILD.want b/language/go/testdata/protos_gogo/BUILD.want index 94ed66b29..f99c368ca 100644 --- a/language/go/testdata/protos_gogo/BUILD.want +++ b/language/go/testdata/protos_gogo/BUILD.want @@ -25,7 +25,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "protos_gogo", srcs = ["extra.go"], _gazelle_imports = [], embed = [":protos_gogo_go_proto"], diff --git a/language/go/testdata/protos_gogo_subdir_reset/BUILD.want b/language/go/testdata/protos_gogo_subdir_reset/BUILD.want index 110b916cd..b3528e5d5 100644 --- a/language/go/testdata/protos_gogo_subdir_reset/BUILD.want +++ b/language/go/testdata/protos_gogo_subdir_reset/BUILD.want @@ -22,7 +22,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "protos_gogo", _gazelle_imports = [], embed = [":protos_gogo_go_proto"], importpath = "example.com/repo/protos_gogo", diff --git a/language/go/testdata/protos_gogo_subdir_reset/sub/BUILD.want b/language/go/testdata/protos_gogo_subdir_reset/sub/BUILD.want index 12c9ff22d..6977c52f2 100644 --- a/language/go/testdata/protos_gogo_subdir_reset/sub/BUILD.want +++ b/language/go/testdata/protos_gogo_subdir_reset/sub/BUILD.want @@ -18,7 +18,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "protos_gogo", _gazelle_imports = [], embed = [":protos_gogo_go_proto"], importpath = "example.com/repo/protos_gogo", diff --git a/language/go/testdata/service/BUILD.want b/language/go/testdata/service/BUILD.want index 28afb1d9b..9c24d4cda 100644 --- a/language/go/testdata/service/BUILD.want +++ b/language/go/testdata/service/BUILD.want @@ -25,7 +25,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "service", srcs = ["extra.go"], _gazelle_imports = [], embed = [":service_go_proto"], diff --git a/language/go/testdata/service_gogo/BUILD.want b/language/go/testdata/service_gogo/BUILD.want index e547b2af5..c77511078 100644 --- a/language/go/testdata/service_gogo/BUILD.want +++ b/language/go/testdata/service_gogo/BUILD.want @@ -25,7 +25,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "service_gogo", srcs = ["extra.go"], _gazelle_imports = [], embed = [":service_gogo_go_proto"], diff --git a/language/go/testdata/service_gogo_subdir_reset/BUILD.want b/language/go/testdata/service_gogo_subdir_reset/BUILD.want index b8db28fdf..16c3a75df 100644 --- a/language/go/testdata/service_gogo_subdir_reset/BUILD.want +++ b/language/go/testdata/service_gogo_subdir_reset/BUILD.want @@ -22,7 +22,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "protos_gogo", _gazelle_imports = [], embed = [":protos_gogo_go_proto"], importpath = "example.com/repo/protos_gogo", diff --git a/language/go/testdata/service_gogo_subdir_reset/sub/BUILD.want b/language/go/testdata/service_gogo_subdir_reset/sub/BUILD.want index 9ee49e09b..1845fec8b 100644 --- a/language/go/testdata/service_gogo_subdir_reset/sub/BUILD.want +++ b/language/go/testdata/service_gogo_subdir_reset/sub/BUILD.want @@ -19,7 +19,7 @@ go_proto_library( ) go_library( - name = "go_default_library", + name = "protos_gogo", _gazelle_imports = [], embed = [":protos_gogo_go_proto"], importpath = "example.com/repo/protos_gogo", diff --git a/language/go/testdata/tests_import_testdata/BUILD.want b/language/go/testdata/tests_import_testdata/BUILD.want index aa6feff65..bf32ce8cc 100644 --- a/language/go/testdata/tests_import_testdata/BUILD.want +++ b/language/go/testdata/tests_import_testdata/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test") go_test( - name = "go_default_test", + name = "tests_import_testdata_test", srcs = [ "external_test.go", "internal_test.go", diff --git a/language/go/testdata/tests_with_testdata/BUILD.want b/language/go/testdata/tests_with_testdata/BUILD.want index 3a0dcd7e3..32b52f589 100644 --- a/language/go/testdata/tests_with_testdata/BUILD.want +++ b/language/go/testdata/tests_with_testdata/BUILD.want @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test") go_test( - name = "go_default_test", + name = "tests_with_testdata_test", srcs = [ "external_test.go", "internal_test.go", diff --git a/language/proto/BUILD.bazel b/language/proto/BUILD.bazel index 93f4fb929..680a22095 100644 --- a/language/proto/BUILD.bazel +++ b/language/proto/BUILD.bazel @@ -34,7 +34,7 @@ known_imports( ) go_library( - name = "go_default_library", + name = "proto", srcs = [ "config.go", "constants.go", @@ -42,8 +42,8 @@ go_library( "fix.go", "generate.go", "kinds.go", - "known_imports.go", "known_go_imports.go", + "known_imports.go", "known_proto_imports.go", "lang.go", "package.go", @@ -52,18 +52,18 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/language/proto", visibility = ["//visibility:public"], deps = [ - "//config:go_default_library", - "//label:go_default_library", - "//language:go_default_library", - "//pathtools:go_default_library", - "//repo:go_default_library", - "//resolve:go_default_library", - "//rule:go_default_library", + "//config", + "//label", + "//language", + "//pathtools", + "//repo", + "//resolve", + "//rule", ], ) go_test( - name = "go_default_test", + name = "proto_test", srcs = [ "config_test.go", "fileinfo_test.go", @@ -71,17 +71,17 @@ go_test( "resolve_test.go", ], data = glob(["testdata/**"]), - embed = [":go_default_library"], + embed = [":proto"], deps = [ - "//config:go_default_library", - "//label:go_default_library", - "//language:go_default_library", - "//merger:go_default_library", - "//repo:go_default_library", - "//resolve:go_default_library", - "//rule:go_default_library", - "//testtools:go_default_library", - "//walk:go_default_library", + "//config", + "//label", + "//language", + "//merger", + "//repo", + "//resolve", + "//rule", + "//testtools", + "//walk", "@com_github_bazelbuild_buildtools//build:go_default_library", ], ) @@ -102,8 +102,8 @@ filegroup( "generate.go", "generate_test.go", "kinds.go", - "known_imports.go", "known_go_imports.go", + "known_imports.go", "known_proto_imports.go", "lang.go", "package.go", @@ -114,3 +114,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":proto", + visibility = ["//visibility:public"], +) diff --git a/language/proto/gen/BUILD.bazel b/language/proto/gen/BUILD.bazel index 6c6a95691..b334f9dcf 100644 --- a/language/proto/gen/BUILD.bazel +++ b/language/proto/gen/BUILD.bazel @@ -1,16 +1,16 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_library( - name = "go_default_library", + name = "gen_lib", srcs = ["gen_known_imports.go"], importpath = "github.com/bazelbuild/bazel-gazelle/language/proto/gen", visibility = ["//visibility:private"], - deps = ["//label:go_default_library"], + deps = ["//label"], ) go_binary( name = "gen_known_imports", - embed = [":go_default_library"], + embed = [":gen_lib"], visibility = ["//:__subpackages__"], ) diff --git a/merger/BUILD.bazel b/merger/BUILD.bazel index 85087079f..5962485bf 100644 --- a/merger/BUILD.bazel +++ b/merger/BUILD.bazel @@ -1,26 +1,26 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "merger", srcs = [ "fix.go", "merger.go", ], importpath = "github.com/bazelbuild/bazel-gazelle/merger", visibility = ["//visibility:public"], - deps = ["//rule:go_default_library"], + deps = ["//rule"], ) go_test( - name = "go_default_test", + name = "merger_test", size = "small", srcs = ["merger_test.go"], - embed = [":go_default_library"], + embed = [":merger"], deps = [ - "//language:go_default_library", - "//language/go:go_default_library", - "//language/proto:go_default_library", - "//rule:go_default_library", + "//language", + "//language/go", + "//language/proto", + "//rule", ], ) @@ -35,3 +35,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":merger", + visibility = ["//visibility:public"], +) diff --git a/pathtools/BUILD.bazel b/pathtools/BUILD.bazel index acead21fd..bb31f8eaa 100644 --- a/pathtools/BUILD.bazel +++ b/pathtools/BUILD.bazel @@ -1,16 +1,16 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "pathtools", srcs = ["path.go"], importpath = "github.com/bazelbuild/bazel-gazelle/pathtools", visibility = ["//visibility:public"], ) go_test( - name = "go_default_test", + name = "pathtools_test", srcs = ["path_test.go"], - embed = [":go_default_library"], + embed = [":pathtools"], ) filegroup( @@ -23,3 +23,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":pathtools", + visibility = ["//visibility:public"], +) diff --git a/repo/BUILD.bazel b/repo/BUILD.bazel index 1030a2f8e..bdf4b2a8b 100644 --- a/repo/BUILD.bazel +++ b/repo/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "repo", srcs = [ "remote.go", "repo.go", @@ -9,25 +9,25 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/repo", visibility = ["//visibility:public"], deps = [ - "//label:go_default_library", - "//pathtools:go_default_library", - "//rule:go_default_library", + "//label", + "//pathtools", + "//rule", "@org_golang_x_tools//go/vcs:go_default_library", ], ) go_test( - name = "go_default_test", + name = "repo_test", srcs = [ "remote_test.go", "repo_test.go", "stubs_test.go", ], - embed = [":go_default_library"], + embed = [":repo"], deps = [ - "//pathtools:go_default_library", - "//rule:go_default_library", - "//testtools:go_default_library", + "//pathtools", + "//rule", + "//testtools", "@org_golang_x_tools//go/vcs:go_default_library", ], ) @@ -45,3 +45,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":repo", + visibility = ["//visibility:public"], +) diff --git a/repository.rst b/repository.rst index 4fd1b7895..bda697f93 100644 --- a/repository.rst +++ b/repository.rst @@ -142,6 +142,14 @@ returned by ``go env GOPATH``. | | | A value for ``sum`` may be found in the ``go.sum`` file or by running | | ``go mod download -json @``. | ++-----------------------------------+----------------------+------------------------------------------------------------+ +| :param:`build_naming_convention` | :type:`string` | :value:`""` | ++--------------------------------+----------------------+---------------------------------------------------------------+ +| Sets the library naming convention to use when resolving dependencies against this external | +| repository. If unset, the convention from the external workspace is used. | +| Legal values are ``go_default_library``, ``import``, and ``import_alias``. | +| | +| See ``-go_naming_convention`` for more information. | +--------------------------------+----------------------+---------------------------------------------------------------+ | :param:`replace` | :type:`string` | :value:`""` | +--------------------------------+----------------------+---------------------------------------------------------------+ diff --git a/resolve/BUILD.bazel b/resolve/BUILD.bazel index fda379b19..02936e246 100644 --- a/resolve/BUILD.bazel +++ b/resolve/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "resolve", srcs = [ "config.go", "index.go", @@ -9,10 +9,10 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/resolve", visibility = ["//visibility:public"], deps = [ - "//config:go_default_library", - "//label:go_default_library", - "//repo:go_default_library", - "//rule:go_default_library", + "//config", + "//label", + "//repo", + "//rule", ], ) @@ -26,3 +26,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":resolve", + visibility = ["//visibility:public"], +) diff --git a/rule/BUILD.bazel b/rule/BUILD.bazel index dfa746b38..33700e282 100644 --- a/rule/BUILD.bazel +++ b/rule/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "rule", srcs = [ "directives.go", "expr.go", @@ -16,19 +16,19 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/rule", visibility = ["//visibility:public"], deps = [ - "//label:go_default_library", + "//label", "@com_github_bazelbuild_buildtools//build:go_default_library", "@com_github_bazelbuild_buildtools//tables:go_default_library", ], ) go_test( - name = "go_default_test", + name = "rule_test", srcs = [ "directives_test.go", "rule_test.go", ], - embed = [":go_default_library"], + embed = [":rule"], deps = ["@com_github_bazelbuild_buildtools//build:go_default_library"], ) @@ -51,3 +51,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":rule", + visibility = ["//visibility:public"], +) diff --git a/testtools/BUILD.bazel b/testtools/BUILD.bazel index db10c2be3..bc3495030 100644 --- a/testtools/BUILD.bazel +++ b/testtools/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( - name = "go_default_library", + name = "testtools", testonly = True, srcs = [ "config.go", @@ -10,9 +10,9 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/testtools", visibility = ["//visibility:public"], deps = [ - "//config:go_default_library", - "//language:go_default_library", - "@com_github_google_go_cmp//cmp:go_default_library", + "//config", + "//language", + "@com_github_google_go_cmp//cmp", ], ) @@ -26,3 +26,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":testtools", + visibility = ["//visibility:public"], +) diff --git a/walk/BUILD.bazel b/walk/BUILD.bazel index c48c1762f..7afa481df 100644 --- a/walk/BUILD.bazel +++ b/walk/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( - name = "go_default_library", + name = "walk", srcs = [ "config.go", "walk.go", @@ -9,26 +9,26 @@ go_library( importpath = "github.com/bazelbuild/bazel-gazelle/walk", visibility = ["//visibility:public"], deps = [ - "//config:go_default_library", - "//flag:go_default_library", - "//pathtools:go_default_library", - "//rule:go_default_library", - "@com_github_bmatcuk_doublestar//:go_default_library", + "//config", + "//flag", + "//pathtools", + "//rule", + "@com_github_bmatcuk_doublestar//:doublestar", ], ) go_test( - name = "go_default_test", + name = "walk_test", srcs = [ "config_test.go", "walk_test.go", ], - embed = [":go_default_library"], + embed = [":walk"], deps = [ - "//config:go_default_library", - "//rule:go_default_library", - "//testtools:go_default_library", - "@com_github_bmatcuk_doublestar//:go_default_library", + "//config", + "//rule", + "//testtools", + "@com_github_bmatcuk_doublestar//:doublestar", ], ) @@ -44,3 +44,9 @@ filegroup( ], visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":walk", + visibility = ["//visibility:public"], +)