-
-
Notifications
You must be signed in to change notification settings - Fork 570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support environment/file-based default Python version in python.toolchain #2587
Comments
This change adds a new `default_version_file` attribute to `python.toolchain`. If set, the toolchain compares the file's contents to its `python_version`, and if they match, treats that toolchain as default (ignoring `is_default`). This allows Bazel to synchronize the default Python version with external tools (e.g., pyenv) that use a `.python-version` file or environment variables. Fixes bazel-contrib#2587.
I have seen this issue but I did not have enough time to come up with good pros/cons list for the long-term maintaining of such a thing. I would love to do this through https://github.com/buildbuddy-io/bazel_env.bzl since this has proven to have the right API for user settings like this. This does require EDIT: This preference would lean towards writing the |
I don't really follow. What is bazel_env.bzl helping us with? Writing the This is one of the reasons we're still using Now, if |
Ah sorry, my breain went and jumped into conclusion that you want to have a python version file for pyenv based on python toolchain configuration in bzlmod. Out of interest, would an include file work in your case? module.bazel files can load/include other files that have fragments of the module.bazel itself. Another way is to use .bazelrc to set the default value of the python version - it can be done by adding --@rules_python//python/config_settings:python_version=3.9.5. Could you add a little more context why those ways would not be sufficient? |
Part of the problem is that Given that Of course, I could just make a wrapper for Bazel with whatever logic I want to determine the default version and then go in and physically change the MODULE.bazel file, but that doesn't really feel like a clean solution. |
Thanks for the explanation. I agree about the default being settable by .python-version file in the root module if it exists, if not, we fallback to the current logic. The root module could also provide an alternative label to the file that has the version. That said, having this logic or an env var doing the switching is probably also fine. I'll look at your PR once again sometime next week then. |
🚀 feature request
Relevant Rules
python.toolchain
where
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
Description
It should be possible to set the default Python version using a file or an environment variable.
In
python.toolchain
you need to set theis_default
attribute. You can have several Python versions defined, and for one of them you need to setis_default = True
. Since aMODULE.bazel
file cannot read an environment variable or read a file, you need to set the default inMODULE.bazel
. Before Bzlmod, you could simply load a file created by a repo rule and have access to the value of an environment variable or the contents of a.python-version
file in theWORKSPACE.bazel
file, and set thedefault_version
in a call topython_register_multi_toolchains
accordingly.This is important for interoperability in projects where not everything is run through Bazel;
pyenv
for example uses thePYENV_VERSION
environment variable, falling back on the.python-version
file if the environment variable is not set.Describe the solution you'd like
A new attribute for
python.toolchain
called e.g.default_python_version_file
, which can be the label of a file generated by a repository rule, or a file in the current repo. If thepython_version
attribute ofpython.toolchain
matches the contents of the file, treat it as the default (as ifis_default = True
).Describe alternatives you've considered
One could imagine having both an attribute for setting the default Python version with a file and another attribute for setting the default Python version with an environment variable, which would lower the threshold for any user wanting to react to an environment variable, at the cost of a more complex implementation in rules_python. Since the same functionality can be achieved with a repository rule, I'm leaning towards telling users to write a repository rule.
One could also imagine having the default Python version be defined in
MODULE.bazel
, as the source of truth, and have something likewrite_source_file
update a.python-version
file in the repo for compatibility with other tools. But done this way, there's no way to support environment variables as a way of setting the default Python version.The text was updated successfully, but these errors were encountered: