Skip to content

Commit 14f436f

Browse files
authored
refactor: introduce helper for defining distribution filegroups (#4152)
Previously, distribution filegroups required manually enumerating each direct subpackage's distribution target in `srcs`. This created repetitive boilerplate across BUILD files and risked omitting newly added subpackages from release distributions. Introduce the `distribution_filegroup` macro to automatically glob package files and discover direct subpackages using `native.subpackages()`, with support for an `exclude` list. * Also updates BUILD files across the repository to use the new macro
1 parent ced38c3 commit 14f436f

43 files changed

Lines changed: 228 additions & 131 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎command_line_option/BUILD.bazel‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# treated as aliases for `//command_line_option:XXX` psuedo-targets. They
55
# are not actual flags or have any value.
66

7+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup") # buildifier: disable=bzl-visibility
8+
79
package(
810
default_visibility = ["//visibility:public"],
911
)
@@ -23,8 +25,6 @@ alias(
2325
actual = "//python:none",
2426
)
2527

26-
filegroup(
28+
distribution_filegroup(
2729
name = "distribution",
28-
srcs = glob(["**"]),
29-
visibility = ["//:__subpackages__"],
3030
)

‎python/BUILD.bazel‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,15 @@ that @rules_python//python is only concerned with the core rules.
2929
# gazelle:resolve starlark //python:current_py_toolchain.bzl //python:current_py_toolchain_bzl
3030

3131
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
32+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
3233
load(":current_py_toolchain.bzl", "current_py_toolchain")
3334

3435
package(default_visibility = ["//visibility:public"])
3536

3637
licenses(["notice"])
3738

38-
filegroup(
39+
distribution_filegroup(
3940
name = "distribution",
40-
srcs = glob(["**"]) + [
41-
"//python/api:distribution",
42-
"//python/bin:distribution",
43-
"//python/cc:distribution",
44-
"//python/config_settings:distribution",
45-
"//python/constraints:distribution",
46-
"//python/entry_points:distribution",
47-
"//python/extensions:distribution",
48-
"//python/local_toolchains:distribution",
49-
"//python/pip_install:distribution",
50-
"//python/private:distribution",
51-
"//python/runfiles:distribution",
52-
"//python/runtime_env_toolchains:distribution",
53-
"//python/uv:distribution",
54-
"//python/zipapp:distribution",
55-
],
56-
visibility = ["//:__pkg__"],
5741
)
5842

5943
# ========= bzl_library targets end =========

‎python/api/BUILD.bazel‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
# limitations under the License.
1414

1515
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
16+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
1617

1718
package(
1819
default_visibility = ["//:__subpackages__"],
1920
)
2021

21-
filegroup(
22+
distribution_filegroup(
2223
name = "distribution",
23-
srcs = glob(["**"]),
2424
)
2525

2626
bzl_library(

‎python/bin/BUILD.bazel‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
12
load("//python/private:interpreter.bzl", _interpreter_binary = "interpreter_binary")
23
load("//python/private:repl.bzl", "py_repl_binary")
34

4-
filegroup(
5+
package(default_visibility = ["//:__subpackages__"])
6+
7+
distribution_filegroup(
58
name = "distribution",
6-
srcs = glob(["**"]),
7-
visibility = ["//:__subpackages__"],
89
)
910

1011
_interpreter_binary(

‎python/cc/BUILD.bazel‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
44
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
55
load("//python/private:current_py_cc_headers.bzl", "current_py_cc_headers", "current_py_cc_headers_abi3")
66
load("//python/private:current_py_cc_libs.bzl", "current_py_cc_libs")
7+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
78

89
package(
910
default_visibility = ["//:__subpackages__"],
@@ -47,9 +48,8 @@ toolchain_type(
4748
visibility = ["//visibility:public"],
4849
)
4950

50-
filegroup(
51+
distribution_filegroup(
5152
name = "distribution",
52-
srcs = glob(["**"]),
5353
)
5454

5555
bzl_library(

‎python/config_settings/BUILD.bazel‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
22
load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION", "MINOR_MAPPING", "PYTHON_VERSIONS")
33
load("@rules_python_internal//:rules_python_config.bzl", "config")
4+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
45
load(
56
"//python/private:flags.bzl",
67
"AddSrcsToRunfilesFlag",
@@ -19,15 +20,15 @@ load("//python/private:visibility.bzl", "NOT_ACTUALLY_PUBLIC") # buildifier: di
1920
load("//python/private/pypi:flags.bzl", "define_pypi_internal_flags")
2021
load(":config_settings.bzl", "construct_config_settings")
2122

23+
package(default_visibility = ["//:__subpackages__"])
24+
2225
# We don't generate bzl_library for these because they aren't public targets
2326
# and should be moved
2427
# gazelle:exclude config_settings.bzl
2528
# gazelle:exclude transition.bzl
2629

27-
filegroup(
30+
distribution_filegroup(
2831
name = "distribution",
29-
srcs = glob(["**"]),
30-
visibility = ["//python:__pkg__"],
3132
)
3233

3334
construct_config_settings(

‎python/constraints/BUILD.bazel‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
16+
1517
package(default_visibility = ["//visibility:public"])
1618

1719
licenses(["notice"])
1820

19-
filegroup(
21+
distribution_filegroup(
2022
name = "distribution",
21-
srcs = glob(["**"]),
22-
visibility = ["//python:__pkg__"],
2323
)
2424

2525
# A constraint_setting to use for constraints related to the location of the

‎python/entry_points/BUILD.bazel‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# limitations under the License.
1414

1515
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
16+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
17+
18+
package(default_visibility = ["//:__subpackages__"])
1619

1720
exports_files(
1821
[
@@ -21,10 +24,8 @@ exports_files(
2124
visibility = ["//docs:__subpackages__"],
2225
)
2326

24-
filegroup(
27+
distribution_filegroup(
2528
name = "distribution",
26-
srcs = glob(["**"]),
27-
visibility = ["//python:__subpackages__"],
2829
)
2930

3031
bzl_library(

‎python/extensions/BUILD.bazel‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
# limitations under the License.
1414

1515
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
16+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
1617

1718
package(default_visibility = ["//visibility:public"])
1819

1920
licenses(["notice"])
2021

21-
filegroup(
22+
distribution_filegroup(
2223
name = "distribution",
23-
srcs = glob(["**"]),
24-
visibility = ["//python:__pkg__"],
2524
)
2625

2726
bzl_library(

‎python/local_toolchains/BUILD.bazel‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
23

34
package(default_visibility = ["//:__subpackages__"])
45

5-
filegroup(
6+
distribution_filegroup(
67
name = "distribution",
7-
srcs = glob(["**"]),
88
)
99

1010
bzl_library(

0 commit comments

Comments
 (0)