Skip to content

Commit

Permalink
Gazelle now handles imports from @bazel_tools
Browse files Browse the repository at this point in the history
`@bazel_tools` is tricky since it is effectively a part of the standard
library that can not have a `bzl_library` attached to it. As a simple
fix for this, `bzl_library` can have a srcs dependency on it so that it
includes the transitive closure of all of its dependencies.

`@bazel_tools` imports are rewritten into the `srcs` attribute since
they are `exports_files`ed from the @bazel_tools.
  • Loading branch information
achew22 committed Sep 11, 2020
1 parent bc97abb commit 9b918cb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
41 changes: 29 additions & 12 deletions gazelle/bzl/gazelle.go
Expand Up @@ -150,33 +150,50 @@ func (*bzlLibraryLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo
return
}

extraSrcs := make([]string, 0, len(imports))
deps := make([]string, 0, len(imports))
for _, imp := range imports {
if strings.HasPrefix(imp, "@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 `srcs` entries.
extraSrcs = append(extraSrcs, imp)
continue
}

if strings.HasPrefix(imp, "@") || !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,
}
matches := ix.FindRulesByImport(res, languageName)
continue
}

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

for _, m := range matches {
deps = append(deps, m.Label.String())
}
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)
}

for _, m := range matches {
deps = append(deps, m.Label.String())
}
}

sort.Strings(deps)
if len(deps) > 0 {
r.SetAttr("deps", deps)
}

sort.Strings(extraSrcs)
if len(extraSrcs) > 0 {
r.SetAttr("srcs", append(r.AttrStrings("srcs"), extraSrcs...))
}
}

var kinds = map[string]rule.KindInfo{
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(["**"]),
)
17 changes: 17 additions & 0 deletions gazelle/bzl/testdata/bazel_tools/BUILD.out
@@ -0,0 +1,17 @@
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",
"@bazel_tools//tools/build_defs/repo:http.bzl",
],
visibility = ["//visibility:public"],
)
Empty file.
6 changes: 6 additions & 0 deletions gazelle/bzl/testdata/bazel_tools/foo.bzl
@@ -0,0 +1,6 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

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

0 comments on commit 9b918cb

Please sign in to comment.