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..a0b8aa6 100644 --- a/gazelle/bzl/gazelle.go +++ b/gazelle/bzl/gazelle.go @@ -152,23 +152,34 @@ 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 != "" || !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, + Imp: impLabel.String(), } matches := ix.FindRulesByImport(res, languageName) 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) + 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 { - deps = append(deps, m.Label.String()) + depLabel := m.Label + depLabel = depLabel.Rel(from.Repo, from.Pkg) + deps = append(deps, depLabel.String()) } } } 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/relative_import/BUILD.in b/gazelle/bzl/testdata/relative_import/BUILD.in new file mode 100644 index 0000000..e69de29 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 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 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 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()