Skip to content

Commit 7cf098e

Browse files
mai93gemini-code-assist[bot]rickeylev
authored
build: Starlarkify python flags (#3334)
Add starlark flags for `--python_path`, `--build_python_zip` and `--incompatible_default_to_explicit_init_py`. - The transitions logic is updated to set both the native and starlark versions of the flags to allow alternating between them until the native ones are removed. - `--build_python_zip` is changed to `boolean` instead of `Tristate` with default value set to `True` on `windows` and `False` otherwise. - `scope = universal` attribute is added to the starlark flags so they can be propagated to exec config on Bazel 9. This required upgrading `bazel_skylib` version to have `scope` attribute defined. Work towards: #3252 cc @gregestren --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Richard Levasseur <rlevasseur@google.com>
1 parent 587f6e9 commit 7cf098e

File tree

21 files changed

+216
-83
lines changed

21 files changed

+216
-83
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test --test_output=errors
1616
# creating (possibly empty) __init__.py files and adding them to the srcs of
1717
# Python targets as required.
1818
build --incompatible_default_to_explicit_init_py
19+
build --//python/config_settings:incompatible_default_to_explicit_init_py=True
1920

2021
# Ensure ongoing compatibility with this flag.
2122
common --incompatible_disallow_struct_provider_syntax

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ END_UNRELEASED_TEMPLATE
8585
evaluated, see [our docs](/pypi/download.html#customizing-requires-dist-resolution) on customizing `Requires-Dist` resolution.
8686
* (toolchains) Added Python versions 3.14.0, 3.13.8, 3.12.12, 3.11.14, 3.10.19, and 3.9.24
8787
from the [20251010] release.
88+
* (deps) (bzlmod) Upgraded to `bazel-skylib` version
89+
[1.8.2](https://github.com/bazelbuild/bazel-skylib/releases/tag/1.8.2)
8890

8991
[20251010]: https://github.com/astral-sh/python-build-standalone/releases/tag/20251010
9092

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module(
55
)
66

77
bazel_dep(name = "bazel_features", version = "1.21.0")
8-
bazel_dep(name = "bazel_skylib", version = "1.8.1")
8+
bazel_dep(name = "bazel_skylib", version = "1.8.2")
99
bazel_dep(name = "rules_cc", version = "0.1.5")
1010
bazel_dep(name = "platforms", version = "0.0.11")
1111

docs/api/rules_python/python/config_settings/index.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,65 @@ This is a transition flag and will be removed in a subsequent release.
2424
::::
2525
:::
2626

27+
::::{bzl:flag} build_python_zip
28+
Controls if a `py_binary/py_test` output is a self-executable zipapp.
29+
30+
When enabled, the output of `py_binary` or `py_test` targets will be a
31+
self-executable zipapp.
32+
33+
:::{note}
34+
This affects _all_ `py_binary` and `py_test` targets in the build, not
35+
only the target(s) specified on the command line.
36+
:::
37+
38+
Values:
39+
* `true`
40+
* `false`
41+
42+
This flag replaces the Bazel builtin `--build_python_zip` flag.
43+
44+
:::{versionadded} VERSION_NEXT_FEATURE
45+
:::
46+
::::
47+
48+
::::{bzl:flag} experimental_python_import_all_repositories
49+
Controls whether repository directories are added to the import path.
50+
51+
When enabled, the top-level directories in the runfiles root directory (which
52+
are presumbed to be repository directories) are added to the Python import
53+
search path.
54+
55+
It's recommended to set this to **`false`** to avoid external dependencies
56+
unexpectedly interferring with import searching.
57+
58+
Values;
59+
* `true` (default)
60+
* `false`
61+
62+
This flag replaces the Bazel builtin
63+
`--experimental_python_import_all_repositories` flag.
64+
65+
:::{versionadded} VERSION_NEXT_FEATURE
66+
:::
67+
::::
68+
69+
::::{bzl:flag} python_path
70+
A fallback path to use for Python for particular legacy Windows-specific code paths.
71+
72+
Deprecated, do not use. This flag is largely a no-op and was replaced by
73+
toolchains. It only remains for some legacy Windows code-paths that will
74+
be removed.
75+
76+
This flag replaces the Bazel builtin `--python_path` flag.
77+
78+
:::{deprecated} VERSION_NEXT_FEATURE
79+
Use toolchains instead.
80+
:::
81+
82+
:::{versionadded} VERSION_NEXT_FEATURE
83+
:::
84+
::::
85+
2786
:::{bzl:flag} python_version
2887
Determines the default hermetic Python toolchain version. This can be set to
2988
one of the values that `rules_python` maintains.
@@ -33,6 +92,29 @@ one of the values that `rules_python` maintains.
3392
Parses the value of the `python_version` and transforms it into a `X.Y` value.
3493
:::
3594

95+
::::{bzl:flag} incompatible_default_to_explicit_init_py
96+
Controls if missing `__init__.py` files are generated or not.
97+
98+
If false, `py_binary` and `py_test` will, for every `*.py` and `*.so` file,
99+
create `__init__.py` files for the containing directory, and all parent
100+
directories, that do not already have an `__init__.py` file. If true, this
101+
behavior is disabled.
102+
103+
It's recommended to disable this behavior to avoid surprising import effects
104+
from directories being importable when they otherwise wouldn't be, and for
105+
how it can interfere with implicit namespace packages.
106+
107+
Values:
108+
* `true`: do not generate missing `__init__.py` files
109+
* `false` (default): generate missing `__init__.py` files
110+
111+
This flag replaces the Bazel builtin
112+
`--incompatible_default_to_explicit_init_py` flag.
113+
114+
:::{versionadded} VERSION_NEXT_FEATURE
115+
:::
116+
::::
117+
36118
:::{bzl:target} is_python_*
37119
config_settings to match Python versions
38120

examples/build_file_generation/WORKSPACE

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,7 @@ workspace(name = "build_file_generation_example")
77
# file. When the symbol is loaded you can use the rule.
88
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
99

10-
######################################################################
11-
# We need rules_go and bazel_gazelle, to build the gazelle plugin from source.
12-
# Setup instructions for this section are at
13-
# https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
14-
# You may need to update the version of the rule, which is listed in the above
15-
# documentation.
16-
######################################################################
17-
18-
# Define an http_archive rule that will download the below ruleset,
19-
# test the sha, and extract the ruleset to you local bazel cache.
20-
21-
http_archive(
22-
name = "io_bazel_rules_go",
23-
sha256 = "9d72f7b8904128afb98d46bbef82ad7223ec9ff3718d419afb355fddd9f9484a",
24-
urls = [
25-
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v0.55.1/rules_go-v0.55.1.zip",
26-
"https://github.com/bazel-contrib/rules_go/releases/download/v0.55.1/rules_go-v0.55.1.zip",
27-
],
28-
)
29-
30-
# Download the bazel_gazelle ruleset.
31-
http_archive(
32-
name = "bazel_gazelle",
33-
sha256 = "75df288c4b31c81eb50f51e2e14f4763cb7548daae126817247064637fd9ea62",
34-
urls = [
35-
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz",
36-
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz",
37-
],
38-
)
39-
40-
# Load rules_go ruleset and expose the toolchain and dep rules.
41-
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
42-
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
43-
44-
# go_rules_dependencies is a function that registers external dependencies
45-
# needed by the Go rules.
46-
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
47-
go_rules_dependencies()
48-
49-
# go_rules_dependencies is a function that registers external dependencies
50-
# needed by the Go rules.
51-
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
52-
go_register_toolchains(version = "1.21.13")
53-
54-
# The following call configured the gazelle dependencies, Go environment and Go SDK.
55-
gazelle_dependencies()
56-
57-
# Remaining setup is for rules_python.
10+
# Setup rules_python.
5811

5912
# DON'T COPY_PASTE THIS.
6013
# Our example uses `local_repository` to point to the HEAD version of rules_python.
@@ -124,6 +77,53 @@ load("@pip//:requirements.bzl", "install_deps")
12477
# Initialize repositories for all packages in requirements_lock.txt.
12578
install_deps()
12679

80+
######################################################################
81+
# We need rules_go and bazel_gazelle, to build the gazelle plugin from source.
82+
# Setup instructions for this section are at
83+
# https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
84+
# You may need to update the version of the rule, which is listed in the above
85+
# documentation.
86+
######################################################################
87+
88+
# Define an http_archive rule that will download the below ruleset,
89+
# test the sha, and extract the ruleset to you local bazel cache.
90+
91+
http_archive(
92+
name = "io_bazel_rules_go",
93+
sha256 = "9d72f7b8904128afb98d46bbef82ad7223ec9ff3718d419afb355fddd9f9484a",
94+
urls = [
95+
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v0.55.1/rules_go-v0.55.1.zip",
96+
"https://github.com/bazel-contrib/rules_go/releases/download/v0.55.1/rules_go-v0.55.1.zip",
97+
],
98+
)
99+
100+
# Download the bazel_gazelle ruleset.
101+
http_archive(
102+
name = "bazel_gazelle",
103+
sha256 = "75df288c4b31c81eb50f51e2e14f4763cb7548daae126817247064637fd9ea62",
104+
urls = [
105+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz",
106+
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz",
107+
],
108+
)
109+
110+
# Load rules_go ruleset and expose the toolchain and dep rules.
111+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
112+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
113+
114+
# go_rules_dependencies is a function that registers external dependencies
115+
# needed by the Go rules.
116+
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
117+
go_rules_dependencies()
118+
119+
# go_rules_dependencies is a function that registers external dependencies
120+
# needed by the Go rules.
121+
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
122+
go_register_toolchains(version = "1.21.13")
123+
124+
# The following call configured the gazelle dependencies, Go environment and Go SDK.
125+
gazelle_dependencies()
126+
127127
# The rules_python gazelle extension has some third-party go dependencies
128128
# which we need to fetch in order to compile it.
129129
load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps")

gazelle/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test --test_output=errors
77
# creating (possibly empty) __init__.py files and adding them to the srcs of
88
# Python targets as required.
99
build --incompatible_default_to_explicit_init_py
10+
build --@rules_python//python/config_settings:incompatible_default_to_explicit_init_py=True
1011

1112
# Windows makes use of runfiles for some rules
1213
build --enable_runfiles

gazelle/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module(
44
compatibility_level = 1,
55
)
66

7-
bazel_dep(name = "bazel_skylib", version = "1.6.1")
7+
bazel_dep(name = "bazel_skylib", version = "1.8.2")
88
bazel_dep(name = "rules_python", version = "0.18.0")
99
bazel_dep(name = "rules_go", version = "0.55.1", repo_name = "io_bazel_rules_go")
1010
bazel_dep(name = "gazelle", version = "0.36.0", repo_name = "bazel_gazelle")

gazelle/WORKSPACE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ workspace(name = "rules_python_gazelle_plugin")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

5+
http_archive(
6+
name = "bazel_skylib",
7+
sha256 = "6e78f0e57de26801f6f564fa7c4a48dc8b36873e416257a92bbb0937eeac8446",
8+
urls = [
9+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
10+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
11+
],
12+
)
13+
14+
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
15+
16+
bazel_skylib_workspace()
17+
518
http_archive(
619
name = "io_bazel_rules_go",
720
sha256 = "9d72f7b8904128afb98d46bbef82ad7223ec9ff3718d419afb355fddd9f9484a",

internal_dev_deps.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ def rules_python_internal_deps():
6060

6161
http_archive(
6262
name = "bazel_skylib",
63-
sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f",
63+
sha256 = "6e78f0e57de26801f6f564fa7c4a48dc8b36873e416257a92bbb0937eeac8446",
6464
urls = [
65-
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
66-
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
65+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
66+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
6767
],
6868
)
6969

python/config_settings/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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")
3+
load("@rules_python_internal//:rules_python_config.bzl", "config")
34
load(
45
"//python/private:flags.bzl",
56
"AddSrcsToRunfilesFlag",
@@ -244,5 +245,28 @@ label_flag(
244245
bool_flag(
245246
name = "experimental_python_import_all_repositories",
246247
build_setting_default = True,
248+
scope = "universal",
249+
visibility = ["//visibility:public"],
250+
)
251+
252+
bool_flag(
253+
name = "build_python_zip",
254+
build_setting_default = config.build_python_zip_default,
255+
help = "Build python executable zip. Defaults to on on Windows, off on other platforms",
256+
scope = "universal",
257+
visibility = ["//visibility:public"],
258+
)
259+
260+
bool_flag(
261+
name = "incompatible_default_to_explicit_init_py",
262+
build_setting_default = False,
263+
scope = "universal",
264+
visibility = ["//visibility:public"],
265+
)
266+
267+
string_flag(
268+
name = "python_path",
269+
build_setting_default = "python",
270+
scope = "universal",
247271
visibility = ["//visibility:public"],
248272
)

0 commit comments

Comments
 (0)