Add support for gazelle handling relative imports (#271)

This commit is contained in:
Samuel Giddins 2020-09-07 14:29:39 -07:00 committed by GitHub
parent 2ec2e6d715
commit 836f1b2f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 99 additions and 6 deletions

View File

@ -49,5 +49,5 @@ gazelle_binary(
gazelle( gazelle(
name = "gazelle", name = "gazelle",
gazelle = "//gazelle:gazelle-skylib", gazelle = ":gazelle-skylib",
) )

View File

@ -152,23 +152,34 @@ func (*bzlLibraryLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo
deps := make([]string, 0, len(imports)) deps := make([]string, 0, len(imports))
for _, imp := range 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 // 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. // is disabled so take a guess at what hte target name should be.
deps = append(deps, strings.TrimSuffix(imp, fileType)) deps = append(deps, strings.TrimSuffix(imp, fileType))
} else { } else {
res := resolve.ImportSpec{ res := resolve.ImportSpec{
Lang: languageName, Lang: languageName,
Imp: imp, Imp: impLabel.String(),
} }
matches := ix.FindRulesByImport(res, languageName) matches := ix.FindRulesByImport(res, languageName)
if len(matches) == 0 { 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 { 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())
} }
} }
} }

View File

@ -10,5 +10,5 @@ bzl_library(
name = "foo", name = "foo",
srcs = ["foo.bzl"], srcs = ["foo.bzl"],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = ["//:bar"], deps = [":bar"],
) )

View File

View File

@ -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"],
)

View File

View File

@ -0,0 +1,6 @@
"""
Doc string
"""
def func():
pass

View File

@ -0,0 +1,7 @@
"""
Doc string
"""
load(":bar.bzl", "func")
func()

View File

@ -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"],
)

View File

@ -0,0 +1,6 @@
"""
Doc string
"""
def func():
pass

View File

@ -0,0 +1,7 @@
"""
Doc string
"""
load(":bar.bzl", "func")
func()

View File

View File

@ -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"],
)

View File

@ -0,0 +1 @@
workspace(name = "com_example")

View File

@ -0,0 +1,6 @@
"""
Doc string
"""
def func():
pass

View File

@ -0,0 +1,7 @@
"""
Doc string
"""
load("//:bar.bzl", "func")
func()