Skip to content

Commit f2b8ad7

Browse files
committed
add a test for non-root modules
1 parent 52db4c9 commit f2b8ad7

File tree

4 files changed

+75
-24
lines changed

4 files changed

+75
-24
lines changed

python/uv/private/toolchains_hub.bzl

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ load(":toolchain_types.bzl", "UV_TOOLCHAIN_TYPE")
1919
def toolchains_hub(
2020
*,
2121
name = None,
22-
names,
22+
toolchains,
2323
implementations,
2424
target_compatible_with,
2525
target_settings):
@@ -29,40 +29,38 @@ def toolchains_hub(
2929
TODO @aignas 2025-03-09: see if this can be reused in the python toolchains.
3030
3131
Args:
32-
name: Unused.
33-
names: The names for toolchain targets. The later occurring items take
34-
precedence over the previous items if they match the target platform.
32+
name: The prefix to all of the targets, which goes after a numeric prefix.
33+
toolchains: The toolchain names for the targets defined by this macro.
34+
The earlier occurring items take precedence over the later items if
35+
they match the target platform and target settings.
3536
implementations: The name to label mapping.
3637
target_compatible_with: The name to target_compatible_with list mapping.
3738
target_settings: The name to target_settings list mapping.
3839
"""
39-
if len(names) != len(implementations):
40+
if len(toolchains) != len(implementations):
4041
fail("Each name must have an implementation")
4142

42-
# We are setting the order of the toolchains so that the later coming
43-
# toolchains override the previous definitions using the toolchain
44-
# resolution properties:
43+
# We are defining the toolchains so that the order of toolchain matching is
44+
# the same as the order of the toolchains, because:
4545
# * the toolchains are matched by target settings and target_compatible_with
4646
# * the first toolchain satisfying the above wins
4747
#
4848
# this means we need to register the toolchains prefixed with a number of
4949
# format 00xy, where x and y are some digits and the leading zeros to
5050
# ensure lexicographical sorting.
51-
prefix_len = len(str(len(names)))
51+
#
52+
# Add 1 so that there is always a leading zero
53+
prefix_len = len(str(len(toolchains))) + 1
5254
prefix = "0" * (prefix_len - 1)
5355

54-
# reverse the names list so that the later items override earlier toolchain
55-
# registrations.
56-
names = [n for _, n in sorted(enumerate(names), key = lambda x: -x[0])]
57-
58-
for i, name in enumerate(names):
56+
for i, toolchain in enumerate(toolchains):
5957
# prefix with a prefix and then truncate the string.
6058
number_prefix = "{}{}".format(prefix, i)[-prefix_len:]
6159

6260
native.toolchain(
63-
name = "{}_{}".format(number_prefix, name),
64-
target_compatible_with = target_compatible_with.get(name, []),
65-
target_settings = target_settings.get(name, []),
66-
toolchain = implementations[name],
61+
name = "{}_{}_{}".format(number_prefix, name, toolchain),
62+
target_compatible_with = target_compatible_with.get(toolchain, []),
63+
target_settings = target_settings.get(toolchain, []),
64+
toolchain = implementations[toolchain],
6765
toolchain_type = UV_TOOLCHAIN_TYPE,
6866
)

python/uv/private/uv_toolchains_repo.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def _toolchains_repo_impl(repository_ctx):
2929
contents = _TEMPLATE.format(
3030
render.call(
3131
"toolchains_hub",
32-
names = render.list(repository_ctx.attr.toolchain_names),
32+
name = repr("uv_toolchain"),
33+
toolchains = render.list(repository_ctx.attr.toolchain_names),
3334
implementations = render.dict(
3435
repository_ctx.attr.toolchain_implementations,
3536
),

tests/uv/uv/uv_tests.bzl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,57 @@ def _test_complex_configuring(env):
436436

437437
_tests.append(_test_complex_configuring)
438438

439+
def _test_non_rules_python_non_root_is_ignored(env):
440+
calls = []
441+
uv = _process_modules(
442+
env,
443+
module_ctx = _mock_mctx(
444+
_mod(
445+
default = [
446+
_default(
447+
base_url = "https://example.org",
448+
manifest_filename = "manifest.json",
449+
version = "1.0.0",
450+
platform = "osx",
451+
compatible_with = ["@platforms//os:os"],
452+
),
453+
],
454+
configure = [
455+
_configure(), # use defaults
456+
],
457+
),
458+
_mod(
459+
name = "something",
460+
configure = [
461+
_configure(version = "6.6.6"), # use defaults whatever they are
462+
],
463+
),
464+
),
465+
uv_repository = lambda **kwargs: calls.append(kwargs),
466+
)
467+
468+
uv.names().contains_exactly([
469+
"1_0_0_osx",
470+
])
471+
uv.implementations().contains_exactly({
472+
"1_0_0_osx": "@uv_1_0_0_osx//:uv_toolchain",
473+
})
474+
uv.compatible_with().contains_exactly({
475+
"1_0_0_osx": ["@platforms//os:os"],
476+
})
477+
uv.target_settings().contains_exactly({})
478+
env.expect.that_collection(calls).contains_exactly([
479+
{
480+
"name": "uv_1_0_0_osx",
481+
"platform": "osx",
482+
"sha256": "deadb00f",
483+
"urls": ["https://example.org/1.0.0/osx"],
484+
"version": "1.0.0",
485+
},
486+
])
487+
488+
_tests.append(_test_non_rules_python_non_root_is_ignored)
489+
439490
_analysis_tests = []
440491

441492
def _test_toolchain_precedence(name):

tests/uv/uv_toolchains/BUILD.bazel

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
load("//python/uv/private:toolchains_hub.bzl", "toolchains_hub") # buildifier: disable=bzl-visibility
22

33
toolchains_hub(
4+
name = "uv_unit_test",
45
implementations = {
56
"bar": "//tests/uv/uv:fake_bar",
67
"foo": "//tests/uv/uv:fake_foof",
78
},
8-
# We expect foo to take precedence over bar
9-
names = [
10-
"bar",
11-
"foo",
12-
],
139
target_compatible_with = {
1410
"bar": [
1511
"@platforms//os:linux",
@@ -21,4 +17,9 @@ toolchains_hub(
2117
],
2218
},
2319
target_settings = {},
20+
# We expect foo to take precedence over bar
21+
toolchains = [
22+
"foo",
23+
"bar",
24+
],
2425
)

0 commit comments

Comments
 (0)