-
-
Notifications
You must be signed in to change notification settings - Fork 681
feat(toolchains): add add_target_settings to python.override #3731
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
rickeylev
merged 4 commits into
bazel-contrib:main
from
novas0x2a:feat/toolchain-target-settings
Apr 25, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
80eac09
feat(toolchains): add toolchain_target_settings to python.override
novas0x2a b212042
Remove Bazel copyright headers from newly added files
rickeylev 7613ec7
Update changelog and fix VERSION_NEXT marker
rickeylev 8706527
Rename toolchain_target_settings to add_target_settings in PR scoped …
rickeylev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| common --lockfile_mode=off | ||
| test --test_output=errors | ||
| build --enable_runfiles |
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,70 @@ | ||
| # Copyright 2025 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("@bazel_skylib//rules:common_settings.bzl", "string_flag") | ||
| load("@rules_python//python:py_test.bzl", "py_test") | ||
|
|
||
| # A flag to select which "family" of toolchains to use. | ||
| string_flag( | ||
| name = "family", | ||
| build_setting_default = "prebuilt", | ||
| values = [ | ||
| "prebuilt", | ||
| "custom", | ||
| ], | ||
| ) | ||
|
|
||
| # Matches when the "prebuilt" family is selected. | ||
| # This is referenced in MODULE.bazel's python.override(toolchain_target_settings=...). | ||
| config_setting( | ||
| name = "is_prebuilt", | ||
| flag_values = { | ||
| ":family": "prebuilt", | ||
| }, | ||
| ) | ||
|
|
||
| # Matches when the "custom" family is selected. | ||
| # No toolchains use this setting, so selecting it should produce an error. | ||
| config_setting( | ||
| name = "is_custom", | ||
| flag_values = { | ||
| ":family": "custom", | ||
| }, | ||
| ) | ||
|
|
||
| # This target selects the "prebuilt" family via config_settings transition. | ||
| # Since python.override(toolchain_target_settings = ["@@//:is_prebuilt"]) gates | ||
| # the default toolchains, this should succeed: the flag matches, the config_setting | ||
| # is satisfied, and the default 3.13 toolchain resolves. | ||
| py_test( | ||
| name = "prebuilt_test", | ||
| srcs = ["main.py"], | ||
| config_settings = { | ||
| "//:family": "prebuilt", | ||
| }, | ||
| main = "main.py", | ||
| ) | ||
|
|
||
| # This target selects the "custom" family via config_settings transition. | ||
| # No toolchains have target_settings = [":is_custom"], so toolchain resolution | ||
| # should fail -- the default toolchains are gated behind ":is_prebuilt" and | ||
| # won't match. | ||
| py_test( | ||
| name = "custom_no_toolchain_test", | ||
| srcs = ["main.py"], | ||
| config_settings = { | ||
| "//:family": "custom", | ||
| }, | ||
| main = "main.py", | ||
| ) |
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,37 @@ | ||
| # Copyright 2025 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. | ||
| module(name = "module_under_test") | ||
|
|
||
| bazel_dep(name = "rules_python", version = "0.0.0") | ||
| bazel_dep(name = "bazel_skylib", version = "1.7.1") | ||
|
|
||
| local_path_override( | ||
| module_name = "rules_python", | ||
| path = "../../..", | ||
| ) | ||
|
|
||
| python = use_extension("@rules_python//python/extensions:python.bzl", "python") | ||
| python.toolchain(python_version = "3.13") | ||
|
|
||
| # Gate ALL default-registered toolchains behind the "prebuilt" config setting. | ||
| # This prevents them from being a silent fallback when a different toolchain | ||
| # family is requested. | ||
| python.override( | ||
| toolchain_target_settings = ["@@//:is_prebuilt"], | ||
|
rickeylev marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| # Register //:family as a transition setting so py_binary/py_test | ||
| # config_settings can set it. | ||
| config = use_extension("@rules_python//python/extensions:config.bzl", "config") | ||
| config.add_transition_setting(setting = "//:family") | ||
Empty file.
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 @@ | ||
| # Intentionally blank; bzlmod is used. |
Empty file.
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,2 @@ | ||
| import sys | ||
| print(f"Python {sys.version}") |
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,55 @@ | ||
| # Copyright 2025 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. | ||
|
|
||
| """Integration test for python.override(toolchain_target_settings=...). | ||
|
|
||
| Verifies that when all default toolchains are gated behind a config_setting, | ||
| requesting a different (unregistered) toolchain family produces a toolchain | ||
| resolution error instead of silently falling back to the default toolchains. | ||
| """ | ||
|
|
||
| import unittest | ||
|
|
||
| from tests.integration import runner | ||
|
|
||
|
|
||
| class ToolchainTargetSettingsTest(runner.TestCase): | ||
| def test_prebuilt_family_resolves(self): | ||
| """Building with the 'prebuilt' family should succeed. | ||
|
|
||
| The default toolchains have target_settings = [":is_prebuilt"], | ||
| and the transition sets //:family=prebuilt, so the config_setting | ||
| matches and toolchain resolution finds the default 3.13 toolchain. | ||
| """ | ||
| self.run_bazel("test", "//:prebuilt_test") | ||
|
|
||
| def test_custom_family_without_toolchain_fails(self): | ||
| """Building with the 'custom' family should fail. | ||
|
|
||
| No toolchains have target_settings = [":is_custom"], and the default | ||
| toolchains are gated behind ":is_prebuilt" (via toolchain_target_settings), | ||
| so toolchain resolution should fail with no matching toolchain. | ||
| """ | ||
| result = self.run_bazel( | ||
| "build", "//:custom_no_toolchain_test", check=False | ||
| ) | ||
| self.assertNotEqual(result.exit_code, 0, "Expected build to fail") | ||
| self.assert_result_matches( | ||
| result, | ||
| r"No matching toolchains found for types", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.