Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test --test_output=errors
# creating (possibly empty) __init__.py files and adding them to the srcs of
# Python targets as required.
build --incompatible_default_to_explicit_init_py
build --//python/config_settings:incompatible_default_to_explicit_init_py=True

# Ensure ongoing compatibility with this flag.
common --incompatible_disallow_struct_provider_syntax
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ END_UNRELEASED_TEMPLATE
evaluated, see [our docs](/pypi/download.html#customizing-requires-dist-resolution) on customizing `Requires-Dist` resolution.
* (toolchains) Added Python versions 3.14.0, 3.13.8, 3.12.12, 3.11.14, 3.10.19, and 3.9.24
from the [20251010] release.
* (deps) (bzlmod) Upgraded to `bazel-skylib` version
[1.8.2](https://github.com/bazelbuild/bazel-skylib/releases/tag/1.8.2)

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

Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module(
)

bazel_dep(name = "bazel_features", version = "1.21.0")
bazel_dep(name = "bazel_skylib", version = "1.8.1")
bazel_dep(name = "bazel_skylib", version = "1.8.2")
bazel_dep(name = "rules_cc", version = "0.1.5")
bazel_dep(name = "platforms", version = "0.0.11")

Expand Down
82 changes: 82 additions & 0 deletions docs/api/rules_python/python/config_settings/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,65 @@ This is a transition flag and will be removed in a subsequent release.
::::
:::

::::{bzl:flag} build_python_zip
Controls if a `py_binary/py_test` output is a self-executable zipapp.

When enabled, the output of `py_binary` or `py_test` targets will be a
self-executable zipapp.

:::{note}
This affects _all_ `py_binary` and `py_test` targets in the build, not
only the target(s) specified on the command line.
:::

Values:
* `true`
* `false`

This flag replaces the Bazel builtin `--build_python_zip` flag.

:::{versionadded} VERSION_NEXT_FEATURE
:::
::::

::::{bzl:flag} experimental_python_import_all_repositories
Controls whether repository directories are added to the import path.

When enabled, the top-level directories in the runfiles root directory (which
are presumbed to be repository directories) are added to the Python import
search path.

It's recommended to set this to **`false`** to avoid external dependencies
unexpectedly interferring with import searching.

Values;
* `true` (default)
* `false`

This flag replaces the Bazel builtin
`--experimental_python_import_all_repositories` flag.

:::{versionadded} VERSION_NEXT_FEATURE
:::
::::

::::{bzl:flag} python_path
A fallback path to use for Python for particular legacy Windows-specific code paths.

Deprecated, do not use. This flag is largely a no-op and was replaced by
toolchains. It only remains for some legacy Windows code-paths that will
be removed.

This flag replaces the Bazel builtin `--python_path` flag.

:::{deprecated} VERSION_NEXT_FEATURE
Use toolchains instead.
:::

:::{versionadded} VERSION_NEXT_FEATURE
:::
::::

:::{bzl:flag} python_version
Determines the default hermetic Python toolchain version. This can be set to
one of the values that `rules_python` maintains.
Expand All @@ -33,6 +92,29 @@ one of the values that `rules_python` maintains.
Parses the value of the `python_version` and transforms it into a `X.Y` value.
:::

::::{bzl:flag} incompatible_default_to_explicit_init_py
Controls if missing `__init__.py` files are generated or not.

If false, `py_binary` and `py_test` will, for every `*.py` and `*.so` file,
create `__init__.py` files for the containing directory, and all parent
directories, that do not already have an `__init__.py` file. If true, this
behavior is disabled.

It's recommended to disable this behavior to avoid surprising import effects
from directories being importable when they otherwise wouldn't be, and for
how it can interfere with implicit namespace packages.

Values:
* `true`: do not generate missing `__init__.py` files
* `false` (default): generate missing `__init__.py` files

This flag replaces the Bazel builtin
`--incompatible_default_to_explicit_init_py` flag.

:::{versionadded} VERSION_NEXT_FEATURE
:::
::::

:::{bzl:target} is_python_*
config_settings to match Python versions

Expand Down
96 changes: 48 additions & 48 deletions examples/build_file_generation/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,7 @@ workspace(name = "build_file_generation_example")
# file. When the symbol is loaded you can use the rule.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

######################################################################
# We need rules_go and bazel_gazelle, to build the gazelle plugin from source.
# Setup instructions for this section are at
# https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
# You may need to update the version of the rule, which is listed in the above
# documentation.
######################################################################

# Define an http_archive rule that will download the below ruleset,
# test the sha, and extract the ruleset to you local bazel cache.

http_archive(
name = "io_bazel_rules_go",
sha256 = "9d72f7b8904128afb98d46bbef82ad7223ec9ff3718d419afb355fddd9f9484a",
urls = [
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v0.55.1/rules_go-v0.55.1.zip",
"https://github.com/bazel-contrib/rules_go/releases/download/v0.55.1/rules_go-v0.55.1.zip",
],
)

# Download the bazel_gazelle ruleset.
http_archive(
name = "bazel_gazelle",
sha256 = "75df288c4b31c81eb50f51e2e14f4763cb7548daae126817247064637fd9ea62",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz",
],
)

# Load rules_go ruleset and expose the toolchain and dep rules.
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

# go_rules_dependencies is a function that registers external dependencies
# needed by the Go rules.
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
go_rules_dependencies()

# go_rules_dependencies is a function that registers external dependencies
# needed by the Go rules.
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
go_register_toolchains(version = "1.21.13")

# The following call configured the gazelle dependencies, Go environment and Go SDK.
gazelle_dependencies()

# Remaining setup is for rules_python.
# Setup rules_python.

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

######################################################################
# We need rules_go and bazel_gazelle, to build the gazelle plugin from source.
# Setup instructions for this section are at
# https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
# You may need to update the version of the rule, which is listed in the above
# documentation.
######################################################################

# Define an http_archive rule that will download the below ruleset,
# test the sha, and extract the ruleset to you local bazel cache.

http_archive(
name = "io_bazel_rules_go",
sha256 = "9d72f7b8904128afb98d46bbef82ad7223ec9ff3718d419afb355fddd9f9484a",
urls = [
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v0.55.1/rules_go-v0.55.1.zip",
"https://github.com/bazel-contrib/rules_go/releases/download/v0.55.1/rules_go-v0.55.1.zip",
],
)

# Download the bazel_gazelle ruleset.
http_archive(
name = "bazel_gazelle",
sha256 = "75df288c4b31c81eb50f51e2e14f4763cb7548daae126817247064637fd9ea62",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz",
],
)

