Skip to content

Commit b79b8fb

Browse files
committed
merge #1764
2 parents c34ad9d + 1abac11 commit b79b8fb

File tree

4 files changed

+87
-21
lines changed

4 files changed

+87
-21
lines changed

python/private/bzlmod/pip.bzl

-4
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,9 @@ def _pip_impl(module_ctx):
478478

479479
for hub_name, whl_map in hub_whl_map.items():
480480
build_file_contents = BUILD_FILE_CONTENTS
481-
has_default = False
482481
config_settings = []
483482
for value in config_settings_map.get(hub_name, {}).values():
484483
if value == "//conditions:default":
485-
has_default = True
486484
continue
487485
config_settings.append(value)
488486

@@ -504,8 +502,6 @@ def _pip_impl(module_ctx):
504502
key: json.encode(value)
505503
for key, value in whl_map.items()
506504
},
507-
default_version = _major_minor_version(DEFAULT_PYTHON_VERSION),
508-
render_default_version = not has_default,
509505
)
510506

511507
def _pip_parse_ext_attrs():

python/private/bzlmod/pip_repository.bzl

-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def _pip_repository_impl(rctx):
3131
key: [whl_alias(**v) for v in json.decode(values)]
3232
for key, values in rctx.attr.whl_map.items()
3333
},
34-
default_version = rctx.attr.default_version if rctx.attr.render_default_version else None,
3534
)
3635
for path, contents in aliases.items():
3736
rctx.file(path, contents)
@@ -65,16 +64,6 @@ pip_repository_attrs = {
6564
default = BUILD_FILE_CONTENTS,
6665
doc = """The BUILD.bazel contents for the root of the repo.""",
6766
),
68-
"default_version": attr.string(
69-
mandatory = True,
70-
doc = """\
71-
This is the default python version in the format of X.Y. This should match
72-
what is setup by the 'python' extension using the 'is_default = True'
73-
setting.""",
74-
),
75-
"render_default_version": attr.bool(
76-
default = True,
77-
),
7867
"repo_name": attr.string(
7968
mandatory = True,
8069
doc = "The apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical name.",

python/private/render_pkg_aliases.bzl

+18-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ If the value is missing, then the "default" Python version is being used,
3838
which has a "null" version value and will not match version constraints.
3939
"""
4040

41+
_CONDITIONS_DEFAULT = "//conditions:default"
42+
4143
def _render_whl_library_alias(
4244
*,
4345
name,
@@ -60,13 +62,13 @@ def _render_whl_library_alias(
6062
for alias in sorted(aliases, key = lambda x: x.config_setting):
6163
actual = "@{repo}//:{name}".format(repo = alias.repo, name = name)
6264
selects[alias.config_setting] = actual
63-
if alias.version == default_version:
65+
if alias.version == default_version or alias.config_setting == _CONDITIONS_DEFAULT:
6466
default = actual
6567
no_match_error = None
6668

6769
if default:
68-
selects["//conditions:default"] = default
69-
fail("foo")
70+
# Attempt setting it but continue if already exists
71+
selects.setdefault(_CONDITIONS_DEFAULT, default)
7072

7173
return render.alias(
7274
name = name,
@@ -84,12 +86,22 @@ def _render_common_aliases(*, name, aliases, default_version = None):
8486
versions = None
8587
has_default = False
8688
if aliases:
87-
versions = sorted({a.version: True for a in aliases if a.version}.keys())
88-
has_default = len([a.config_setting for a in aliases if a.config_setting == "//conditions:default"]) == 1
89+
versions = sorted([a.version for a in aliases if a.version])
90+
has_default = len([a.config_setting for a in aliases if a.config_setting == _CONDITIONS_DEFAULT]) == 1
91+
default_version_aliases = [a for a in aliases if a.version == default_version or a.config_setting == _CONDITIONS_DEFAULT]
92+
if not has_default and len(default_version_aliases) > 1:
93+
fail(
94+
(
95+
"BUG: expected to have a single alias for the default version, but got multiple: '{}'. " +
96+
"Add the 'whl_alias(config_setting = {}, ...)' setting explicitly."
97+
).format(default_version_aliases, repr(_CONDITIONS_DEFAULT)),
98+
)
99+
89100
if has_default:
101+
# Zero this out as it is not useful anymore.
90102
default_version = None
91103

92-
if not versions or default_version in versions:
104+
if not versions or default_version in versions or has_default:
93105
pass
94106
else:
95107
error_msg = NO_MATCH_ERROR_MESSAGE_TEMPLATE.format(

tests/pip_hub_repository/render_pkg_aliases/render_pkg_aliases_test.bzl

+69
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,75 @@ alias(
160160

161161
_tests.append(_test_bzlmod_aliases)
162162

163+
def _test_bzlmod_aliases_with_default(env):
164+
actual = render_pkg_aliases(
165+
aliases = {
166+
"bar-baz": [
167+
whl_alias(version = "3.2", repo = "pypi_32_bar_baz", config_setting = "//:my_config_setting_32"),
168+
whl_alias(version = "3.1", repo = "pypi_31_bar_baz", config_setting = "//:my_config_setting_31"),
169+
whl_alias(version = "3.2", repo = "pypi_32_bar_baz", config_setting = "//conditions:default"),
170+
],
171+
},
172+
)
173+
174+
want_key = "bar_baz/BUILD.bazel"
175+
want_content = """\
176+
package(default_visibility = ["//visibility:public"])
177+
178+
alias(
179+
name = "bar_baz",
180+
actual = ":pkg",
181+
)
182+
183+
alias(
184+
name = "pkg",
185+
actual = select(
186+
{
187+
"//:my_config_setting_31": "@pypi_31_bar_baz//:pkg",
188+
"//:my_config_setting_32": "@pypi_32_bar_baz//:pkg",
189+
"//conditions:default": "@pypi_32_bar_baz//:pkg",
190+
},
191+
),
192+
)
193+
194+
alias(
195+
name = "whl",
196+
actual = select(
197+
{
198+
"//:my_config_setting_31": "@pypi_31_bar_baz//:whl",
199+
"//:my_config_setting_32": "@pypi_32_bar_baz//:whl",
200+
"//conditions:default": "@pypi_32_bar_baz//:whl",
201+
},
202+
),
203+
)
204+
205+
alias(
206+
name = "data",
207+
actual = select(
208+
{
209+
"//:my_config_setting_31": "@pypi_31_bar_baz//:data",
210+
"//:my_config_setting_32": "@pypi_32_bar_baz//:data",
211+
"//conditions:default": "@pypi_32_bar_baz//:data",
212+
},
213+
),
214+
)
215+
216+
alias(
217+
name = "dist_info",
218+
actual = select(
219+
{
220+
"//:my_config_setting_31": "@pypi_31_bar_baz//:dist_info",
221+
"//:my_config_setting_32": "@pypi_32_bar_baz//:dist_info",
222+
"//conditions:default": "@pypi_32_bar_baz//:dist_info",
223+
},
224+
),
225+
)"""
226+
227+
env.expect.that_collection(actual.keys()).contains_exactly([want_key])
228+
env.expect.that_str(actual[want_key]).equals(want_content)
229+
230+
_tests.append(_test_bzlmod_aliases_with_default)
231+
163232
def _test_bzlmod_aliases_with_no_default_version(env):
164233
actual = render_pkg_aliases(
165234
default_version = None,

0 commit comments

Comments
 (0)