From 32bbb52730ce82f7b6458dca8e8955e1801a9d69 Mon Sep 17 00:00:00 2001 From: Kilian Funk Date: Fri, 12 Jul 2024 14:36:14 -0700 Subject: [PATCH 1/5] fix: subpackages.all works for root packages (#530) (#531) --- lib/subpackages.bzl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/subpackages.bzl b/lib/subpackages.bzl index 5b674fd..1d006e5 100644 --- a/lib/subpackages.bzl +++ b/lib/subpackages.bzl @@ -62,7 +62,10 @@ def _all(exclude = [], allow_empty = False, fully_qualified = True): return subs def _fully_qualified(relative_path): - return "//%s/%s" % (native.package_name(), relative_path) + package_name = native.package_name() + if package_name: + return "//%s/%s" % (package_name, relative_path) + return "//" + relative_path def _exists(relative_path): """Checks to see if relative_path is a direct subpackage of the current package. From da7ba2d23c77ad37f7b4b093df888e668782982a Mon Sep 17 00:00:00 2001 From: aiuto Date: Mon, 15 Jul 2024 13:50:23 -0400 Subject: [PATCH 2/5] Strip compatible_with from bzl_library rules (#522) * Wrap bzl_library in a macro so that we can force off some global attribures that are never appropriate for BUILD files. Removes: - *_compatible_with - features * cdate * linty * linty * more lint --- BUILD | 3 + bzl_library.bzl | 111 ++++++++-------------------------- docs/BUILD | 2 +- rules/private/BUILD | 14 +++++ rules/private/bzl_library.bzl | 107 ++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 87 deletions(-) create mode 100644 rules/private/bzl_library.bzl diff --git a/BUILD b/BUILD index 6dd357c..635f953 100644 --- a/BUILD +++ b/BUILD @@ -60,6 +60,9 @@ bzl_library( bzl_library( name = "bzl_library", srcs = ["bzl_library.bzl"], + deps = [ + "//rules/private:bzl_library", + ], ) bzl_library( diff --git a/bzl_library.bzl b/bzl_library.bzl index a139386..d6dbf81 100644 --- a/bzl_library.bzl +++ b/bzl_library.bzl @@ -14,94 +14,33 @@ """Skylib module containing a library rule for aggregating rules files.""" -StarlarkLibraryInfo = provider( - "Information on contained Starlark rules.", - fields = { - "srcs": "Top level rules files.", - "transitive_srcs": "Transitive closure of rules files required for " + - "interpretation of the srcs", - }, +# buildifier: disable=bzl-visibility +load( + "//rules/private:bzl_library.bzl", + _StarlarkLibraryInfo = "StarlarkLibraryInfo", + _bzl_library = "bzl_library", ) -def _bzl_library_impl(ctx): - deps_files = [x.files for x in ctx.attr.deps] - all_files = depset(ctx.files.srcs, order = "postorder", transitive = deps_files) - if not ctx.files.srcs and not deps_files: - fail("bzl_library rule '%s' has no srcs or deps" % ctx.label) +StarlarkLibraryInfo = _StarlarkLibraryInfo - return [ - # All dependent files should be listed in both `files` and in `runfiles`; - # this ensures that a `bzl_library` can be referenced as `data` from - # a separate program, or from `tools` of a genrule(). - DefaultInfo( - files = all_files, - runfiles = ctx.runfiles(transitive_files = all_files), - ), +def bzl_library(name, **kwargs): + """Wrapper for bzl_library. - # We also define our own provider struct, for aggregation and testing. - StarlarkLibraryInfo( - srcs = ctx.files.srcs, - transitive_srcs = all_files, - ), - ] + Args: + name: name + **kwargs: see the generated doc for rules/private/bzl_library. + """ -bzl_library = rule( - implementation = _bzl_library_impl, - attrs = { - "srcs": attr.label_list( - allow_files = [".bzl", ".scl"], - doc = "List of `.bzl` and `.scl` files that are processed to create this target.", - ), - "deps": attr.label_list( - allow_files = [".bzl", ".scl"], - doc = """List of other `bzl_library` or `filegroup` targets that are required by the -Starlark files listed in `srcs`.""", - ), - }, - doc = """Creates a logical collection of Starlark .bzl and .scl files. - -Example: - Suppose your project has the following structure: - - ``` - [workspace]/ - WORKSPACE - BUILD - checkstyle/ - BUILD - checkstyle.bzl - lua/ - BUILD - lua.bzl - luarocks.bzl - ``` - - In this case, you can have `bzl_library` targets in `checkstyle/BUILD` and - `lua/BUILD`: - - `checkstyle/BUILD`: - - ```python - load("@bazel_skylib//:bzl_library.bzl", "bzl_library") - - bzl_library( - name = "checkstyle-rules", - srcs = ["checkstyle.bzl"], - ) - ``` - - `lua/BUILD`: - - ```python - load("@bazel_skylib//:bzl_library.bzl", "bzl_library") - - bzl_library( - name = "lua-rules", - srcs = [ - "lua.bzl", - "luarocks.bzl", - ], - ) - ``` -""", -) + # buildifier: disable=unused-variable + _ = kwargs.pop("compatible_with", None) + _ = kwargs.pop("exec_compatible_with", None) + _ = kwargs.pop("features", None) + _ = kwargs.pop("target_compatible_with", None) + _bzl_library( + name = name, + compatible_with = [], + exec_compatible_with = [], + features = [], + target_compatible_with = [], + **kwargs + ) diff --git a/docs/BUILD b/docs/BUILD index 73ace3d..4ee05bc 100644 --- a/docs/BUILD +++ b/docs/BUILD @@ -18,7 +18,7 @@ stardoc_with_diff_test( stardoc_with_diff_test( name = "bzl_library", - bzl_library_target = "//:bzl_library", + bzl_library_target = "//rules/private:bzl_library", out_label = "//docs:bzl_library.md", ) diff --git a/rules/private/BUILD b/rules/private/BUILD index b6a935d..a876c7c 100644 --- a/rules/private/BUILD +++ b/rules/private/BUILD @@ -4,6 +4,20 @@ package(default_applicable_licenses = ["//:license"]) licenses(["notice"]) +bzl_library( + name = "bzl_library", + srcs = ["bzl_library.bzl"], + visibility = [ + "//:__pkg__", + "//docs:__pkg__", + ], +) + +exports_files( + ["bzl_library.bzl"], + visibility = ["//docs:__pkg__"], +) + bzl_library( name = "copy_common", srcs = ["copy_common.bzl"], diff --git a/rules/private/bzl_library.bzl b/rules/private/bzl_library.bzl new file mode 100644 index 0000000..a139386 --- /dev/null +++ b/rules/private/bzl_library.bzl @@ -0,0 +1,107 @@ +# Copyright 2017 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. + +"""Skylib module containing a library rule for aggregating rules files.""" + +StarlarkLibraryInfo = provider( + "Information on contained Starlark rules.", + fields = { + "srcs": "Top level rules files.", + "transitive_srcs": "Transitive closure of rules files required for " + + "interpretation of the srcs", + }, +) + +def _bzl_library_impl(ctx): + deps_files = [x.files for x in ctx.attr.deps] + all_files = depset(ctx.files.srcs, order = "postorder", transitive = deps_files) + if not ctx.files.srcs and not deps_files: + fail("bzl_library rule '%s' has no srcs or deps" % ctx.label) + + return [ + # All dependent files should be listed in both `files` and in `runfiles`; + # this ensures that a `bzl_library` can be referenced as `data` from + # a separate program, or from `tools` of a genrule(). + DefaultInfo( + files = all_files, + runfiles = ctx.runfiles(transitive_files = all_files), + ), + + # We also define our own provider struct, for aggregation and testing. + StarlarkLibraryInfo( + srcs = ctx.files.srcs, + transitive_srcs = all_files, + ), + ] + +bzl_library = rule( + implementation = _bzl_library_impl, + attrs = { + "srcs": attr.label_list( + allow_files = [".bzl", ".scl"], + doc = "List of `.bzl` and `.scl` files that are processed to create this target.", + ), + "deps": attr.label_list( + allow_files = [".bzl", ".scl"], + doc = """List of other `bzl_library` or `filegroup` targets that are required by the +Starlark files listed in `srcs`.""", + ), + }, + doc = """Creates a logical collection of Starlark .bzl and .scl files. + +Example: + Suppose your project has the following structure: + + ``` + [workspace]/ + WORKSPACE + BUILD + checkstyle/ + BUILD + checkstyle.bzl + lua/ + BUILD + lua.bzl + luarocks.bzl + ``` + + In this case, you can have `bzl_library` targets in `checkstyle/BUILD` and + `lua/BUILD`: + + `checkstyle/BUILD`: + + ```python + load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + + bzl_library( + name = "checkstyle-rules", + srcs = ["checkstyle.bzl"], + ) + ``` + + `lua/BUILD`: + + ```python + load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + + bzl_library( + name = "lua-rules", + srcs = [ + "lua.bzl", + "luarocks.bzl", + ], + ) + ``` +""", +) From 43c6185e7efb39991059c161db645dfd64ffca34 Mon Sep 17 00:00:00 2001 From: Alexandre Rostovtsev Date: Mon, 15 Jul 2024 13:57:10 -0400 Subject: [PATCH 3/5] Make only root test_deps externally visible (#508) All other test_deps targets implicitly require the root BUILD file for the `license` target for their default_applicable_licenses; therefore, users should only depend on the root test_deps target. --- lib/BUILD | 1 + rules/BUILD | 1 + toolchains/unittest/BUILD | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/BUILD b/lib/BUILD index cff8065..c22c7ab 100644 --- a/lib/BUILD +++ b/lib/BUILD @@ -99,6 +99,7 @@ filegroup( name = "test_deps", testonly = True, srcs = ["BUILD"] + glob(["*.bzl"]), + visibility = ["//:__subpackages__"], # Needs skylib's root BUILD file for default_applicable_licenses ) # The files needed for distribution diff --git a/rules/BUILD b/rules/BUILD index 11706e4..293fd03 100644 --- a/rules/BUILD +++ b/rules/BUILD @@ -69,6 +69,7 @@ filegroup( srcs = [ "BUILD", ] + glob(["*.bzl"]), + visibility = ["//:__subpackages__"], # Needs skylib's root BUILD file for default_applicable_licenses ) # The files needed for distribution diff --git a/toolchains/unittest/BUILD b/toolchains/unittest/BUILD index 275ddfc..c5744dd 100644 --- a/toolchains/unittest/BUILD +++ b/toolchains/unittest/BUILD @@ -56,7 +56,7 @@ filegroup( srcs = [ "BUILD", ], - visibility = ["//:__subpackages__"], + visibility = ["//:__subpackages__"], # Needs skylib's root BUILD file for default_applicable_licenses ) # The files needed for distribution From fa66e6b15b06070c0c6467983b4892bc33dc9145 Mon Sep 17 00:00:00 2001 From: Boleyn Su Date: Tue, 16 Jul 2024 02:03:44 +0800 Subject: [PATCH 4/5] Update README.md (#380) * Update README.md If we will remove new_sets, then we should deprecate it instead of sets. * Update README.md --------- Co-authored-by: Ivo List --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index b72b729..fd2e275 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,8 @@ s = shell.quote(p) * [partial](docs/partial_doc.md) * [paths](docs/paths_doc.md) * [selects](docs/selects_doc.md) -* [sets](lib/sets.bzl) - _deprecated_, use `new_sets` +* [sets](docs/new_sets_doc.md) * [modules](docs/modules_doc.md) -* [new_sets](docs/new_sets_doc.md) * [shell](docs/shell_doc.md) * [structs](docs/structs_doc.md) * [subpackages](docs/subpackages_doc.md) From 5c071b5006bb9799981d04d74a28bdee2f000d4a Mon Sep 17 00:00:00 2001 From: aiuto Date: Fri, 9 Aug 2024 08:40:50 -0400 Subject: [PATCH 5/5] Add test_deps to rules/private (#534) This pattern was missed when rules/private was added. --- rules/BUILD | 1 + rules/private/BUILD | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/rules/BUILD b/rules/BUILD index 293fd03..ee04c77 100644 --- a/rules/BUILD +++ b/rules/BUILD @@ -68,6 +68,7 @@ filegroup( testonly = True, srcs = [ "BUILD", + "//rules/private:test_deps", ] + glob(["*.bzl"]), visibility = ["//:__subpackages__"], # Needs skylib's root BUILD file for default_applicable_licenses ) diff --git a/rules/private/BUILD b/rules/private/BUILD index a876c7c..44f1509 100644 --- a/rules/private/BUILD +++ b/rules/private/BUILD @@ -49,6 +49,17 @@ bzl_library( srcs = ["maprule_util.bzl"], ) +filegroup( + name = "test_deps", + testonly = True, + srcs = [ + "BUILD", + ] + glob(["*.bzl"]), + visibility = [ + "//rules:__pkg__", + ], +) + # The files needed for distribution filegroup( name = "distribution",