Skip to content

Commit

Permalink
language/go: merge new Go naming convention to master (#863)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jay Conrod authored Aug 19, 2020
1 parent aa42a21 commit 29343aa
Show file tree
Hide file tree
Showing 97 changed files with 1,937 additions and 341 deletions.
7 changes: 4 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
],
)

Expand Down
35 changes: 35 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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. |
Expand Down
10 changes: 10 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 12 additions & 12 deletions cmd/autogazelle/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -11,42 +11,42 @@ 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": [],
}),
)

go_binary(
name = "autogazelle",
embed = [":go_default_library"],
embed = [":autogazelle_lib"],
visibility = ["//visibility:public"],
)

Expand Down
8 changes: 4 additions & 4 deletions cmd/fetch_repo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"],
)

Expand Down
38 changes: 19 additions & 19 deletions cmd/gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ gazelle_binary(
)

go_library(
name = "go_default_library",
name = "gazelle_lib",
# keep
srcs = [
"diff.go",
Expand All @@ -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",
Expand All @@ -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",
],
)
Expand Down
6 changes: 3 additions & 3 deletions cmd/gazelle/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down
12 changes: 6 additions & 6 deletions cmd/gazelle/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -291,15 +291,15 @@ 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"],
)
go_binary(
name = "repo",
embed = [":go_default_library"],
embed = [":repo_lib"],
visibility = ["//visibility:public"],
)
`,
Expand All @@ -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"],
Expand Down
Loading

0 comments on commit 29343aa

Please sign in to comment.