xref: /aosp_15_r20/external/bazelbuild-rules_python/BZLMOD_SUPPORT.md (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Bzlmod support
2*60517a1eSAndroid Build Coastguard Worker
3*60517a1eSAndroid Build Coastguard Worker## `rules_python` `bzlmod` support
4*60517a1eSAndroid Build Coastguard Worker
5*60517a1eSAndroid Build Coastguard Worker- Status: Beta
6*60517a1eSAndroid Build Coastguard Worker- Full Feature Parity: No
7*60517a1eSAndroid Build Coastguard Worker
8*60517a1eSAndroid Build Coastguard WorkerSome features are missing or broken, and the public APIs are not yet stable.
9*60517a1eSAndroid Build Coastguard Worker
10*60517a1eSAndroid Build Coastguard Worker## Configuration
11*60517a1eSAndroid Build Coastguard Worker
12*60517a1eSAndroid Build Coastguard WorkerThe releases page will give you the latest version number, and a basic example.  The release page is located [here](/bazelbuild/rules_python/releases).
13*60517a1eSAndroid Build Coastguard Worker
14*60517a1eSAndroid Build Coastguard Worker## What is bzlmod?
15*60517a1eSAndroid Build Coastguard Worker
16*60517a1eSAndroid Build Coastguard Worker> Bazel supports external dependencies, source files (both text and binary) used in your build that are not from your workspace. For example, they could be a ruleset hosted in a GitHub repo, a Maven artifact, or a directory on your local machine outside your current workspace.
17*60517a1eSAndroid Build Coastguard Worker>
18*60517a1eSAndroid Build Coastguard Worker> As of Bazel 6.0, there are two ways to manage external dependencies with Bazel: the traditional, repository-focused WORKSPACE system, and the newer module-focused MODULE.bazel system (codenamed Bzlmod, and enabled with the flag `--enable_bzlmod`). The two systems can be used together, but Bzlmod is replacing the WORKSPACE system in future Bazel releases.
19*60517a1eSAndroid Build Coastguard Worker> -- <cite>https://bazel.build/external/overview</cite>
20*60517a1eSAndroid Build Coastguard Worker
21*60517a1eSAndroid Build Coastguard Worker## Examples
22*60517a1eSAndroid Build Coastguard Worker
23*60517a1eSAndroid Build Coastguard WorkerWe have two examples that demonstrate how to configure `bzlmod`.
24*60517a1eSAndroid Build Coastguard Worker
25*60517a1eSAndroid Build Coastguard WorkerThe first example is in [examples/bzlmod](examples/bzlmod), and it demonstrates basic bzlmod configuration.
26*60517a1eSAndroid Build Coastguard WorkerA user does not use `local_path_override` stanza and would define the version in the `bazel_dep` line.
27*60517a1eSAndroid Build Coastguard Worker
28*60517a1eSAndroid Build Coastguard WorkerA second example, in [examples/bzlmod_build_file_generation](examples/bzlmod_build_file_generation) demonstrates the use of `bzlmod` to configure `gazelle` support for `rules_python`.
29*60517a1eSAndroid Build Coastguard Worker
30*60517a1eSAndroid Build Coastguard Worker## Feature parity
31*60517a1eSAndroid Build Coastguard Worker
32*60517a1eSAndroid Build Coastguard WorkerThis rule set does not have full feature partity with the older `WORKSPACE` type configuration:
33*60517a1eSAndroid Build Coastguard Worker
34*60517a1eSAndroid Build Coastguard Worker1. Gazelle does not support finding deps in sub-modules.  For instance we can have a dep like ` "@our_other_module//other_module/pkg:lib",` in a `py_test` definition.
35*60517a1eSAndroid Build Coastguard Worker2. We have some features that are still not fully flushed out, and the user interface may change.
36*60517a1eSAndroid Build Coastguard Worker
37*60517a1eSAndroid Build Coastguard WorkerCheck ["issues"](/bazelbuild/rules_python/issues) for an up to date list.
38*60517a1eSAndroid Build Coastguard Worker
39*60517a1eSAndroid Build Coastguard Worker## Differences in behavior from WORKSPACE
40*60517a1eSAndroid Build Coastguard Worker
41*60517a1eSAndroid Build Coastguard Worker### Default toolchain is not the local system Python
42*60517a1eSAndroid Build Coastguard Worker
43*60517a1eSAndroid Build Coastguard WorkerUnder bzlmod, the default toolchain is no longer based on the locally installed
44*60517a1eSAndroid Build Coastguard Workersystem Python. Instead, a recent Python version using the pre-built,
45*60517a1eSAndroid Build Coastguard Workerstandalone runtimes are used.
46*60517a1eSAndroid Build Coastguard Worker
47*60517a1eSAndroid Build Coastguard WorkerIf you need the local system Python to be your toolchain, then it's suggested
48*60517a1eSAndroid Build Coastguard Workerthat you setup and configure your own toolchain and register it. Note that using
49*60517a1eSAndroid Build Coastguard Workerthe local system's Python is not advised because will vary between users and
50*60517a1eSAndroid Build Coastguard Workerplatforms.
51*60517a1eSAndroid Build Coastguard Worker
52*60517a1eSAndroid Build Coastguard WorkerIf you want to use the same toolchain as what WORKSPACE used, then manually
53*60517a1eSAndroid Build Coastguard Workerregister the builtin Bazel Python toolchain by doing
54*60517a1eSAndroid Build Coastguard Worker`register_toolchains("@bazel_tools//tools/python:autodetecting_toolchain")`.
55*60517a1eSAndroid Build Coastguard Worker**IMPORTANT: this should only be done in a root module, and may intefere with
56*60517a1eSAndroid Build Coastguard Workerthe toolchains rules_python registers**.
57*60517a1eSAndroid Build Coastguard Worker
58*60517a1eSAndroid Build Coastguard WorkerNOTE: Regardless of your toolchain, due to
59*60517a1eSAndroid Build Coastguard Worker[#691](https://github.com/bazelbuild/rules_python/issues/691), `rules_python`
60*60517a1eSAndroid Build Coastguard Workerstill relies on a local Python being available to bootstrap the program before
61*60517a1eSAndroid Build Coastguard Workerhanding over execution to the toolchain Python.
62