aboutsummaryrefslogtreecommitdiff
path: root/gazelle
diff options
context:
space:
mode:
Diffstat (limited to 'gazelle')
-rw-r--r--gazelle/BUILD37
-rw-r--r--gazelle/MODULE.bazel21
-rw-r--r--gazelle/WORKSPACE25
-rw-r--r--gazelle/WORKSPACE.bzlmod8
-rw-r--r--gazelle/bzl/BUILD32
-rw-r--r--gazelle/bzl/gazelle.go4
-rw-r--r--gazelle/bzl/gazelle_test.go4
-rw-r--r--gazelle/bzl/testdata/README.md2
-rw-r--r--gazelle/bzl/testdata/empty/BUILD.in2
-rw-r--r--gazelle/setup.bzl32
-rw-r--r--gazelle/workspace.bzl43
11 files changed, 200 insertions, 10 deletions
diff --git a/gazelle/BUILD b/gazelle/BUILD
new file mode 100644
index 0000000..d1c6daa
--- /dev/null
+++ b/gazelle/BUILD
@@ -0,0 +1,37 @@
+load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+
+exports_files(["WORKSPACE.bzlmod"])
+
+bzl_library(
+ name = "setup",
+ srcs = ["setup.bzl"],
+ visibility = ["//visibility:public"],
+ deps = [
+ "@bazel_gazelle//:deps",
+ "@io_bazel_rules_go//go:deps",
+ ],
+)
+
+bzl_library(
+ name = "workspace",
+ srcs = ["workspace.bzl"],
+ visibility = ["//visibility:public"],
+ deps = [
+ "@bazel_skylib//:workspace",
+ "@bazel_tools//tools/build_defs/repo:http.bzl",
+ "@bazel_tools//tools/build_defs/repo:utils.bzl",
+ ],
+)
+
+# TODO(arostovtsev): exclude everything below from distro tarball
+filegroup(
+ name = "distribution",
+ srcs = [
+ "BUILD",
+ "MODULE.bazel",
+ "WORKSPACE.bzlmod",
+ "setup.bzl",
+ "workspace.bzl",
+ ],
+ visibility = ["//visibility:public"],
+)
diff --git a/gazelle/MODULE.bazel b/gazelle/MODULE.bazel
new file mode 100644
index 0000000..7afa242
--- /dev/null
+++ b/gazelle/MODULE.bazel
@@ -0,0 +1,21 @@
+module(
+ name = "bazel_skylib_gazelle_plugin",
+ # Keep in sync with @bazel_skylib//:MODULE.bazel and @bazel_skylib//:version.bzl
+ version = "1.5.0",
+ compatibility_level = 1,
+)
+
+# Keep in sync with @bazel_skylib//:MODULE.bazel and @bazel_skylib//:version.bzl
+bazel_dep(name = "bazel_skylib", version = "1.4.2")
+bazel_dep(name = "gazelle", version = "0.29.0", repo_name = "bazel_gazelle")
+bazel_dep(name = "rules_go", version = "0.41.0", repo_name = "io_bazel_rules_go")
+
+# `rules_go` will register a toolchain for us if the user doesn't do so
+
+go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
+go_deps.module(
+ path = "github.com/bazelbuild/buildtools",
+ sum = "h1:fmdo+fvvWlhldUcqkhAMpKndSxMN3vH5l7yow5cEaiQ=",
+ version = "v0.0.0-20220531122519-a43aed7014c8",
+)
+use_repo(go_deps, "com_github_bazelbuild_buildtools")
diff --git a/gazelle/WORKSPACE b/gazelle/WORKSPACE
new file mode 100644
index 0000000..cc0217d
--- /dev/null
+++ b/gazelle/WORKSPACE
@@ -0,0 +1,25 @@
+workspace(name = "bazel_skylib_gazelle_plugin")
+
+local_repository(
+ name = "bazel_skylib",
+ path = "..",
+)
+
+load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
+
+bazel_skylib_workspace()
+
+load(":workspace.bzl", "bazel_skylib_gazelle_plugin_workspace")
+
+bazel_skylib_gazelle_plugin_workspace()
+
+load(":setup.bzl", "bazel_skylib_gazelle_plugin_setup")
+
+bazel_skylib_gazelle_plugin_setup()
+
+# Provide a repository hint for Gazelle to inform it that the go package
+# github.com/bazelbuild/rules_go is available from io_bazel_rules_go and it
+# doesn't need to duplicatively fetch it.
+# gazelle:repository go_repository name=io_bazel_rules_go importpath=github.com/bazelbuild/rules_go
+# Another Gazelle repository hint.
+# gazelle:repository go_repository name=bazel_gazelle importpath=github.com/bazelbuild/bazel-gazelle/testtools
diff --git a/gazelle/WORKSPACE.bzlmod b/gazelle/WORKSPACE.bzlmod
new file mode 100644
index 0000000..621db93
--- /dev/null
+++ b/gazelle/WORKSPACE.bzlmod
@@ -0,0 +1,8 @@
+workspace(name = "bazel_skylib_gazelle_plugin")
+
+# Provide a repository hint for Gazelle to inform it that the go package
+# github.com/bazelbuild/rules_go is available from io_bazel_rules_go and it
+# doesn't need to duplicatively fetch it.
+# gazelle:repository go_repository name=io_bazel_rules_go importpath=github.com/bazelbuild/rules_go
+# Another Gazelle repository hint.
+# gazelle:repository go_repository name=bazel_gazelle importpath=github.com/bazelbuild/bazel-gazelle/testtools
diff --git a/gazelle/bzl/BUILD b/gazelle/bzl/BUILD
index 29caeb0..0b8e6d3 100644
--- a/gazelle/bzl/BUILD
+++ b/gazelle/bzl/BUILD
@@ -1,5 +1,5 @@
-load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
+load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
# gazelle:exclude testdata
@@ -25,9 +25,12 @@ go_test(
srcs = ["gazelle_test.go"],
data = [
":gazelle-skylib",
- ] + glob([
- "testdata/**",
- ]),
+ ] + glob(
+ [
+ "testdata/**",
+ ],
+ allow_empty = True,
+ ),
embed = [":bzl"],
deps = [
"@bazel_gazelle//testtools:go_default_library",
@@ -51,3 +54,24 @@ gazelle(
name = "gazelle",
gazelle = ":gazelle-skylib",
)
+
+# TODO(arostovtsev): exclude everything below from distro tarball
+
+# The files needed for distribution
+# A fake testdata directory is created so that
+# the build file has nothing missing, but we
+# do not bloat the distribution tarball
+filegroup(
+ name = "distribution",
+ srcs = glob(["*.go"]) + [
+ "BUILD",
+ ":fake-testdata",
+ ],
+ visibility = ["//visibility:public"],
+)
+
+genrule(
+ name = "fake-testdata",
+ outs = ["testdata"],
+ cmd = "touch $@",
+)
diff --git a/gazelle/bzl/gazelle.go b/gazelle/bzl/gazelle.go
index dccec27..94c32e9 100644
--- a/gazelle/bzl/gazelle.go
+++ b/gazelle/bzl/gazelle.go
@@ -111,7 +111,7 @@ func (*bzlLibraryLang) Fix(c *config.Config, f *rule.File) {}
// returned, including an empty slice, the rule will be indexed.
func (b *bzlLibraryLang) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
srcs := r.AttrStrings("srcs")
- imports := make([]resolve.ImportSpec, len(srcs))
+ imports := make([]resolve.ImportSpec, 0, len(srcs))
for _, src := range srcs {
spec := resolve.ImportSpec{
@@ -173,7 +173,7 @@ func (*bzlLibraryLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo
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.
+ // is disabled so take a guess at what the target name should be.
deps = append(deps, strings.TrimSuffix(imp, fileType))
continue
}
diff --git a/gazelle/bzl/gazelle_test.go b/gazelle/bzl/gazelle_test.go
index e4f59e8..1554337 100644
--- a/gazelle/bzl/gazelle_test.go
+++ b/gazelle/bzl/gazelle_test.go
@@ -29,7 +29,7 @@ import (
var gazellePath = findGazelle()
-const testDataPath = "gazelle/bzl/testdata/"
+const testDataPath = "bzl/testdata/"
// TestGazelleBinary runs a gazelle binary with starlib installed on each
// directory in `testdata/*`. Please see `testdata/README.md` for more
@@ -135,7 +135,7 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) {
}
func findGazelle() string {
- gazellePath, ok := bazel.FindBinary("gazelle/bzl", "gazelle-skylib")
+ gazellePath, ok := bazel.FindBinary("bzl", "gazelle-skylib")
if !ok {
panic("could not find gazelle binary")
}
diff --git a/gazelle/bzl/testdata/README.md b/gazelle/bzl/testdata/README.md
index a6a3603..b498c66 100644
--- a/gazelle/bzl/testdata/README.md
+++ b/gazelle/bzl/testdata/README.md
@@ -3,7 +3,7 @@
This directory contains a suite of test cases for the Skylark language plugin
for Gazelle.
-Please note that there are no `BUILD` or `BUILD.bazel` files in subdirs, insted
+Please note that there are no `BUILD` or `BUILD.bazel` files in subdirs, instead
there are `BUILD.in` and `BUILD.out` describing what the `BUILD` should look
like initially and what the `BUILD` file should look like after the run. These
names are special because they are not recognized by Bazel as a proper `BUILD`
diff --git a/gazelle/bzl/testdata/empty/BUILD.in b/gazelle/bzl/testdata/empty/BUILD.in
index e1e154c..ce5d4cc 100644
--- a/gazelle/bzl/testdata/empty/BUILD.in
+++ b/gazelle/bzl/testdata/empty/BUILD.in
@@ -2,7 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "weirdly_named_target_that_will_be_removed",
- srcs = ["nonexistant.bzl"],
+ srcs = ["nonexistent.bzl"],
visibility = ["//visibility:public"],
)
diff --git a/gazelle/setup.bzl b/gazelle/setup.bzl
new file mode 100644
index 0000000..06f911e
--- /dev/null
+++ b/gazelle/setup.bzl
@@ -0,0 +1,32 @@
+# Copyright 2019 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Dependency registration helpers for the gazelle plugin."""
+
+load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
+load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
+
+def bazel_skylib_gazelle_plugin_setup(go_version = "1.20.5", register_go_toolchains = True):
+ """Set up the dependencies needed by the Gazelle plugin.
+
+ Args:
+ go_version: The version of Go registered as part of the build as a string
+ register_go_toolchains: A boolean indicating whether or not to register the Go toolchains. Defaults to `True`
+ """
+ go_rules_dependencies()
+
+ if register_go_toolchains:
+ go_register_toolchains(version = go_version)
+
+ gazelle_dependencies()
diff --git a/gazelle/workspace.bzl b/gazelle/workspace.bzl
new file mode 100644
index 0000000..0eca993
--- /dev/null
+++ b/gazelle/workspace.bzl
@@ -0,0 +1,43 @@
+# Copyright 2019 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Dependency registration helpers for repositories which need to load bazel-skylib's gazelle plugin."""
+
+load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
+
+def bazel_skylib_gazelle_plugin_workspace():
+ """Loads dependencies required to use skylib's gazelle plugin"""
+ bazel_skylib_workspace()
+
+ maybe(
+ http_archive,
+ name = "io_bazel_rules_go",
+ sha256 = "278b7ff5a826f3dc10f04feaf0b70d48b68748ccd512d7f98bf442077f043fe3",
+ urls = [
+ "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
+ "https://github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
+ ],
+ )
+
+ maybe(
+ http_archive,
+ name = "bazel_gazelle",
+ sha256 = "ecba0f04f96b4960a5b250c8e8eeec42281035970aa8852dda73098274d14a1d",
+ urls = [
+ "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz",
+ "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz",
+ ],
+ )