Skip to content

Commit 3c40804

Browse files
committed
toolchain ordering: something wrong with it
1 parent 64a2f4b commit 3c40804

File tree

6 files changed

+80
-28
lines changed

6 files changed

+80
-28
lines changed

MODULE.bazel

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,19 @@ dev_python.toolchain(python_version = "3.13")
133133
# See //tests/toolchains:custom_platform_toolchain_test
134134
dev_python.single_version_platform_override(
135135
platform = "linux-x86-install-only-stripped",
136-
python_version = "3.13.3",
137-
sha256 = "01d08b9bc8a96698b9d64c2fc26da4ecc4fa9e708ce0a34fb88f11ab7e552cbd",
136+
##python_version = "3.13.3",
137+
python_version = "3.13.1",
138+
##sha256 = "01d08b9bc8a96698b9d64c2fc26da4ecc4fa9e708ce0a34fb88f11ab7e552cbd",
139+
sha256 = "56817aa976e4886bec1677699c136cb01c1cdfe0495104c0d8ef546541864bbb",
138140
target_compatible_with = [
139141
"@platforms//os:linux",
140142
"@platforms//cpu:x86_64",
141143
],
142144
target_settings = [
143145
"@@//tests/support:is_custom_runtime_linux-x86-install-only-stripped",
144146
],
145-
urls = ["https://github.com/astral-sh/python-build-standalone/releases/download/20250409/cpython-3.13.3+20250409-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"],
147+
urls = ["https://github.com/astral-sh/python-build-standalone/releases/download/20250115/cpython-3.13.1+20250115-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"],
148+
##urls = ["https://github.com/astral-sh/python-build-standalone/releases/download/20250409/cpython-3.13.3+20250409-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"],
146149
)
147150

148151
dev_pip = use_extension(

python/private/python.bzl

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ def _python_impl(module_ctx):
305305
# list[str] of the repo names for host compatible repos
306306
all_host_compatible_repo_names = []
307307

308+
# todo: The 3.13.1-custom-runtime ends up _last_ in the toolchain
309+
# ordering, so it never gets picked.
310+
# Need to move it first
311+
308312
# Create the underlying python_repository repos that contain the
309313
# python runtimes and their toolchain implementation definitions.
310314
for i, toolchain_info in enumerate(py.toolchains):
@@ -331,10 +335,13 @@ def _python_impl(module_ctx):
331335
**kwargs
332336
)
333337
if not register_result.impl_repos:
338+
fail("hit")
334339
continue
335340

