aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Giddins <segiddins@segiddins.me>2020-09-07 14:29:39 -0700
committerGitHub <noreply@github.com>2020-09-07 15:29:39 -0600
commit836f1b2f564e8952a9b1ae72f66fc9fad8c8e6f1 (patch)
treeb580324750be7398f89e3c3d6852edba7a092dc3
parent2ec2e6d715e993d96ad6222770805b5bd25399ae (diff)
downloadbazel-skylib-836f1b2f564e8952a9b1ae72f66fc9fad8c8e6f1.tar.gz
Add support for gazelle handling relative imports (#271)
-rw-r--r--gazelle/bzl/BUILD2
-rw-r--r--gazelle/bzl/gazelle.go19
-rw-r--r--gazelle/bzl/testdata/import/BUILD.out2
-rw-r--r--gazelle/bzl/testdata/relative_import/BUILD.in0
-rw-r--r--gazelle/bzl/testdata/relative_import/BUILD.out14
-rw-r--r--gazelle/bzl/testdata/relative_import/WORKSPACE0
-rw-r--r--gazelle/bzl/testdata/relative_import/bar.bzl6
-rw-r--r--gazelle/bzl/testdata/relative_import/foo.bzl7
-rw-r--r--gazelle/bzl/testdata/relative_import/nested/dir/BUILD.in0
-rw-r--r--gazelle/bzl/testdata/relative_import/nested/dir/BUILD.out14
-rw-r--r--gazelle/bzl/testdata/relative_import/nested/dir/bar.bzl6
-rw-r--r--gazelle/bzl/testdata/relative_import/nested/dir/foo.bzl7
-rw-r--r--gazelle/bzl/testdata/workspace_name/BUILD.in0
-rw-r--r--gazelle/bzl/testdata/workspace_name/BUILD.out14
-rw-r--r--gazelle/bzl/testdata/workspace_name/WORKSPACE1
-rw-r--r--gazelle/bzl/testdata/workspace_name/bar.bzl6
-rw-r--r--gazelle/bzl/testdata/workspace_name/foo.bzl7
17 files changed, 99 insertions, 6 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..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
--- /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()