Skip to content

Commit 564041b

Browse files
committed
Merge branch 'main' of https://github.com/bazel-contrib/rules_python into pr-3514
2 parents 1175508 + 708b07b commit 564041b

142 files changed

Lines changed: 2823 additions & 757 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.

.bazelci/presubmit.yml

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ buildifier:
3434
build_flags:
3535
- "--keep_going"
3636
- "--build_tag_filters=-integration-test"
37+
- "--verbose_failures"
3738
test_targets:
3839
- "--"
3940
- "..."
@@ -282,7 +283,7 @@ tasks:
282283
platform: rbe_ubuntu2204
283284
# TODO @aignas 2024-12-11: get the RBE working in CI for bazel 8.0
284285
# See https://github.com/bazelbuild/rules_python/issues/2499
285-
bazel: 7.x
286+
bazel: 8.x
286287
test_flags:
287288
- "--test_tag_filters=-integration-test,-acceptance-test"
288289
- "--extra_toolchains=@buildkite_config//config:cc-toolchain"
@@ -421,33 +422,6 @@ tasks:
421422
# We don't run pip_parse_vendored under Windows as the file checked in is
422423
# generated from a repository rule containing OS-specific rendered paths.
423424

424-
# The proto example is workspace-only; bzlmod functionality is covered
425-
# by examples/bzlmod/py_proto_library
426-
integration_test_py_proto_library_ubuntu_workspace:
427-
<<: *reusable_build_test_all
428-
<<: *common_workspace_flags
429-
name: "examples/py_proto_library: Ubuntu, workspace"
430-
working_directory: examples/py_proto_library
431-
platform: ubuntu2204
432-
integration_test_py_proto_library_debian_workspace:
433-
<<: *reusable_build_test_all
434-
<<: *common_workspace_flags
435-
name: "examples/py_proto_library: Debian, workspace"
436-
working_directory: examples/py_proto_library
437-
platform: debian11
438-
integration_test_py_proto_library_macos_workspace:
439-
<<: *reusable_build_test_all
440-
<<: *common_workspace_flags
441-
name: "examples/py_proto_library: MacOS, workspace"
442-
working_directory: examples/py_proto_library
443-
platform: macos_arm64
444-
integration_test_py_proto_library_windows_workspace:
445-
<<: *reusable_build_test_all
446-
<<: *common_workspace_flags
447-
name: "examples/py_proto_library: Windows, workspace"
448-
working_directory: examples/py_proto_library
449-
platform: windows
450-
451425
integration_test_pip_repository_annotations_ubuntu_workspace:
452426
<<: *reusable_build_test_all
453427
<<: *common_workspace_flags

.bazelrc.deleted_packages

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ common --deleted_packages=examples/bzlmod/libs/my_lib
88
common --deleted_packages=examples/bzlmod/other_module
99
common --deleted_packages=examples/bzlmod/other_module/other_module/pkg
1010
common --deleted_packages=examples/bzlmod/patches
11-
common --deleted_packages=examples/bzlmod/py_proto_library
12-
common --deleted_packages=examples/bzlmod/py_proto_library/example.com/another_proto
13-
common --deleted_packages=examples/bzlmod/py_proto_library/example.com/proto
14-
common --deleted_packages=examples/bzlmod/py_proto_library/foo_external
1511
common --deleted_packages=examples/bzlmod/runfiles
1612
common --deleted_packages=examples/bzlmod/tests
1713
common --deleted_packages=examples/bzlmod/tests/other_module
@@ -22,9 +18,6 @@ common --deleted_packages=examples/multi_python_versions/tests
2218
common --deleted_packages=examples/pip_parse
2319
common --deleted_packages=examples/pip_parse_vendored
2420
common --deleted_packages=examples/pip_repository_annotations
25-
common --deleted_packages=examples/py_proto_library
26-
common --deleted_packages=examples/py_proto_library/example.com/another_proto
27-
common --deleted_packages=examples/py_proto_library/example.com/proto
2821
common --deleted_packages=gazelle
2922
common --deleted_packages=gazelle/examples/bzlmod_build_file_generation
3023
common --deleted_packages=gazelle/examples/bzlmod_build_file_generation/other_module/other_module/pkg

CHANGELOG.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,20 @@ END_UNRELEASED_TEMPLATE
5858