336341
host_platforms = {}
337342
for repo_name, (platform_name, platform_info) in register_result.impl_repos.items():
343+
if "3.13.1" in full_python_version:
344+
print("registered:", repo_name)
338345
toolchain_impls.append(struct(
339346
# str: The base name to use for the toolchain() target
340347
name = repo_name,
@@ -669,7 +676,14 @@ def _process_single_version_platform_overrides(*, tag, _fail = fail, default):
669676
if tag.sha256:
670677
available_versions[tag.python_version].setdefault("sha256", {})[tag.platform] = tag.sha256
671678
if tag.strip_prefix:
672-
available_versions[tag.python_version].setdefault("strip_prefix", {})[tag.platform] = tag.strip_prefix
679+
v = available_versions
680+
v1 = available_versions[tag.python_version]
681+
v1.setdefault("strip_prefix", {})
682+
v2 = v1["strip_prefix"]
683+
print(v2)
684+
v2[tag.platform] = tag.strip_prefix
685+
##available_versions[tag.python_version].setdefault("strip_prefix", {})[tag.platform] = tag.strip_prefix
686+
673687
if tag.urls:
674688
available_versions[tag.python_version].setdefault("url", {})[tag.platform] = tag.urls
675689

@@ -784,22 +798,29 @@ def _get_toolchain_config(*, modules, _fail = fail):
784798
"""
785799

786800
# Items that can be overridden
787-
available_versions = {
788-
version: {
789-
# Use a dicts straight away so that we could do URL overrides for a
790-
# single version.
791-
"sha256": dict(item["sha256"]),
792-
"strip_prefix": {
793-
platform: item["strip_prefix"]
794-
for platform in item["sha256"]
795-
} if type(item["strip_prefix"]) == type("") else item["strip_prefix"],
796-
"url": {
797-
platform: [item["url"]]
798-
for platform in item["sha256"]
799-
} if type(item["url"]) == type("") else item["url"],
800-
}
801-
for version, item in TOOL_VERSIONS.items()
802-
}
801+
available_versions = {}
802+
for py_version, item in TOOL_VERSIONS.items():
803+
available_versions[py_version] = {}
804+
available_versions[py_version]["sha256"] = dict(item["sha256"])
805+
platforms = item["sha256"].keys()
806+
807+
strip_prefix = item["strip_prefix"]
808+
if type(strip_prefix) == type(""):
809+
available_versions[py_version]["strip_prefix"] = {
810+
platform: strip_prefix
811+
for platform in platforms
812+
}
813+
else:
814+
available_versions[py_version]["strip_prefix"] = dict(strip_prefix)
815+
url = item["url"]
816+
if type(url) == type(""):
817+
available_versions[py_version]["url"] = {
818+
platform: url
819+
for platform in platforms
820+
}
821+
else:
822+
available_versions[py_version]["url"] = dict(url)
823+
803824
default = {
804825
"base_url": DEFAULT_RELEASE_BASE_URL,
805826
"platforms": dict(PLATFORMS), # Copy so it's mutable.

python/private/pythons_hub.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ PYTHON_VERSIONS = {python_versions}
9696
def _hub_repo_impl(rctx):
9797
# Create the various toolchain definitions and
9898
# write them to the BUILD file.
99+
content = _hub_build_file_content(rctx)
100+
print(content, "===")
99101
rctx.file(
100102
"BUILD.bazel",
101103
_hub_build_file_content(rctx),

python/private/repo_utils.bzl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ def _is_repo_debug_enabled(mrctx):
3131
"""
3232
return _getenv(mrctx, REPO_DEBUG_ENV_VAR) == "1"
3333

34-
def _logger(mrctx, name = None):
34+
def _logger(mrctx = None, name = None, verbosity_level = None):
3535
"""Creates a logger instance for printing messages.
3636
3737
Args:
3838
mrctx: repository_ctx or module_ctx object. If the attribute
3939
`_rule_name` is present, it will be included in log messages.
4040
name: name for the logger. Optional for repository_ctx usage.
41+
verbosity_level: {type}`int | None` verbosity level. If not set,
42+
taken from `mrctx`
4143
4244
Returns:
4345
A struct with attributes logging: trace, debug, info, warn, fail.
@@ -46,13 +48,14 @@ def _logger(mrctx, name = None):
4648
the logger injected into the function work as expected by terminating
4749
on the given line.
4850
"""
49-
if _is_repo_debug_enabled(mrctx):
50-
verbosity_level = "DEBUG"
51-
else:
52-
verbosity_level = "WARN"
51+
if verbosity_level == None:
52+
if _is_repo_debug_enabled(mrctx):
53+
verbosity_level = "DEBUG"
54+
else:
55+
verbosity_level = "WARN"
5356

54-
env_var_verbosity = _getenv(mrctx, REPO_VERBOSITY_ENV_VAR)
55-
verbosity_level = env_var_verbosity or verbosity_level
57+
env_var_verbosity = _getenv(mrctx, REPO_VERBOSITY_ENV_VAR)
58+
verbosity_level = env_var_verbosity or verbosity_level
5659

5760
verbosity = {
5861
"DEBUG": 2,

tests/python/python_tests.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
load("@pythons_hub//:versions.bzl", "MINOR_MAPPING")
1818
load("@rules_testing//lib:test_suite.bzl", "test_suite")
1919
load("//python/private:python.bzl", "parse_modules") # buildifier: disable=bzl-visibility
20+
load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl-visibility
2021

2122
_tests = []
2223

@@ -131,13 +132,18 @@ def _single_version_platform_override(
131132
python_version = python_version,
132133
patch_strip = patch_strip,
133134
patches = patches,
135+
target_compatible_with = [],
136+
target_settings = [],
137+
os_name = "",
138+
arch = "",
134139
)
135140

136141
def _test_default(env):
137142
py = parse_modules(
138143
module_ctx = _mock_mctx(
139144
_mod(name = "rules_python", toolchain = [_toolchain("3.11")]),
140145
),
146+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
141147
)
142148

143149
# The value there should be consistent in bzlmod with the automatically
@@ -168,6 +174,7 @@ def _test_default_some_module(env):
168174
module_ctx = _mock_mctx(
169175
_mod(name = "rules_python", toolchain = [_toolchain("3.11")], is_root = False),
170176
),
177+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
171178
)
172179

173180
env.expect.that_str(py.default_python_version).equals("3.11")
@@ -186,6 +193,7 @@ def _test_default_with_patch_version(env):
186193
module_ctx = _mock_mctx(
187194
_mod(name = "rules_python", toolchain = [_toolchain("3.11.2")]),
188195
),
196+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
189197
)
190198

191199
env.expect.that_str(py.default_python_version).equals("3.11.2")
@@ -207,6 +215,7 @@ def _test_default_non_rules_python(env):
207215
# does not make any calls to the extension.
208216
_mod(name = "rules_python", toolchain = [_toolchain("3.11")], is_root = False),
209217
),
218+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
210219
)
211220

