Skip to content

Commit 01c8dca

Browse files
authored
chore: verify that our bzl_library targets declare their deps (#1110)
* chore: verify that our bzl_library targets declare their deps Due to bazelbuild/bazel-skylib#568 we have no actions running for bzl_library, which means some are incorrect. This affects any downstream docgen pipeline, such as a user trying to run stardoc pointing at our bzl_library targets * chore: green up CI
1 parent e6b23cb commit 01c8dca

File tree

11 files changed

+111
-12
lines changed

11 files changed

+111
-12
lines changed

BUILD.bazel

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
12
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
23
load("@aspect_bazel_lib_host//:defs.bzl", "host")
3-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
44
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
55
load("@bazel_skylib//rules:write_file.bzl", "write_file")
66
load("@buildifier_prebuilt//:rules.bzl", "buildifier")
@@ -115,11 +115,21 @@ assert_contains(
115115
target_compatible_with = [] if host.bazel_version.startswith("7") else ["@platforms//:incompatible"],
116116
)
117117

118+
bzl_library(
119+
name = "bzl_library",
120+
srcs = ["bzl_library.bzl"],
121+
deps = ["@bazel_skylib//:bzl_library"],
122+
)
123+
118124
bzl_library(
119125
name = "deps",
120126
srcs = ["deps.bzl"],
121127
visibility = ["//visibility:public"],
122-
deps = ["@gazelle//:deps"],
128+
deps = [
129+
"@bazel_tools//tools/build_defs/repo:cache.bzl",
130+
"@bazel_tools//tools/build_defs/repo:http.bzl",
131+
"@gazelle//:deps",
132+
],
123133
)
124134

125135
# Place the .vale.ini file in bazel-bin so the relative path it contains

bzl_library.bzl

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""A library rule and macro for grouping Starlark sources.
2+
3+
Drop-in replacement for bzl_library in bazel_skylib, with exceptions:
4+
- We support .bzl and .star extensions, while bzl_library accepts .bzl and .scl.
5+
"""
6+
7+
load("@bazel_skylib//:bzl_library.bzl", "StarlarkLibraryInfo")
8+
9+
def _bzl_library_impl(ctx):
10+
deps_files = [x.files for x in ctx.attr.deps]
11+
all_files = depset(ctx.files.srcs, order = "postorder", transitive = deps_files)
12+
if not ctx.files.srcs and not deps_files:
13+
fail("bzl_library rule '%s' has no srcs or deps" % ctx.label)
14+
15+
return [
16+
# All dependent files should be listed in both `files` and in `runfiles`;
17+
# this ensures that a `bzl_library` can be referenced as `data` from
18+
# a separate program, or from `tools` of a genrule().
19+
DefaultInfo(
20+
files = all_files,
21+
runfiles = ctx.runfiles(transitive_files = all_files),
22+
),
23+
24+
# Interop with @bazel_skylib//:bzl_library
25+
StarlarkLibraryInfo(
26+
srcs = ctx.files.srcs,
27+
transitive_srcs = all_files,
28+
),
29+
]
30+
31+
bzl_library_rule = rule(
32+
implementation = _bzl_library_impl,
33+
attrs = {
34+
"srcs": attr.label_list(
35+
allow_files = [".bzl", ".star"],
36+
),
37+
"deps": attr.label_list(
38+
allow_files = [".bzl", ".star"],
39+
),
40+
},
41+
doc = """Creates a logical collection of Starlark .bzl and .star files.""",
42+
)
43+
44+
def bzl_library(name, srcs = [], deps = [], **kwargs):
45+
"""Wrapper for bzl_library.
46+
47+
Args:
48+
name: name
49+
50+
srcs: List of `.bzl` and `.star` files that are processed to create this target.
51+
deps: List of other `bzl_library` or `filegroup` targets that are required by the Starlark files listed in `srcs`.
52+
**kwargs: additional arguments for the bzl_library rule.
53+
"""
54+
55+
# buildifier: disable=unused-variable
56+
_ = kwargs.pop("compatible_with", None)
57+
_ = kwargs.pop("exec_compatible_with", None)
58+
_ = kwargs.pop("features", None)
59+
_ = kwargs.pop("target_compatible_with", None)
60+
bzl_library_rule(
61+
name = name,
62+
srcs = srcs,
63+
deps = deps,
64+
compatible_with = [],
65+
exec_compatible_with = [],
66+
features = [],
67+
target_compatible_with = [],
68+
**kwargs
69+
)
70+
71+
# validate that public API docs have correct deps, by running the doc extractor over them.
72+
# TODO(alexeagle): it would be better to attach this as a validation action in the bzl_library_rule
73+
# but there's no tool available for that since the Java implementation code is only exposed as a
74+
# native Bazel rule.
75+
# See bazelbuild/bazel-skylib#568
76+
if hasattr(native, "starlark_doc_extract") and "/private" not in native.package_name():
77+
for i, src in enumerate(srcs):
78+
native.starlark_doc_extract(
79+
name = "{}.doc_extract{}".format(name, i if i > 0 else ""),
80+
src = src,
81+
deps = deps,
82+
testonly = True,
83+
visibility = ["//visibility:private"],
84+
)

lib/BUILD.bazel

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
22
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
33
load("//lib:utils.bzl", "is_bzlmod_enabled")
44
load("//lib/private:stamping.bzl", "stamp_build_setting")
@@ -243,7 +243,12 @@ bzl_library(
243243
bzl_library(
244244
name = "extensions",
245245
srcs = ["extensions.bzl"],
246-
deps = ["@aspect_bazel_lib//lib:repositories"],
246+
deps = [
247+
"//lib:repositories",
248+
"//lib/private:extension_utils",
249+
"//lib/private:host_repo",
250+
"@bazel_features//:features",
251+
],
247252
)
248253

249254
bzl_library(

lib/private/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
22
load("//lib:utils.bzl", "is_bazel_7_or_greater")
33
load(":utf8_environment.bzl", "utf8_environment")
44

lib/private/host_repo.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ load(":repo_utils.bzl", "repo_utils")
66
def _host_repo_impl(rctx):
77
# Base BUILD file for this repository
88
rctx.file("BUILD.bazel", """# @generated by @aspect_bazel_lib//lib/private:host_repo.bzl
9-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
9+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
1010
bzl_library(
1111
name = "defs",
1212
srcs = ["defs.bzl"],

lib/tests/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"tests for libs"
22

3+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
34
load("@bazel_features//:features.bzl", "bazel_features")
4-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
55
load("@bazel_skylib//rules:write_file.bzl", "write_file")
66
load("@rules_shell//shell:sh_test.bzl", "sh_test")
77
load("//lib:expand_template.bzl", "expand_template")

lib/tests/bazelrc_presets/all/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
22
load(":write_aspect_bazelrc_presets.bzl", "write_aspect_bazelrc_presets")
33

44
write_aspect_bazelrc_presets(name = "update_aspect_bazelrc_presets")

lib/tests/copy_to_directory_bin_action/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
22
load("//lib:copy_directory.bzl", "copy_directory")
33
load("//lib:copy_to_bin.bzl", "copy_to_bin")
44
load("//lib:diff_test.bzl", "diff_test")

lib/tests/stamping/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
22
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
33
load("//lib:run_binary.bzl", "run_binary")
44
load(":stamp_aware_rule.bzl", "my_stamp_aware_rule")

tools/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1+
load("@aspect_bazel_lib//:bzl_library.bzl", "bzl_library")
22

33
bzl_library(
44
name = "integrity",

0 commit comments

Comments
 (0)