-
🐞 bug report <> help wantedAffected RuleThe issue is caused by the rule: Is this a regression?I'm not sure, but probably related to this: DescriptionI'm adding In this repo we have some rules, aspects, and python actions that should be executed using our own toolchain. In this regard, we're also switching from our own custom toolchain to the ones offered by Using the traditional WORKSPACE method we have no issues but when bzlmod is activated other Bazel modules cannot properly load and use our tools. As additional info, our WORKSPACE.bzlmod is currently empty, we are offering only python3.9, and every python dependency comes from From what I read from the above Issue and PR this is somewhat intentional, but then I'm not sure how to proceed. Ideally I would want the end user (other module) to be able to inherit our python toolchain / pip requirements with 🔬 Minimal ReproductionNot a really a minimal reproduction but a code snippet that might help: # our_module/MODULE.bazel
...
bazel_dep(name = "rules_python", repo_name = "our_module_rules_python", version = "0.33.2")
python = use_extension("@our_module_rules_python//python/extensions:python.bzl", "python")
python.toolchain(
python_version = "3.9",
configure_coverage_tool = True,
is_default = True, # From what I understood this is only valid for this root
)
use_repo(python, "python_versions", "python_3_9") # Tested this ...
register_toolchains("@bazel_tools//tools/python:autodetecting_toolchain") # Tested this ...
pip = use_extension("@our_module_rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "our_module_pip",
python_version = "3.9",
requirements_lock = "//third_party/pip:requirements_lock.txt",
)
use_repo(pip, "our_module_pip")
... # other_module/MODULE.bazel
...
bazel_dep(name = "other_module", version = "2.0.0")
local_path_override( # The real final user would actually load us through `http_archive` or `bazel_dep`
module_name = "other_module",
path = "../other_module",
)
... Then a 🔥 Exception or Error
🌍 Your EnvironmentOperating System: Output of Rules_python version: Anything else relevant? If I register a python3.9 toolchain in the other_module it works. If I register, for example, python3.8, it raises the same error, as expected. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
The problem is that you are not using transitions to ensure that your module is using the right python toolchain. Could you add snippets of BUILD.bazel files where the targets in the 'our_module' are consumed by the 'other_module'. 'our_module' should use the python version aware py_binary in order to ensure that the default toolchain version set by 'other_module' does not influnce the build configuration for the targets in 'our_module'. So right now it is working as intended. |
Beta Was this translation helpful? Give feedback.
-
Hey, thanks for looking into it. Actually the use case is that
load("@our_module//:defs.bzl", "aspect_config")
aspect_config(
name = "custom_aspect_config",
additional_flags = [],
config_files = "@our_module//configs:config_1", # We usually offer a set of predefined configs as well
default_feature = "awesome",
visibility = ["//visibility:public"],
)
build:my_config --@our_module//:my_aspect_config=//:custom_aspect_config
build:my_config --output_groups=my_aspect_output
build:my_config --aspects=@our_module//:defs.bzl%my_aspect And then we call it with, for example,
Usual python target used by our aspects: load("@our_module_pip//:requirements.bzl", "requirement")
load("@our_module_rules_python//python:defs.bzl", "py_binary", "py_library")
py_binary(
name = "runner", # This is then a default (private) attr of our aspects and is invoked by a `ctx.actions.run`
srcs = ["runner.py"],
visibility = ["//visibility:public"],
deps = [":utils"],
)
py_library(
name = "utils",
srcs = ["utils.py"],
visibility = ["//:__subpackages__"],
) |
Beta Was this translation helpful? Give feedback.
-
The problem is the second snippet, you should be declaring the Or you should call |
Beta Was this translation helpful? Give feedback.
-
That did the trick, thanks @aignas. If I may ask one more question, with workspace approach I was using a custom python toolchain name, but with bzlmod the name is directly derived from the python version. Is it possible to change it someway? If not, does it makes sense? I found that it's hard coded here: |
Beta Was this translation helpful? Give feedback.
-
Why would you like to change that? With bzlmod the registration of the hermetic toolchains is happening in rules_python instead of the root module, so there is no need to have a custom name.
…On 4 July 2024 23:23:43 GMT+09:00, Lucas Munaretto ***@***.***> wrote:
That did the trick, thanks @aignas.
If I may ask one more question, with workspace approach I was using a custom python toolchain name, but with bzlmod the name is directly derived from the python version. Is it possible to change it someway? If not, does it makes sense?
I found that it's hard coded here:
https://github.com/bazelbuild/rules_python/blob/084b877c98b580839ceab2b071b02fc6768f3de6/python/private/python.bzl#L96C13-L96C27
--
Reply to this email directly or view it on GitHub:
#2029 (comment)
You are receiving this because you were mentioned.
Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Yes, for bzlmod itself there is no need for such thing. The idea would be to keep consistency between bzlmod and workspace. With workspace we usually have specialised names to avoid possible name conflicts inside big monorepos. Right now I basically modified the workspace toolchain name to match the bzlmod one. |
Beta Was this translation helpful? Give feedback.
-
The consistency between You may have success in using:
But I am not sure if we can help you here anything beyond here. |
Beta Was this translation helpful? Give feedback.
The problem is the second snippet, you should be declaring the
py_binary
in a similar way how the version specific py binaries are defined: https://github.com/bazelbuild/rules_python/blob/main/examples/bzlmod/tests/BUILD.bazel#L2Or you should call
pip.parse
multiple times for each python version that your users may use to ensure that you have working dependency tree on any Python version.