From 5c21350cff5d6c058e0aef3a07ffe19078704c81 Mon Sep 17 00:00:00 2001 From: Andrew Allen Date: Fri, 11 Sep 2020 16:15:24 +0000 Subject: [PATCH] Gazelle now handles imports from `@bazel_tools` `@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. --- gazelle/bzl/gazelle.go | 39 ++++++++++++++-------- gazelle/bzl/testdata/bazel_tools/BUILD.in | 6 ++++ gazelle/bzl/testdata/bazel_tools/BUILD.out | 15 +++++++++ gazelle/bzl/testdata/bazel_tools/WORKSPACE | 0 gazelle/bzl/testdata/bazel_tools/foo.bzl | 10 ++++++ 5 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 gazelle/bzl/testdata/bazel_tools/BUILD.in create mode 100644 gazelle/bzl/testdata/bazel_tools/BUILD.out create mode 100644 gazelle/bzl/testdata/bazel_tools/WORKSPACE create mode 100644 gazelle/bzl/testdata/bazel_tools/foo.bzl diff --git a/gazelle/bzl/gazelle.go b/gazelle/bzl/gazelle.go index a0b8aa60..b58eee63 100644 --- a/gazelle/bzl/gazelle.go +++ b/gazelle/bzl/gazelle.go @@ -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()) } } diff --git a/gazelle/bzl/testdata/bazel_tools/BUILD.in b/gazelle/bzl/testdata/bazel_tools/BUILD.in new file mode 100644 index 00000000..e267b5a1 --- /dev/null +++ b/gazelle/bzl/testdata/bazel_tools/BUILD.in @@ -0,0 +1,6 @@ +# Some comment to be preserved + +filegroup( + name = "allfiles", + srcs = glob(["**"]), +) diff --git a/gazelle/bzl/testdata/bazel_tools/BUILD.out b/gazelle/bzl/testdata/bazel_tools/BUILD.out new file mode 100644 index 00000000..576cfade --- /dev/null +++ b/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"], +) diff --git a/gazelle/bzl/testdata/bazel_tools/WORKSPACE b/gazelle/bzl/testdata/bazel_tools/WORKSPACE new file mode 100644 index 00000000..e69de29b diff --git a/gazelle/bzl/testdata/bazel_tools/foo.bzl b/gazelle/bzl/testdata/bazel_tools/foo.bzl new file mode 100644 index 00000000..b025d213 --- /dev/null +++ b/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 + )