212221
env.expect.that_str(py.default_python_version).equals("3.11")
@@ -228,6 +237,7 @@ def _test_default_non_rules_python_ignore_root_user_error(env):
228237
),
229238
_mod(name = "rules_python", toolchain = [_toolchain("3.11")]),
230239
),
240+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
231241
)
232242

233243
env.expect.that_bool(py.config.default["ignore_root_user_error"]).equals(False)
@@ -257,6 +267,7 @@ def _test_default_non_rules_python_ignore_root_user_error_non_root_module(env):
257267
_mod(name = "some_module", toolchain = [_toolchain("3.12", ignore_root_user_error = False)]),
258268
_mod(name = "rules_python", toolchain = [_toolchain("3.11")]),
259269
),
270+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
260271
)
261272

262273
env.expect.that_str(py.default_python_version).equals("3.13")
@@ -302,6 +313,7 @@ def _test_toolchain_ordering(env):
302313
),
303314
_mod(name = "rules_python", toolchain = [_toolchain("3.11")]),
304315
),
316+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
305317
)
306318
got_versions = [
307319
t.python_version
@@ -347,6 +359,7 @@ def _test_default_from_defaults(env):
347359
is_root = True,
348360
),
349361
),
362+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
350363
)
351364

352365
env.expect.that_str(py.default_python_version).equals("3.11")
@@ -374,6 +387,7 @@ def _test_default_from_defaults_env(env):
374387
),
375388
environ = {"PYENV_VERSION": "3.12"},
376389
),
390+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
377391
)
378392

379393
env.expect.that_str(py.default_python_version).equals("3.12")
@@ -401,6 +415,7 @@ def _test_default_from_defaults_file(env):
401415
),
402416
mocked_files = {"@@//:.python-version": "3.12\n"},
403417
),
418+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
404419
)
405420

406421
env.expect.that_str(py.default_python_version).equals("3.12")
@@ -427,6 +442,7 @@ def _test_first_occurance_of_the_toolchain_wins(env):
427442
"RULES_PYTHON_BZLMOD_DEBUG": "1",
428443
},
429444
),
445+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
430446
)
431447

432448
env.expect.that_str(py.default_python_version).equals("3.12")
@@ -472,6 +488,7 @@ def _test_auth_overrides(env):
472488
),
473489
_mod(name = "rules_python", toolchain = [_toolchain("3.11")]),
474490
),
491+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
475492
)
476493

477494
env.expect.that_dict(py.config.default).contains_at_least({
@@ -541,6 +558,7 @@ def _test_add_new_version(env):
541558
],
542559
),
543560
),
561+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
544562
)
545563

546564
env.expect.that_str(py.default_python_version).equals("3.13")
@@ -609,6 +627,7 @@ def _test_register_all_versions(env):
609627
],
610628
),
611629
),
630+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
612631
)
613632

614633
env.expect.that_str(py.default_python_version).equals("3.13")
@@ -685,6 +704,7 @@ def _test_add_patches(env):
685704
],
686705
),
687706
),
707+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
688708
)
689709

690710
env.expect.that_str(py.default_python_version).equals("3.13")
@@ -731,6 +751,7 @@ def _test_fail_two_overrides(env):
731751
),
732752
),
733753
_fail = errors.append,
754+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
734755
)
735756
env.expect.that_collection(errors).contains_exactly([
736757
"Only a single 'python.override' can be present",
@@ -758,6 +779,7 @@ def _test_single_version_override_errors(env):
758779
),
759780
),
760781
_fail = errors.append,
782+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
761783
)
762784
env.expect.that_collection(errors).contains_exactly([test.want_error])
763785

@@ -795,6 +817,7 @@ def _test_single_version_platform_override_errors(env):
795817
),
796818
),
797819
_fail = lambda *a: errors.append(" ".join(a)),
820+
logger = repo_utils.logger(verbosity_level = 0, name = "python"),
798821
)
799822
env.expect.that_collection(errors).contains_exactly([test.want_error])
800823

tests/toolchains/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ py_reconfig_test(
2323
name = "custom_platform_toolchain_test",
2424
srcs = ["custom_platform_toolchain_test.py"],
2525
custom_runtime = "linux-x86-install-only-stripped",
26-
python_version = "3.13.3",
26+
python_version = "3.13.1",
2727
target_compatible_with = [
2828
"@platforms//os:linux",
2929
"@platforms//cpu:x86_64",

0 commit comments

Comments
 (0)