-
-
Notifications
You must be signed in to change notification settings - Fork 597
feat(python.toolchain): support file-based default Python version #2588
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6813055
feat(python.toolchain): support file-based default Python version
vonschultz e0b7a3d
Update python/private/python.bzl
aignas f2cc08a
Update python/private/python.bzl
aignas 31cb09c
Update python/private/python.bzl
aignas 86d17aa
doc: add changelog
aignas 0228b9d
Merge branch 'main' into default_version_file
aignas 2b1286d
adjust changelog
aignas f4631dc
Merge branch 'bazel-contrib:main' into default_version_file
vonschultz 4acb4d7
feat: Implement a python.defaults tag class
vonschultz 678a608
Use Label when resolving @@//:.python-version
vonschultz b5a337b
Drop support for Bazel 7.0 in python_version_env
vonschultz 1a94ad4
Explicitly watch the python version file we read
vonschultz 5bdfbe2
Stop attempting to read @@//:.python-version
vonschultz c601c06
Merge branch 'main' into default_version_file
aignas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,47 @@ def parse_modules(*, module_ctx, _fail = fail): | |
|
||
config = _get_toolchain_config(modules = module_ctx.modules, _fail = _fail) | ||
|
||
default_python_version = None | ||
for mod in module_ctx.modules: | ||
defaults_attr_structs = _create_defaults_attr_structs(mod = mod) | ||
default_python_version_env = None | ||
default_python_version_file = None | ||
|
||
# Only the root module and rules_python are allowed to specify the default | ||
# toolchain for a couple reasons: | ||
# * It prevents submodules from specifying different defaults and only | ||
# one of them winning. | ||
# * rules_python needs to set a soft default in case the root module doesn't, | ||
# e.g. if the root module doesn't use Python itself. | ||
# * The root module is allowed to override the rules_python default. | ||
if mod.is_root or (mod.name == "rules_python" and not default_python_version): | ||
for defaults_attr in defaults_attr_structs: | ||
default_python_version = _one_or_the_same( | ||
default_python_version, | ||
defaults_attr.python_version, | ||
onerror = _fail_multiple_defaults_python_version, | ||
) | ||
default_python_version_env = _one_or_the_same( | ||
default_python_version_env, | ||
defaults_attr.python_version_env, | ||
onerror = _fail_multiple_defaults_python_version_env, | ||
) | ||
default_python_version_file = _one_or_the_same( | ||
default_python_version_file, | ||
defaults_attr.python_version_file, | ||
onerror = _fail_multiple_defaults_python_version_file, | ||
) | ||
if default_python_version_file: | ||
default_python_version = _one_or_the_same( | ||
default_python_version, | ||
module_ctx.read(default_python_version_file, watch = "yes").strip(), | ||
) | ||
if default_python_version_env: | ||
default_python_version = module_ctx.getenv( | ||
default_python_version_env, | ||
default_python_version, | ||
) | ||
|
||
seen_versions = {} | ||
for mod in module_ctx.modules: | ||
module_toolchain_versions = [] | ||
|
@@ -104,7 +145,13 @@ def parse_modules(*, module_ctx, _fail = fail): | |
# * rules_python needs to set a soft default in case the root module doesn't, | ||
# e.g. if the root module doesn't use Python itself. | ||
# * The root module is allowed to override the rules_python default. | ||
is_default = toolchain_attr.is_default | ||
if default_python_version: | ||
is_default = default_python_version == toolchain_version | ||
if toolchain_attr.is_default and not is_default: | ||
fail("The 'is_default' attribute doesn't work if you set " + | ||
"the default Python version with the `defaults` tag.") | ||
else: | ||
is_default = toolchain_attr.is_default | ||
|
||
# Also only the root module should be able to decide ignore_root_user_error. | ||
# Modules being depended upon don't know the final environment, so they aren't | ||
|
@@ -115,7 +162,7 @@ def parse_modules(*, module_ctx, _fail = fail): | |
fail("Toolchains in the root module must have consistent 'ignore_root_user_error' attributes") | ||
|
||
ignore_root_user_error = toolchain_attr.ignore_root_user_error | ||
elif mod.name == "rules_python" and not default_toolchain: | ||
elif mod.name == "rules_python" and not default_toolchain and not default_python_version: | ||
# We don't do the len() check because we want the default that rules_python | ||
# sets to be clearly visible. | ||
is_default = toolchain_attr.is_default | ||
|
@@ -282,6 +329,19 @@ def _python_impl(module_ctx): | |
else: | ||
return None | ||
|
||
def _one_or_the_same(first, second, *, onerror = None): | ||
if not first: | ||
return second | ||
if not second or second == first: | ||
return first | ||
if onerror: | ||
return onerror(first, second) | ||
else: | ||
fail("Unique value needed, got both '{}' and '{}', which are different".format( | ||
first, | ||
second, | ||
)) | ||
|
||
def _fail_duplicate_module_toolchain_version(version, module): | ||
fail(("Duplicate module toolchain version: module '{module}' attempted " + | ||
"to use version '{version}' multiple times in itself").format( | ||
|
@@ -305,6 +365,30 @@ def _warn_duplicate_global_toolchain_version(version, first, second_toolchain_na | |
version = version, | ||
)) | ||
|
||
def _fail_multiple_defaults_python_version(first, second): | ||
fail(("Multiple python_version entries in defaults: " + | ||
"First default was python_version '{first}'. " + | ||
"Second was python_version '{second}'").format( | ||
first = first, | ||
second = second, | ||
)) | ||
|
||
def _fail_multiple_defaults_python_version_file(first, second): | ||
fail(("Multiple python_version_file entries in defaults: " + | ||
"First default was python_version_file '{first}'. " + | ||
"Second was python_version_file '{second}'").format( | ||
first = first, | ||
second = second, | ||
)) | ||
|
||
def _fail_multiple_defaults_python_version_env(first, second): | ||
fail(("Multiple python_version_env entries in defaults: " + | ||
"First default was python_version_env '{first}'. " + | ||
"Second was python_version_env '{second}'").format( | ||
first = first, | ||
second = second, | ||
)) | ||
|
||
def _fail_multiple_default_toolchains(first, second): | ||
fail(("Multiple default toolchains: only one toolchain " + | ||
"can have is_default=True. First default " + | ||
|
@@ -526,6 +610,21 @@ def _get_toolchain_config(*, modules, _fail = fail): | |
register_all_versions = register_all_versions, | ||
) | ||
|
||
def _create_defaults_attr_structs(*, mod): | ||
arg_structs = [] | ||
|
||
for tag in mod.tags.defaults: | ||
arg_structs.append(_create_defaults_attr_struct(tag = tag)) | ||
|
||
return arg_structs | ||
|
||
def _create_defaults_attr_struct(*, tag): | ||
return struct( | ||
python_version = getattr(tag, "python_version", None), | ||
python_version_env = getattr(tag, "python_version_env", None), | ||
python_version_file = getattr(tag, "python_version_file", None), | ||
) | ||
|
||
def _create_toolchain_attr_structs(*, mod, config, seen_versions): | ||
arg_structs = [] | ||
|
||
|
@@ -570,6 +669,49 @@ def _get_bazel_version_specific_kwargs(): | |
|
||
return kwargs | ||
|
||
_defaults = tag_class( | ||
doc = """Tag class to specify the default Python version.""", | ||
attrs = { | ||
"python_version": attr.string( | ||
mandatory = False, | ||
doc = """\ | ||
String saying what the default Python version should be. If the string | ||
matches the {attr}`python_version` attribute of a toolchain, this | ||
toolchain is the default version. If this attribute is set, the | ||
{attr}`is_default` attribute of the toolchain is ignored. | ||
|
||
:::{versionadded} VERSION_NEXT_FEATURE | ||
::: | ||
""", | ||
), | ||
"python_version_env": attr.string( | ||
mandatory = False, | ||
doc = """\ | ||
Environment variable saying what the default Python version should be. | ||
If the string matches the {attr}`python_version` attribute of a | ||
toolchain, this toolchain is the default version. If this attribute is | ||
set, the {attr}`is_default` attribute of the toolchain is ignored. | ||
|
||
:::{versionadded} VERSION_NEXT_FEATURE | ||
::: | ||
""", | ||
), | ||
"python_version_file": attr.label( | ||
mandatory = False, | ||
allow_single_file = True, | ||
doc = """\ | ||
File saying what the default Python version should be. If the contents | ||
of the file match the {attr}`python_version` attribute of a toolchain, | ||
this toolchain is the default version. If this attribute is set, the | ||
{attr}`is_default` attribute of the toolchain is ignored. | ||
aignas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:::{versionadded} VERSION_NEXT_FEATURE | ||
::: | ||
""", | ||
), | ||
}, | ||
) | ||
|
||
_toolchain = tag_class( | ||
doc = """Tag class used to register Python toolchains. | ||
Use this tag class to register one or more Python toolchains. This class | ||
|
@@ -653,7 +795,14 @@ error to run with root access instead. | |
), | ||
"is_default": attr.bool( | ||
mandatory = False, | ||
doc = "Whether the toolchain is the default version", | ||
doc = """\ | ||
Whether the toolchain is the default version. | ||
|
||
:::{versionchanged} VERSION_NEXT_FEATURE | ||
This setting is ignored if the default version is set using the `defaults` | ||
tag class. | ||
::: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we deprecate this in favour of the cc: @rickeylev what do you think? |
||
""", | ||
), | ||
"python_version": attr.string( | ||
mandatory = True, | ||
|
@@ -852,6 +1001,7 @@ python = module_extension( | |
""", | ||
implementation = _python_impl, | ||
tag_classes = { | ||
"defaults": _defaults, | ||
"override": _override, | ||
"single_version_override": _single_version_override, | ||
"single_version_platform_override": _single_version_platform_override, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.