Skip to content
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

Open
vonschultz opened this issue Jan 28, 2025 · 0 comments · May be fixed by #2588
Open

Support environment/file-based default Python version in python.toolchain #2587

vonschultz opened this issue Jan 28, 2025 · 0 comments · May be fixed by #2588

Comments

@vonschultz
Copy link
Contributor

🚀 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 the is_default attribute. You can have several Python versions defined, and for one of them you need to set is_default = True. Since a MODULE.bazel file cannot read an environment variable or read a file, you need to set the default in MODULE.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 the WORKSPACE.bazel file, and set the default_version in a call to python_register_multi_toolchains accordingly.

This is important for interoperability in projects where not everything is run through Bazel; pyenv for example uses the PYENV_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 the python_version attribute of python.toolchain matches the contents of the file, treat it as the default (as if is_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 like write_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.

vonschultz added a commit to vonschultz/rules_python that referenced this issue Jan 28, 2025
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 bazelbuild#2587.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant