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

Gazelle now handles imports from @bazel_tools #273

Merged
merged 2 commits into from Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 25 additions & 14 deletions gazelle/bzl/gazelle.go
Expand Up @@ -161,26 +161,37 @@ func (*bzlLibraryLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo
// the index only contains absolute labels, not relative
impLabel = impLabel.Abs(from.Repo, from.Pkg)

if impLabel.Repo == "bazel_tools" {
// The @bazel_tools repo is tricky because it is a part of the "shipped
// with bazel" core library for interacting with the outside world.
// This means that it can not depend on skylib. Fortunately there is a
// fairly simple workaround for this, which is that you can add those
// bzl files as `deps` entries.
deps = append(deps, imp)
continue
}

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: impLabel.String(),
}
matches := ix.FindRulesByImport(res, languageName)
continue
}

if len(matches) == 0 {
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())
}
res := resolve.ImportSpec{
Lang: languageName,
Imp: impLabel.String(),
}
matches := ix.FindRulesByImport(res, languageName)

for _, m := range matches {
depLabel := m.Label
depLabel = depLabel.Rel(from.Repo, from.Pkg)
deps = append(deps, depLabel.String())
}
if len(matches) == 0 {
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 {
depLabel := m.Label
depLabel = depLabel.Rel(from.Repo, from.Pkg)
deps = append(deps, depLabel.String())
}
}

Expand Down
6 changes: 6 additions & 0 deletions gazelle/bzl/testdata/bazel_tools/BUILD.in
@@ -0,0 +1,6 @@
# Some comment to be preserved

filegroup(
name = "allfiles",
srcs = glob(["**"]),
)
15 changes: 15 additions & 0 deletions gazelle/bzl/testdata/bazel_tools/BUILD.out
@@ -0,0 +1,15 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

# Some comment to be preserved

filegroup(
name = "allfiles",
srcs = glob(["**"]),
)

bzl_library(
name = "foo",
srcs = ["foo.bzl"],
visibility = ["//visibility:public"],
deps = ["@bazel_tools//tools/build_defs/repo:http.bzl"],
)
Empty file.
10 changes: 10 additions & 0 deletions gazelle/bzl/testdata/bazel_tools/foo.bzl
@@ -0,0 +1,10 @@
"""
Doc string
"""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def wrapped_http_archive(**kwargs):
http_archive(
**kwargs
)