# Load rules_go ruleset and expose the toolchain and dep rules.
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

# go_rules_dependencies is a function that registers external dependencies
# needed by the Go rules.
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
go_rules_dependencies()

# go_rules_dependencies is a function that registers external dependencies
# needed by the Go rules.
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
go_register_toolchains(version = "1.21.13")

# The following call configured the gazelle dependencies, Go environment and Go SDK.
gazelle_dependencies()

# The rules_python gazelle extension has some third-party go dependencies
# which we need to fetch in order to compile it.
load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps")
Expand Down
1 change: 1 addition & 0 deletions gazelle/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test --test_output=errors
# creating (possibly empty) __init__.py files and adding them to the srcs of
# Python targets as required.
build --incompatible_default_to_explicit_init_py
build --@rules_python//python/config_settings:incompatible_default_to_explicit_init_py=True

# Windows makes use of runfiles for some rules
build --enable_runfiles
Expand Down
2 changes: 1 addition & 1 deletion gazelle/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "bazel_skylib", version = "1.8.2")
bazel_dep(name = "rules_python", version = "0.18.0")
bazel_dep(name = "rules_go", version = "0.55.1", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.36.0", repo_name = "bazel_gazelle")
Expand Down
13 changes: 13 additions & 0 deletions gazelle/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ workspace(name = "rules_python_gazelle_plugin")

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

http_archive(
name = "bazel_skylib",
sha256 = "6e78f0e57de26801f6f564fa7c4a48dc8b36873e416257a92bbb0937eeac8446",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
name = "io_bazel_rules_go",
sha256 = "9d72f7b8904128afb98d46bbef82ad7223ec9ff3718d419afb355fddd9f9484a",
Expand Down
6 changes: 3 additions & 3 deletions internal_dev_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def rules_python_internal_deps():

http_archive(
name = "bazel_skylib",
sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f",
sha256 = "6e78f0e57de26801f6f564fa7c4a48dc8b36873e416257a92bbb0937eeac8446",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
],
)

Expand Down
24 changes: 24 additions & 0 deletions python/config_settings/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION", "MINOR_MAPPING", "PYTHON_VERSIONS")
load("@rules_python_internal//:rules_python_config.bzl", "config")
load(
"//python/private:flags.bzl",
"AddSrcsToRunfilesFlag",
Expand Down Expand Up @@ -244,5 +245,28 @@ label_flag(
bool_flag(
name = "experimental_python_import_all_repositories",
build_setting_default = True,
scope = "universal",
visibility = ["//visibility:public"],
)

bool_flag(
name = "build_python_zip",
build_setting_default = config.build_python_zip_default,
help = "Build python executable zip. Defaults to on on Windows, off on other platforms",
scope = "universal",
visibility = ["//visibility:public"],
)

bool_flag(
name = "incompatible_default_to_explicit_init_py",
build_setting_default = False,
scope = "universal",
visibility = ["//visibility:public"],
)

string_flag(
name = "python_path",
build_setting_default = "python",
scope = "universal",
visibility = ["//visibility:public"],
)
2 changes: 2 additions & 0 deletions python/private/common_labels.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ labels = struct(
# keep sorted
ADD_SRCS_TO_RUNFILES = str(Label("//python/config_settings:add_srcs_to_runfiles")),
BOOTSTRAP_IMPL = str(Label("//python/config_settings:bootstrap_impl")),
BUILD_PYTHON_ZIP = str(Label("//python/config_settings:build_python_zip")),
EXEC_TOOLS_TOOLCHAIN = str(Label("//python/config_settings:exec_tools_toolchain")),
PIP_ENV_MARKER_CONFIG = str(Label("//python/config_settings:pip_env_marker_config")),
NONE = str(Label("//python:none")),
Expand All @@ -18,6 +19,7 @@ labels = struct(
PRECOMPILE = str(Label("//python/config_settings:precompile")),
PRECOMPILE_SOURCE_RETENTION = str(Label("//python/config_settings:precompile_source_retention")),
PYC_COLLECTION = str(Label("//python/config_settings:pyc_collection")),
PYTHON_IMPORT_ALL_REPOSITORIES = str(Label("//python/config_settings:experimental_python_import_all_repositories")),
PYTHON_SRC = str(Label("//python/bin:python_src")),
PYTHON_VERSION = str(Label("//python/config_settings:python_version")),
PYTHON_VERSION_MAJOR_MINOR = str(Label("//python/config_settings:python_version_major_minor")),
Expand Down
2 changes: 1 addition & 1 deletion python/private/flags.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def read_possibly_native_flag(ctx, flag_name):
return _POSSIBLY_NATIVE_FLAGS[flag_name][0](ctx)
else:
# Starlark definition of "--foo" is assumed to be a label dependency named "_foo".
return getattr(ctx.attr, "_" + flag_name)[BuildSettingInfo].value
return getattr(ctx.attr, "_" + flag_name + "_flag")[BuildSettingInfo].value

def _AddSrcsToRunfilesFlag_is_enabled(ctx):
value = ctx.attr._add_srcs_to_runfiles_flag[BuildSettingInfo].value
Expand Down
2 changes: 2 additions & 0 deletions python/private/internal_config_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ _ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0"

_CONFIG_TEMPLATE = """
config = struct(
build_python_zip_default = {build_python_zip_default},
enable_pystar = True,
enable_pipstar = {enable_pipstar},
enable_deprecation_warnings = {enable_deprecation_warnings},
Expand Down Expand Up @@ -96,6 +97,7 @@ def _internal_config_repo_impl(rctx):
builtin_py_cc_link_params_provider = "PyCcLinkParamsProvider"

rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format(
build_python_zip_default = repo_utils.get_platforms_os_name(rctx) == "windows",
enable_pipstar = _bool_from_environ(rctx, _ENABLE_PIPSTAR_ENVVAR_NAME, _ENABLE_PIPSTAR_DEFAULT),
enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT),
builtin_py_info_symbol = builtin_py_info_symbol,
Expand Down
10 changes: 10 additions & 0 deletions python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ EXECUTABLE_ATTRS = dicts.add(
AGNOSTIC_EXECUTABLE_ATTRS,
PY_SRCS_ATTRS,
IMPORTS_ATTRS,
# starlark flags attributes
{
"_build_python_zip_flag": attr.label(default = "//python/config_settings:build_python_zip"),
"_default_to_explicit_init_py_flag": attr.label(default = "//python/config_settings:incompatible_default_to_explicit_init_py"),
"_python_import_all_repositories_flag": attr.label(default = "//python/config_settings:experimental_python_import_all_repositories"),
"_python_path_flag": attr.label(default = "//python/config_settings:python_path"),
},
{
"interpreter_args": lambda: attrb.StringList(
doc = """
Expand Down Expand Up @@ -1136,6 +1143,9 @@ def _get_runtime_details(ctx, semantics):
# TOOD(bazelbuild/bazel#7901): Remove this once --python_path flag is removed.

flag_interpreter_path = read_possibly_native_flag(ctx, "python_path")
if not flag_interpreter_path.startswith("python") and not paths.is_absolute(flag_interpreter_path):
fail("'python_path' must be an absolute path or a name to be resolved from the system PATH (e.g., 'python', 'python3').")

toolchain_runtime, effective_runtime = _maybe_get_runtime_from_ctx(ctx)
if not effective_runtime:
# Clear these just in case
Expand Down
6 changes: 3 additions & 3 deletions python/private/py_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def py_repositories(transition_settings = []):
)
http_archive(
name = "bazel_skylib",
sha256 = "d00f1389ee20b60018e92644e0948e16e350a7707219e7a390fb0a99b6ec9262",
sha256 = "6e78f0e57de26801f6f564fa7c4a48dc8b36873e416257a92bbb0937eeac8446",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.0/bazel-skylib-1.7.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.0/bazel-skylib-1.7.0.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.8.2/bazel-skylib-1.8.2.tar.gz",
],
)
http_archive(
Expand Down
Loading