diff options
Diffstat (limited to 'gazelle/bzl')
26 files changed, 194 insertions, 20 deletions
diff --git a/gazelle/bzl/BUILD b/gazelle/bzl/BUILD index fea1d51..29caeb0 100644 --- a/gazelle/bzl/BUILD +++ b/gazelle/bzl/BUILD @@ -49,5 +49,5 @@ gazelle_binary( gazelle( name = "gazelle", - gazelle = "//gazelle:gazelle-skylib", + gazelle = ":gazelle-skylib", ) diff --git a/gazelle/bzl/gazelle.go b/gazelle/bzl/gazelle.go index f948390..dccec27 100644 --- a/gazelle/bzl/gazelle.go +++ b/gazelle/bzl/gazelle.go @@ -152,24 +152,46 @@ func (*bzlLibraryLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo deps := make([]string, 0, len(imports)) for _, imp := range imports { - if strings.HasPrefix(imp, "@") || !c.IndexLibraries { + impLabel, err := label.Parse(imp) + if err != nil { + log.Printf("%s: import of %q is invalid: %v", from.String(), imp, err) + continue + } + + // the index only contains absolute labels, not relative + impLabel = impLabel.Abs(from.Repo, from.Pkg) + + if impLabel.Repo == "bazel_tools" { + // The @bazel_tools repo is tricky because it is a part of the "shipped + // with bazel" core library for interacting with the outside world. + // This means that it can not depend on skylib. Fortunately there is a + // fairly simple workaround for this, which is that you can add those + // bzl files as `deps` entries. + deps = append(deps, imp) + continue + } + + if impLabel.Repo != "" || !c.IndexLibraries { // This is a dependency that is external to the current repo, or indexing // is disabled so take a guess at what hte target name should be. deps = append(deps, strings.TrimSuffix(imp, fileType)) - } else { - res := resolve.ImportSpec{ - Lang: languageName, - Imp: imp, - } - matches := ix.FindRulesByImport(res, languageName) + continue + } - if len(matches) == 0 { - log.Printf("%s: %q was not found in dependency index. Skipping. This may result in an incomplete deps section and require manual BUILD file intervention.\n", from.String(), imp) - } + res := resolve.ImportSpec{ + Lang: languageName, + Imp: impLabel.String(), + } + matches := ix.FindRulesByImport(res, languageName) - for _, m := range matches { - deps = append(deps, m.Label.String()) - } + if len(matches) == 0 { + log.Printf("%s: %q (%s) was not found in dependency index. Skipping. This may result in an incomplete deps section and require manual BUILD file intervention.\n", from.String(), imp, impLabel.String()) + } + + for _, m := range matches { + depLabel := m.Label + depLabel = depLabel.Rel(from.Repo, from.Pkg) + deps = append(deps, depLabel.String()) } } @@ -211,11 +233,10 @@ func (*bzlLibraryLang) GenerateRules(args language.GenerateArgs) language.Genera r.SetAttr("srcs", []string{f}) - if args.File == nil || !args.File.HasDefaultVisibility() { - inPrivateDir := pathtools.Index(args.Rel, "private") >= 0 - if !inPrivateDir { - r.SetAttr("visibility", []string{"//visibility:public"}) - } + shouldSetVisibility := args.File == nil || !args.File.HasDefaultVisibility() + if shouldSetVisibility { + vis := checkInternalVisibility(args.Rel, "//visibility:public") + r.SetAttr("visibility", []string{vis}) } fullPath := filepath.Join(args.Dir, f) @@ -313,3 +334,16 @@ func (s srcsList) Contains(m string) bool { } return false } + +// checkInternalVisibility overrides the given visibility if the package is +// internal. +func checkInternalVisibility(rel, visibility string) string { + if i := pathtools.Index(rel, "internal"); i > 0 { + visibility = fmt.Sprintf("//%s:__subpackages__", rel[:i-1]) + } else if i := pathtools.Index(rel, "private"); i > 0 { + visibility = fmt.Sprintf("//%s:__subpackages__", rel[:i-1]) + } else if pathtools.HasPrefix(rel, "internal") || pathtools.HasPrefix(rel, "private") { + visibility = "//:__subpackages__" + } + return visibility +} diff --git a/gazelle/bzl/testdata/bazel_tools/BUILD.in b/gazelle/bzl/testdata/bazel_tools/BUILD.in new file mode 100644 index 0000000..e267b5a --- /dev/null +++ b/gazelle/bzl/testdata/bazel_tools/BUILD.in @@ -0,0 +1,6 @@ +# Some comment to be preserved + +filegroup( + name = "allfiles", + srcs = glob(["**"]), +) diff --git a/gazelle/bzl/testdata/bazel_tools/BUILD.out b/gazelle/bzl/testdata/bazel_tools/BUILD.out new file mode 100644 index 0000000..576cfad --- /dev/null +++ b/gazelle/bzl/testdata/bazel_tools/BUILD.out @@ -0,0 +1,15 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +# Some comment to be preserved + +filegroup( + name = "allfiles", + srcs = glob(["**"]), +) + +bzl_library( + name = "foo", + srcs = ["foo.bzl"], + visibility = ["//visibility:public"], + deps = ["@bazel_tools//tools/build_defs/repo:http.bzl"], +) diff --git a/gazelle/bzl/testdata/bazel_tools/WORKSPACE b/gazelle/bzl/testdata/bazel_tools/WORKSPACE new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/gazelle/bzl/testdata/bazel_tools/WORKSPACE diff --git a/gazelle/bzl/testdata/bazel_tools/foo.bzl b/gazelle/bzl/testdata/bazel_tools/foo.bzl new file mode 100644 index 0000000..b025d21 --- /dev/null +++ b/gazelle/bzl/testdata/bazel_tools/foo.bzl @@ -0,0 +1,10 @@ +""" +Doc string +""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def wrapped_http_archive(**kwargs): + http_archive( + **kwargs + ) diff --git a/gazelle/bzl/testdata/import/BUILD.out b/gazelle/bzl/testdata/import/BUILD.out index a19a11a..2d39feb 100644 --- a/gazelle/bzl/testdata/import/BUILD.out +++ b/gazelle/bzl/testdata/import/BUILD.out @@ -10,5 +10,5 @@ bzl_library( name = "foo", srcs = ["foo.bzl"], visibility = ["//visibility:public"], - deps = ["//:bar"], + deps = [":bar"], ) diff --git a/gazelle/bzl/testdata/private/nested/private/BUILD.out b/gazelle/bzl/testdata/private/nested/private/BUILD.out new file mode 100644 index 0000000..442401c --- /dev/null +++ b/gazelle/bzl/testdata/private/nested/private/BUILD.out @@ -0,0 +1,7 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +bzl_library( + name = "bar", + srcs = ["bar.bzl"], + visibility = ["//nested:__subpackages__"], +) diff --git a/gazelle/bzl/testdata/private/nested/private/bar.bzl b/gazelle/bzl/testdata/private/nested/private/bar.bzl new file mode 100644 index 0000000..c8a4871 --- /dev/null +++ b/gazelle/bzl/testdata/private/nested/private/bar.bzl @@ -0,0 +1,6 @@ +""" +Test sample code. +""" + +def func(): + pass diff --git a/gazelle/bzl/testdata/private/nested/private/inside/internal/BUILD.out b/gazelle/bzl/testdata/private/nested/private/inside/internal/BUILD.out new file mode 100644 index 0000000..afaf8f3 --- /dev/null +++ b/gazelle/bzl/testdata/private/nested/private/inside/internal/BUILD.out @@ -0,0 +1,7 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +bzl_library( + name = "bar", + srcs = ["bar.bzl"], + visibility = ["//nested/private/inside:__subpackages__"], +) diff --git a/gazelle/bzl/testdata/private/nested/private/inside/internal/bar.bzl b/gazelle/bzl/testdata/private/nested/private/inside/internal/bar.bzl new file mode 100644 index 0000000..c8a4871 --- /dev/null +++ b/gazelle/bzl/testdata/private/nested/private/inside/internal/bar.bzl @@ -0,0 +1,6 @@ +""" +Test sample code. +""" + +def func(): + pass diff --git a/gazelle/bzl/testdata/private/private/BUILD.out b/gazelle/bzl/testdata/private/private/BUILD.out index eb2e935..f442a99 100644 --- a/gazelle/bzl/testdata/private/private/BUILD.out +++ b/gazelle/bzl/testdata/private/private/BUILD.out @@ -3,4 +3,5 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") bzl_library( name = "bar", srcs = ["bar.bzl"], + visibility = ["//:__subpackages__"], ) diff --git a/gazelle/bzl/testdata/relative_import/BUILD.in b/gazelle/bzl/testdata/relative_import/BUILD.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/BUILD.in diff --git a/gazelle/bzl/testdata/relative_import/BUILD.out b/gazelle/bzl/testdata/relative_import/BUILD.out new file mode 100644 index 0000000..2d39feb --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/BUILD.out @@ -0,0 +1,14 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +bzl_library( + name = "bar", + srcs = ["bar.bzl"], + visibility = ["//visibility:public"], +) + +bzl_library( + name = "foo", + srcs = ["foo.bzl"], + visibility = ["//visibility:public"], + deps = [":bar"], +) diff --git a/gazelle/bzl/testdata/relative_import/WORKSPACE b/gazelle/bzl/testdata/relative_import/WORKSPACE new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/WORKSPACE diff --git a/gazelle/bzl/testdata/relative_import/bar.bzl b/gazelle/bzl/testdata/relative_import/bar.bzl new file mode 100644 index 0000000..7ffea51 --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/bar.bzl @@ -0,0 +1,6 @@ +""" +Doc string +""" + +def func(): + pass diff --git a/gazelle/bzl/testdata/relative_import/foo.bzl b/gazelle/bzl/testdata/relative_import/foo.bzl new file mode 100644 index 0000000..829cce3 --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/foo.bzl @@ -0,0 +1,7 @@ +""" +Doc string +""" + +load(":bar.bzl", "func") + +func() diff --git a/gazelle/bzl/testdata/relative_import/nested/dir/BUILD.in b/gazelle/bzl/testdata/relative_import/nested/dir/BUILD.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/nested/dir/BUILD.in diff --git a/gazelle/bzl/testdata/relative_import/nested/dir/BUILD.out b/gazelle/bzl/testdata/relative_import/nested/dir/BUILD.out new file mode 100644 index 0000000..2d39feb --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/nested/dir/BUILD.out @@ -0,0 +1,14 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +bzl_library( + name = "bar", + srcs = ["bar.bzl"], + visibility = ["//visibility:public"], +) + +bzl_library( + name = "foo", + srcs = ["foo.bzl"], + visibility = ["//visibility:public"], + deps = [":bar"], +) diff --git a/gazelle/bzl/testdata/relative_import/nested/dir/bar.bzl b/gazelle/bzl/testdata/relative_import/nested/dir/bar.bzl new file mode 100644 index 0000000..7ffea51 --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/nested/dir/bar.bzl @@ -0,0 +1,6 @@ +""" +Doc string +""" + +def func(): + pass diff --git a/gazelle/bzl/testdata/relative_import/nested/dir/foo.bzl b/gazelle/bzl/testdata/relative_import/nested/dir/foo.bzl new file mode 100644 index 0000000..829cce3 --- /dev/null +++ b/gazelle/bzl/testdata/relative_import/nested/dir/foo.bzl @@ -0,0 +1,7 @@ +""" +Doc string +""" + +load(":bar.bzl", "func") + +func() diff --git a/gazelle/bzl/testdata/workspace_name/BUILD.in b/gazelle/bzl/testdata/workspace_name/BUILD.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/gazelle/bzl/testdata/workspace_name/BUILD.in diff --git a/gazelle/bzl/testdata/workspace_name/BUILD.out b/gazelle/bzl/testdata/workspace_name/BUILD.out new file mode 100644 index 0000000..2d39feb --- /dev/null +++ b/gazelle/bzl/testdata/workspace_name/BUILD.out @@ -0,0 +1,14 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +bzl_library( + name = "bar", + srcs = ["bar.bzl"], + visibility = ["//visibility:public"], +) + +bzl_library( + name = "foo", + srcs = ["foo.bzl"], + visibility = ["//visibility:public"], + deps = [":bar"], +) diff --git a/gazelle/bzl/testdata/workspace_name/WORKSPACE b/gazelle/bzl/testdata/workspace_name/WORKSPACE new file mode 100644 index 0000000..2fa2441 --- /dev/null +++ b/gazelle/bzl/testdata/workspace_name/WORKSPACE @@ -0,0 +1 @@ +workspace(name = "com_example") diff --git a/gazelle/bzl/testdata/workspace_name/bar.bzl b/gazelle/bzl/testdata/workspace_name/bar.bzl new file mode 100644 index 0000000..7ffea51 --- /dev/null +++ b/gazelle/bzl/testdata/workspace_name/bar.bzl @@ -0,0 +1,6 @@ +""" +Doc string +""" + +def func(): + pass diff --git a/gazelle/bzl/testdata/workspace_name/foo.bzl b/gazelle/bzl/testdata/workspace_name/foo.bzl new file mode 100644 index 0000000..eb8d33c --- /dev/null +++ b/gazelle/bzl/testdata/workspace_name/foo.bzl @@ -0,0 +1,7 @@ +""" +Doc string +""" + +load("//:bar.bzl", "func") + +func() |