Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language/go: several naming convention fixes plus auto-detection #842

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
124 changes: 91 additions & 33 deletions cmd/gazelle/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,24 +524,24 @@ 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"],
)
`,
}, {
Path: "vendor/golang.org/x/bar/" + config.DefaultValidBuildFileNames[0],
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"],
)
`,
},
Expand Down Expand Up @@ -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"],
)
`,
}})
Expand Down Expand Up @@ -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",
Expand All @@ -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"],
)
`,
},
Expand Down Expand Up @@ -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"],
)
`,
},
Expand Down Expand Up @@ -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"),
Expand All @@ -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"],
)
`,
},
Expand Down Expand Up @@ -2246,7 +2253,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"],
Expand All @@ -2272,11 +2279,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"],
)
`,
}})
Expand Down Expand Up @@ -2311,7 +2318,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"],
Expand Down Expand Up @@ -2342,11 +2349,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"],
)
`,
},
Expand Down Expand Up @@ -2394,13 +2401,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",
],
)
`,
Expand Down Expand Up @@ -2564,8 +2571,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`,
Expand Down Expand Up @@ -2654,6 +2664,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",
Expand Down Expand Up @@ -2820,7 +2831,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"],
Expand All @@ -2833,7 +2844,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"],
Expand Down Expand Up @@ -2880,7 +2891,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 = [
Expand All @@ -2890,9 +2901,9 @@ go_library(
)

go_test(
name = "go_default_test",
name = "version_test",
srcs = ["version_test.go"],
embed = [":go_default_library"],
embed = [":version"],
)
`,
}})
Expand Down Expand Up @@ -3101,7 +3112,7 @@ go_proto_library(
)

go_library(
name = "go_default_library",
name = "foo",
embed = [":foo_go_proto"],
importpath = "example.com/foo",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -3159,7 +3170,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
# gazelle:lang

go_library(
name = "go_default_library",
name = "bar",
srcs = ["bar.go"],
importpath = "",
visibility = ["//visibility:public"],
Expand All @@ -3173,7 +3184,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
# gazelle:lang go,proto

go_library(
name = "go_default_library",
name = "baz",
srcs = ["baz.go"],
importpath = "",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -3241,11 +3252,58 @@ proto_library(
)

go_library(
name = "go_default_library",
name = "proto",
srcs = ["foo.pb.go"],
importpath = "example.com/proto",
visibility = ["//visibility:public"],
)`,
},
})
}

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: "",
},
})
}
Loading