-
-
Notifications
You must be signed in to change notification settings - Fork 604
fix(toolchains): correctly order the toolchains #2735
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
15 commits
Select commit
Hold shift + click to select a range
0b9e65d
fix(python): correctly order the toolchains
aignas 3c7f542
doc: changelog
aignas 6c02eb5
remove workaround from the uv lock
aignas 3f14780
wip
aignas c5a43f4
wip
aignas ec006ee
add unit tests and refine
aignas 3253ae6
remove the lock fail
aignas 1728c0d
fixup
aignas 48bc1ab
buildifier
aignas a112daf
cardcode the version for WORKSPACE integration tests
aignas 9619624
disable the extra tests in non-bzlmod
aignas 09ae63c
wip
aignas a5079f5
wip
aignas d640490
add a test and revert the old toolchain integration tests
aignas 20fd356
fix tests on bazel <8
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
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
load(":transitions_tests.bzl", "transitions_test_suite") | ||
|
||
transitions_test_suite( | ||
name = "transitions_tests", | ||
) |
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 |
---|---|---|
@@ -0,0 +1,182 @@ | ||
# Copyright 2022 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"" | ||
|
||
load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION", "MINOR_MAPPING") | ||
load("@rules_testing//lib:analysis_test.bzl", "analysis_test") | ||
load("@rules_testing//lib:test_suite.bzl", "test_suite") | ||
load("@rules_testing//lib:util.bzl", rt_util = "util") | ||
load("//python:versions.bzl", "TOOL_VERSIONS") | ||
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility | ||
load("//python/private:full_version.bzl", "full_version") # buildifier: disable=bzl-visibility | ||
load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility | ||
load("//tests/support:support.bzl", "PYTHON_VERSION") | ||
|
||
_analysis_tests = [] | ||
|
||
def _transition_impl(input_settings, attr): | ||
"""Transition based on python_version flag. | ||
|
||
This is a simple transition impl that a user of rules_python may implement | ||
for their own rule. | ||
""" | ||
settings = { | ||
PYTHON_VERSION: input_settings[PYTHON_VERSION], | ||
} | ||
if attr.python_version: | ||
settings[PYTHON_VERSION] = attr.python_version | ||
return settings | ||
|
||
_python_version_transition = transition( | ||
implementation = _transition_impl, | ||
inputs = [PYTHON_VERSION], | ||
outputs = [PYTHON_VERSION], | ||
) | ||
|
||
TestInfo = provider( | ||
doc = "A simple test provider to forward the values for the assertion.", | ||
fields = {"got": "", "want": ""}, | ||
) | ||
|
||
def _impl(ctx): | ||
if ctx.attr.skip: | ||
return [TestInfo(got = "", want = "")] | ||
|
||
exec_tools = ctx.toolchains[EXEC_TOOLS_TOOLCHAIN_TYPE].exec_tools | ||
got_version = exec_tools.exec_interpreter[platform_common.ToolchainInfo].py3_runtime.interpreter_version_info | ||
|
||
return [ | ||
TestInfo( | ||
got = "{}.{}.{}".format( | ||
got_version.major, | ||
got_version.minor, | ||
got_version.micro, | ||
), | ||
want = ctx.attr.want_version, | ||
), | ||
] | ||
|
||
_simple_transition = rule( | ||
implementation = _impl, | ||
attrs = { | ||
"python_version": attr.string( | ||
doc = "The input python version which we transition on.", | ||
), | ||
"skip": attr.bool( | ||
doc = "Whether to skip the test", | ||
), | ||
"want_version": attr.string( | ||
doc = "The python version that we actually expect to receive.", | ||
), | ||
"_allowlist_function_transition": attr.label( | ||
default = "@bazel_tools//tools/allowlists/function_transition_allowlist", | ||
), | ||
}, | ||
toolchains = [ | ||
config_common.toolchain_type( | ||
EXEC_TOOLS_TOOLCHAIN_TYPE, | ||
mandatory = False, | ||
), | ||
], | ||
cfg = _python_version_transition, | ||
) | ||
|
||
def _test_transitions(*, name, tests, skip = False): | ||
"""A reusable rule so that we can split the tests.""" | ||
targets = {} | ||
for test_name, (input_version, want_version) in tests.items(): | ||
target_name = "{}_{}".format(name, test_name) | ||
targets["python_" + test_name] = target_name | ||
rt_util.helper_target( | ||
_simple_transition, | ||
name = target_name, | ||
python_version = input_version, | ||
want_version = want_version, | ||
skip = skip, | ||
) | ||
|
||
analysis_test( | ||
name = name, | ||
impl = _test_transition_impl, | ||
targets = targets, | ||
) | ||
|
||
def _test_transition_impl(env, targets): | ||
# Check that the forwarded version from the PyRuntimeInfo is correct | ||
for target in dir(targets): | ||
if not target.startswith("python"): | ||
# Skip other attributes that might be not the ones we set (e.g. to_json, to_proto). | ||
continue | ||
|
||
test_info = env.expect.that_target(getattr(targets, target)).provider( | ||
TestInfo, | ||
factory = lambda v, meta: v, | ||
) | ||
env.expect.that_str(test_info.got).equals(test_info.want) | ||
|
||
def _test_full_version(name): | ||
"""Check that python_version transitions work. | ||
|
||
Expectation is to get the same full version that we input. | ||
""" | ||
_test_transitions( | ||
name = name, | ||
tests = { | ||
v.replace(".", "_"): (v, v) | ||
for v in TOOL_VERSIONS | ||
}, | ||
) | ||
|
||
_analysis_tests.append(_test_full_version) | ||
|
||
def _test_minor_versions(name): | ||
"""Ensure that MINOR_MAPPING versions are correctly selected.""" | ||
_test_transitions( | ||
name = name, | ||
skip = not BZLMOD_ENABLED, | ||
tests = { | ||
minor.replace(".", "_"): (minor, full) | ||
for minor, full in MINOR_MAPPING.items() | ||
}, | ||
) | ||
|
||
_analysis_tests.append(_test_minor_versions) | ||
|
||
def _test_default(name): | ||
"""Check the default version. | ||
|
||
Lastly, if we don't provide any version to the transition, we should | ||
get the default version | ||
""" | ||
default_version = full_version( | ||
version = DEFAULT_PYTHON_VERSION, | ||
minor_mapping = MINOR_MAPPING, | ||
) if DEFAULT_PYTHON_VERSION else "" | ||
|
||
_test_transitions( | ||
name = name, | ||
skip = not BZLMOD_ENABLED, | ||
tests = { | ||
"default": (None, default_version), | ||
}, | ||
) | ||
|
||
_analysis_tests.append(_test_default) | ||
|
||
def transitions_test_suite(name): | ||
test_suite( | ||
name = name, | ||
tests = _analysis_tests, | ||
) |
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.