8000 doc: correct toolchain usage in repository_rule context docs (#2510) · bazel-contrib/rules_python@66a8b5b · GitHub
[go: up one dir, main page]

Skip to content

Commit 66a8b5b

Browse files
aignasrickeylev
andauthored
doc: correct toolchain usage in repository_rule context docs (#2510)
This clarifies a few points about how to use the python interpreter in the repository context. It also fixes outdated documentation as noted by some users of 1.0. See discussion: #2509 Fixes #2494 --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
1 parent 9306393 commit 66a8b5b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

docs/toolchains.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,43 @@ existing attributes:
184184
* Adding additional Python versions via {bzl:obj}`python.single_version_override` or
185185
{bzl:obj}`python.single_version_platform_override`.
186186

187+
### Using defined toolchains from WORKSPACE
188+
189+
It is possible to use toolchains defined in `MODULE.bazel` in `WORKSPACE`. For example
190+
the following `MODULE.bazel` and `WORKSPACE` provides a working {bzl:obj}`pip_parse` setup:
191+
```starlark
192+
# File: WORKSPACE
193+
load("@rules_python//python:repositories.bzl", "py_repositories")
194+
195+
py_repositories()
196+
197+
load("@rules_python//python:pip.bzl", "pip_parse")
198+
199+
pip_parse(
200+
name = "third_party",
201+
requirements_lock = "//:requirements.txt",
202+
python_interpreter_target = "@python_3_10_host//:python",
203+
)
204+
205+
load("@third_party//:requirements.bzl", "install_deps")
206+
207+
install_deps()
208+
209+
# File: MODULE.bazel
210+
bazel_dep(name = "rules_python", version = "0.40.0")
211+
212+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
213+
214+
python.toolchain(is_default = True, python_version = "3.10")
215+
216+
use_repo(python, "python_3_10", "python_3_10_host")
217+
```
218+
219+
Note, the user has to import the `*_host` repository to use the python interpreter in the
220+
{bzl:obj}`pip_parse` and {bzl:obj}`whl_library` repository rules and once that is done
221+
users should be able to ensure the setting of the default toolchain even during the
222+
transition period when some of the code is still defined in `WORKSPACE`.
223+
187224
## Workspace configuration
188225

189226
To import rules_python in your project, you first need to add it to your
@@ -229,13 +266,11 @@ python_register_toolchains(
229266
python_version = "3.11",
230267
)
231268

232-
load("@python_3_11//:defs.bzl", "interpreter")
233-
234269
load("@rules_python//python:pip.bzl", "pip_parse")
235270

236271
pip_parse(
237272
...
238-
python_interpreter_target = interpreter,
273+
python_interpreter_target = "@python_3_11_host//:python",
239274
...
240275
)
241276
```

examples/pip_parse_vendored/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ python_register_toolchains(
2020
name = "python39",
2121
python_version = "3.9",
2222
)
23-
load("@python39//:defs.bzl", "interpreter")
2423

2524
# Load dependencies vendored by some other ruleset.
2625
load("@some_rules//:py_deps.bzl 5326 ", "install_deps")
2726

2827
install_deps(
29-
python_interpreter_target = interpreter,
28+
python_interpreter_target = "@python39_host//:python",
3029
)
3130
```

0 commit comments

Comments
 (0)
0