Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gazelle handling relative imports #271

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()