feat(interpreter): add current_py_toolchain rule for Make variable support#897
feat(interpreter): add current_py_toolchain rule for Make variable support#897ctcjab wants to merge 2 commits intoaspect-build:mainfrom
Conversation
…pport (aspect-build#896) Provide $(PYTHON3) and $(PYTHON3_ROOTPATH) Make variables via a current_py_toolchain rule, enabling bazel_env and genrule integration without depending on rules_python. The rule is auto-instantiated in the python_interpreters hub repo at @python_interpreters//:current_py_toolchain, and also exported from @aspect_rules_py//py:defs.bzl for standalone use. Closes aspect-build#896 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
The starlark_doc_extract rule (auto-generated by bzl_library) needs a bzl_library target for the private .bzl file, and the public bzl_library targets need to declare the dep. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
The pre-commit / conclusion GitHub Actions failures are unrelated to this PR — the workflow's checkout step tries to fetch The Buildkite CI (which does handle fork PRs correctly) is the relevant check here — it passed on the previous push after the bzl_library fix. |
|
https://github.com/bazel-contrib/rules_python/blob/d0b9baadc43eff92b3b9df0a04343e7ca653c44a/python/current_py_toolchain.bzl#L47 Hm. This is how rules_python does it too, which I don't love. I thought there was a way to associate the make vars directly with the toolchain type rather than having to have a wrapper rule which generates the variable info. |
|
There was a fix in Bazel 8.3.0 (bazelbuild/bazel@0e876b1, referenced in bazelbuild/bazel#14009) that allows The wrapper rule pattern (as used by |
|
In light of the above, is it worth merging this, and if Bazel ever provides something better rules_py can take advantage of that then? @arrdem @gregmagolan et al. |
|
cc @xangcastle |
|
Friendly bump (since I opened this a month ago and am hoping to avoid acquiring a merge conflict) - thanks in advance for any attention you can give this🙏 |
| `bazel_env`, and other rules that support Make variable expansion. | ||
| """ | ||
|
|
||
| _TOOLCHAIN_TYPE = "@bazel_tools//tools/python:toolchain_type" |
There was a problem hiding this comment.
We already have this in py/private/toolchain/types.bzl, can we use that constant?
| python_path_file="$(dirname "$0")/python_path.txt" | ||
| if [[ ! -f "$python_path_file" ]]; then | ||
| # Try rlocation-style path (Bazel runfiles) | ||
| python_path_file="${RUNFILES_DIR:-$0.runfiles}/aspect_rules_py/e2e/cases/current-py-toolchain-896/python_path.txt" |
There was a problem hiding this comment.
Won't it be _main and not aspect_rules_py? Does that mean this path is never hit?
| python_path_file="$(dirname "$0")/python_path.txt" | ||
| if [[ ! -f "$python_path_file" ]]; then | ||
| # Try rlocation-style path (Bazel runfiles) | ||
| python_path_file="${RUNFILES_DIR:-$0.runfiles}/aspect_rules_py/e2e/cases/current-py-toolchain-896/python_path.txt" |
There was a problem hiding this comment.
Also the MODULE is in e2e so that shouldn't be in the path at all, and this path is definitely wrong...
| transitive.append(toolchain.py3_runtime.files) | ||
| vars["PYTHON3"] = toolchain.py3_runtime.interpreter.path | ||
| vars["PYTHON3_ROOTPATH"] = toolchain.py3_runtime.interpreter.short_path | ||
| elif toolchain.py3_runtime.interpreter_path: |
There was a problem hiding this comment.
What if neither are set? Should it fail()?
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 691ec6b122
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| elif toolchain.py3_runtime.interpreter_path: | ||
| # Local / system interpreter (absolute path string) | ||
| vars["PYTHON3"] = toolchain.py3_runtime.interpreter_path | ||
| vars["PYTHON3_ROOTPATH"] = toolchain.py3_runtime.interpreter_path |
There was a problem hiding this comment.
Keep non-empty files for local interpreter toolchains
When py3_runtime uses interpreter_path, this branch only sets Make variables and leaves direct/transitive empty, so DefaultInfo.files becomes empty. That breaks the advertised bazel_env integration for local/system interpreters: bazel_env derives toolchain repo/runfiles from the toolchain target’s files and fails when none are present, so @python_interpreters//:current_py_toolchain cannot be used in toolchains for that mode.
Useful? React with 👍 / 👎.

Summary
Closes #896.
Adds a
current_py_toolchainrule that resolves the active Python toolchain and exposes$(PYTHON3)and$(PYTHON3_ROOTPATH)Make variables viaTemplateVariableInfo. This enablesbazel_envandgenruleintegration without depending onrules_python.python_interpretershub repo at@python_interpreters//:current_py_toolchain@aspect_rules_py//py:defs.bzland@aspect_rules_py//py:current_py_toolchain.bzlfor standalone use$(PYTHON3)expansion viagenruleUsage
Test plan
//e2e/cases/current-py-toolchain-896:testpassesbuildifierandbuildifier-lintpass🤖 Generated with Claude Code