5959
{#v0-0-0-changed}
6060
### Changed
61+
* **DEPRECATED: implicit zipapp support**
62+
* Implicit zipapp output of `py_binary`/`py_test` has been deprecated and
63+
replaced by separate {obj}`py_zipapp_binary` and {obj}`py_zipapp_test`
64+
rules. See
65+
[#3567](https://github.com/bazel-contrib/rules_python/issues/3567)
66+
for a detailed migration guide.
67+
* (toolchains) stop exposing config settings in python toolchain alias repos.
68+
Please consider depending on the flags defined in
69+
`//python/config_setting/...` and the `@platforms` package instead.
6170
* (binaries/tests) The `PYTHONBREAKPOINT` environment variable is automatically inherited
6271
* (binaries/tests) The {obj}`stamp` attribute now transitions the Bazel builtin
6372
{obj}`--stamp` flag.
73+
* (pypi) Now the RECORD file patches will follow the quoted or unquoted filenames convention
74+
in order to make `pytorch` and friends easier to patch.
6475

6576
{#v0-0-0-fixed}
6677
### Fixed
@@ -79,6 +90,45 @@ END_UNRELEASED_TEMPLATE
7990
* (binaries/tests) Build information is now included in binaries and tests.
8091
Use the `bazel_binary_info` module to access it. The {flag}`--stamp` flag will
8192
add {flag}`--workspace_status` information.
93+
* (gazelle) A new directive `python_generate_pyi_deps` has been added. When
94+
`true`, a `py_*` target's `pyi_srcs` attribute will be set if any `.pyi` files
95+
that are associated with the target's `srcs` are present.
96+
([#3354](https://github.com/bazel-contrib/rules_python/issues/3354)).
97+
* (zipapp) {obj}`py_zipapp_binary` and {obj}`py_zipapp_test` rules added. These
98+
will replace `--build_python_zip` and the zip output group of
99+
`py_binary/py_test`. The zipapp rules support more functionality, correctness,
100+
and have better build performance.
101+
* (toolchains) Added {obj}`PyExecToolsInfo.exec_runtime` for more easily
102+
getting an RBE-compatible runtime to use for build actions.
103+
* (providers) {obj}`PyExecutableInfo` has several new fields to aid packaging
104+
of binaries: {obj}`PyExecutableInfo.app_runfiles`,
105+
{obj}`PyExecutableInfo.interpreter_args`,
106+
{obj}`PyExecutableInfo.stage2_bootstrap`, and
107+
{obj}`PyExecutableInfo.venv_python_exe`.
108+
109+
{#v1-8-3}
110+
## [1.8.3] - 2026-01-27
111+
112+
{#v1-8-3-fixed}
113+
### Fixed
114+
* (pipstar) Fix whl extraction on Windows when bazelrc has XX flags.
115+
Fixes [#3543](https://github.com/bazel-contrib/rules_python/issues/3543).
116+
117+
{#v1-8-2}
118+
## [1.8.2] - 2026-01-24
119+
120+
{#v1-8-2-fixed}
121+
### Fixed
122+
* (venvs) relax the C library filename check to make tensorflow work
123+
Fixes [#3524](https://github.com/bazel-contrib/rules_python/issues/3529).
124+
125+
{#v1-8-1}
126+
## [1.8.1] - 2026-01-20
127+
128+
{#v1-8-1-fixed}
129+
### Fixed
130+
* (pipstar) Extra resolution that refers back to the package being resolved works again.
131+
Fixes [#3524](https://github.com/bazel-contrib/rules_python/issues/3524).
82132

83133
{#v1-8-0}
84134
## [1.8.0] - 2025-12-19
@@ -141,6 +191,7 @@ END_UNRELEASED_TEMPLATE
141191
### Fixed
142192
* (gazelle) Remove {obj}`py_binary` targets with invalid `srcs`. This includes files
143193
that are not generated or regular files.
194+
[#3046](https://github.com/bazel-contrib/rules_python/pull/3046)
144195
* (runfiles) Fix incorrect Python runfiles path assumption - the existing
145196
implementation assumes that it is always four levels below the runfiles
146197
directory, leading to incorrect path checks

README.md

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

77
This repository is the home of the core Python rules -- `py_library`,
8-
`py_binary`, `py_test`, `py_proto_library`, and related symbols that provide the basis for Python
8+
`py_binary`, `py_test`, and related symbols that provide the basis for Python
99
support in Bazel. It also contains package installation rules for integrating with PyPI and other indices.
1010

1111
Documentation for rules_python is at <https://rules-python.readthedocs.io> and in the

docs/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ sphinx_stardocs(
125125
"//python/private/pypi:pkg_aliases_bzl",
126126
"//python/private/pypi:whl_config_setting_bzl",
127127
"//python/private/pypi:whl_library_bzl",
128+
"//python/private/zipapp:py_zipapp_rule_bzl",
128129
"//python/uv:lock_bzl",
129130
"//python/uv:uv_bzl",
130131
"//python/uv:uv_toolchain_bzl",
131132
"//python/uv:uv_toolchain_info_bzl",
133+
"//python/zipapp:py_zipapp_binary_bzl",
134+
"//python/zipapp:py_zipapp_test_bzl",
132135
] + ([
133136
# This depends on @pythons_hub, which is only created under bzlmod,
134137
"//python/extensions:pip_bzl",

docs/environment-variables.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ would be:
1111
python -Xaaa /path/to/file.py
1212
```
1313

14-
This feature is likely to be useful for the integration of debuggers. For example,
14+
This feature is useful for the integration of debuggers. For example,
1515
it would be possible to configure `RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` to
1616
be set to `/path/to/debugger.py --port 12344 --file`, resulting
1717
in the command executed being:
@@ -22,6 +22,8 @@ python /path/to/debugger.py --port 12345 --file /path/to/file.py
2222

2323
:::{seealso}
2424
The {bzl:obj}`interpreter_args` attribute.
25+
26+
The guide on {any}`How to integrate a debugger`
2527
:::
2628

2729
:::{versionadded} 1.3.0

docs/howto/debuggers.md

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
# How to integrate a debugger
55

6-
This guide explains how to use the {obj}`--debugger` flag to integrate a debugger
6+
This guide explains how to integrate a debugger
77
with your Python applications built with `rules_python`.
88

9-
## Basic Usage
9+
There are two ways available: the {obj}`--debugger` flag, and the {any}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable.
10+
11+
## {obj}`--debugger` flag
12+
13+
### Basic Usage
1014

1115
The {obj}`--debugger` flag allows you to inject an extra dependency into `py_test`
1216
and `py_binary` targets so that they have a custom debugger available at
@@ -32,7 +36,7 @@ The specified target must be in the requirements.txt file used with
3236
`pip.parse()` to make it available to Bazel.
3337
:::
3438

35-
## Python `PYTHONBREAKPOINT` Environment Variable
39+
### Python `PYTHONBREAKPOINT` Environment Variable
3640

3741
For more fine-grained control over debugging, especially for programmatic breakpoints,
3842
you can leverage the Python built-in `breakpoint()` function and the
@@ -52,7 +56,7 @@ PYTHONBREAKPOINT=pudb.set_trace bazel run \
5256

5357
For more details on `PYTHONBREAKPOINT`, refer to the [Python documentation](https://docs.python.org/3/library/functions.html#breakpoint).
5458

55-
## Setting a default debugger
59+
### Setting a default debugger
5660

5761
By adding settings to your user or project `.bazelrc` files, you can have
5862
these settings automatically added to your bazel invocations. e.g.
@@ -64,3 +68,139 @@ common --test_env=PYTHONBREAKPOINT=pudb.set_trace
6468

6569
Note that `--test_env` isn't strictly necessary. The `py_test` and `py_binary`
6670
rules will respect the `PYTHONBREAKPOINT` environment variable in your shell.
71+
72+
## debugpy (e.g. vscode)
73+
74+
You can integrate `debugpy` (i.e. the debugger used in vscode or PyCharm) by using a launcher script. This method leverages {any}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` to inject the debugger into the Bazel-managed Python process.
75+
76+
For the remainder of this document, we assume you are using vscode.
77+
78+
![VS Code debugpy demo](https://raw.githubusercontent.com/shayanhoshyari/issue-reports/refs/heads/main/rules_python/vscode_debugger/docs/demo.gif)
79+
80+
81+
1. **Create a launcher script**: Save the following Python script as `.vscode/debugpy/launch.py` (or another location, adjusting `launch.json` accordingly). This script bridges VS Code's debugger with Bazel.
82+
83+
<details>
84+
<summary><code>launch.py</code></summary>
85+
86+
```python
87+
"""
88+
Launcher script for VS Code (debugpy).
89+
90+
This script is not managed by Bazel; it is invoked by VS Code's launch.json to
91+
wrap the Bazel command, injecting the debugger into the runtime environment.
92+
"""
93+
94+
import argparse
95+
import os
96+
import shlex
97+
import subprocess
98+
import sys
99+
from typing import cast
100+
101+
def main() -> None:
102+
parser = argparse.ArgumentParser(description="Launch bazel debugpy with test or run.")
103+
parser.add_argument("mode", choices=["test", "run"], help="Choose whether to run a bazel test or run.")
104+
parser.add_argument("args", help="The bazel target to test or run (e.g., //foo:bar) and any additional args")
105+
args = parser.parse_args()
106+
107+
# Import debugpy, provided by VS Code
108+
try:
109+
# debugpy._vendored is needed for force_pydevd to perform path manipulation.
110+
import debugpy._vendored # type: ignore[import-not-found]
111+
112+
# pydev_monkey patches os and subprocess functions to handle new launched processes.
113+
from _pydev_bundle import pydev_monkey # type: ignore[import-not-found]
114+
except ImportError as exc:
115+
print(f"Error: This script must be run via VS Code's debug adapter. Details: {exc}")
116+
sys.exit(-1)
117+
118+
# Prepare arguments for the monkey-patched process.
119+
# is_exec=False ensures we don't replace the current process immediately.
120+
patched_args = cast(list[str], pydev_monkey.patch_args(["python", "dummy.py"], is_exec=False))
121+
pydev_monkey.send_process_created_message()
122+
123+
# Extract the injected arguments (skipping the dummy python executable and script).
124+
# These args invoke the pydevd entrypoint which connects back to the debugger.
125+
rules_python_interpreter_args = " ".join(patched_args[1:-1])
126+
127+
bzl_args = shlex.split(args.args)
128+
if not bzl_args:
129+
print("Error: At least one argument (the target) is required.")
130+
sys.exit(-1)
131+
132+
cmd = [
133+
"bazel",
134+
args.mode,
135+
# Propagate environment variables to the test/run environment.
136+
"--test_env=PYDEVD_RESOLVE_SYMLINKS",
137+
"--test_env=RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS",
138+
"--test_env=IDE_PROJECT_ROOTS",
139+
bzl_args[0],
140+
]
141+
142+
if bzl_args[1:]:
143+
if args.mode == "run":
144+
# Append extra arguments for 'run' mode.
145+
cmd.append("--")
146+
cmd.extend(bzl_args[1:])
147+
elif args.mode == "test":
148+
# Append extra arguments for 'test' mode.
149+
cmd.extend([f"--test_arg={arg}" for arg in bzl_args[1:]])
150+
151+
env = {
152+
**os.environ.copy(),
153+
# Inject the debugger arguments into the rules_python toolchain.
154+
"RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS": rules_python_interpreter_args,
155+
# Ensure breakpoints hit the original source files, not Bazel's symlinks.
156+
"PYDEVD_RESOLVE_SYMLINKS": "1",
157+
}
158+
159+
# Execute Bazel.
160+
result = subprocess.run(cmd, env=env, check=False)
161+
sys.exit(result.returncode)
162+
163+
if __name__ == "__main__":
164+
main()
165+
```
166+
</details>
167+
168+
2. **Configure `launch.json`**: Add the following configurations to your `.vscode/launch.json`. This tells VS Code to use the launcher script.
169+
170+
<details>
171+
<summary><code>launch.json</code></summary>
172+
173+
```json
174+
{
175+
"version": "0.2.0",
176+
"configurations": [
177+
{
178+
"name": "Python: Bazel py run",
179+
"type": "debugpy",
180+
"request": "launch",
181+
"program": "${workspaceFolder}/.vscode/debugpy/launch.py",
182+
"args": ["run", "${input:BazelArgs}"],
183+
"console": "integratedTerminal"
184+
},
185+
{
186+
"name": "Python: Bazel py test",
187+
"type": "debugpy",
188+
"request": "launch",
189+
"program": "${workspaceFolder}/.vscode/debugpy/launch.py",
190+
"args": ["test", "${input:BazelArgs}"],
191+
"console": "integratedTerminal"
192+
}
193+
],
194+
"inputs": [
195+
{
196+
"id": "BazelArgs",
197+
"type": "promptString",
198+
"description": "Bazel target and arguments (e.g., //foo:bar --my-arg)"
199+
}
200+
]
201+
}
202+
```
203+
</details>
204+
205+
Note: If you find `justMyCode` behavior is incompatible with Bazel's symlinks (causing breakpoints to be missed), you can set `"justMyCode": false` in `launch.json` and use the `IDE_PROJECT_ROOTS` environment variable (set to `"${workspaceFolder}"`) to explicitly map your workspace.
206+

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:::{topic} Core rules
66

77
The core Python rules -- `py_library`, `py_binary`, `py_test`,
8-
`py_proto_library`, and related symbols that provide the basis for Python
8+
and related symbols that provide the basis for Python
99
support in Bazel.
1010

1111
When using Bazel 6 (or earlier), the core rules are bundled into the Bazel binary, and the symbols

0 commit comments

Comments
 (0)