Skip to content

Commit

Permalink
Add support for gazelle handling relative imports (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Sep 7, 2020
1 parent 2ec2e6d commit 836f1b2
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gazelle/bzl/BUILD
Expand Up @@ -49,5 +49,5 @@ gazelle_binary(

gazelle(
name = "gazelle",
gazelle = "//gazelle:gazelle-skylib",
gazelle = ":gazelle-skylib",
)
19 changes: 15 additions & 4 deletions gazelle/bzl/gazelle.go
Expand Up @@ -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())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gazelle/bzl/testdata/import/BUILD.out
Expand Up @@ -10,5 +10,5 @@ bzl_library(
name = "foo",
srcs = ["foo.bzl"],
visibility = ["//visibility:public"],
deps = ["//:bar"],
deps = [":bar"],
)
Empty file.
14 changes: 14 additions & 0 deletions 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"],
)
Empty file.
6 changes: 6 additions & 0 deletions gazelle/bzl/testdata/relative_import/bar.bzl
@@ -0,0 +1,6 @@
"""
Doc string
"""

def func():
pass
7 changes: 7 additions & 0 deletions gazelle/bzl/testdata/relative_import/foo.bzl
@@ -0,0 +1,7 @@
"""
Doc string
"""

load(":bar.bzl", "func")

func()
Empty file.
14 changes: 14 additions & 0 deletions 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"],
)
6 changes: 6 additions & 0 deletions gazelle/bzl/testdata/relative_import/nested/dir/bar.bzl
@@ -0,0 +1,6 @@
"""
Doc string
"""

def func():
pass
7 changes: 7 additions & 0 deletions gazelle/bzl/testdata/relative_import/nested/dir/foo.bzl
@@ -0,0 +1,7 @@
"""
Doc string
"""

load(":bar.bzl", "func")

func()
Empty file.
14 changes: 14 additions & 0 deletions 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"],
)
1 change: 1 addition & 0 deletions gazelle/bzl/testdata/workspace_name/WORKSPACE
@@ -0,0 +1 @@
workspace(name = "com_example")
6 changes: 6 additions & 0 deletions gazelle/bzl/testdata/workspace_name/bar.bzl
@@ -0,0 +1,6 @@
"""
Doc string
"""

def func():
pass
7 changes: 7 additions & 0 deletions gazelle/bzl/testdata/workspace_name/foo.bzl
@@ -0,0 +1,7 @@
"""
Doc string
"""

load("//:bar.bzl", "func")

func()

0 comments on commit 836f1b2

Please sign in to comment.