Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/pypi/rules-pycross-comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ all of them. You can define more platforms with {obj}`pip.default`.
| `glibc_version`, `musl_version`, `macos_version` | {attr}`pip.default.whl_platform_tags` of a `platform`, e.g. `pip.default(platform = "linux_x86_64", whl_platform_tags = ["manylinux_*_x86_64"])`; the wheels are picked when the extension runs |
| `py_linux_libc` | the same flag, {flag}`--py_linux_libc` |
| `pep508_*` flags | {attr}`pip.default.env` when the extension runs, the {flag}`--pip_env_marker_config` flag in the build |
| `unavailable_package_mode` | an error when the target is analysed; planned: an opt-in to make the target incompatible |
| `unavailable_package_mode` | {attr}`pip.parse.unavailable_package_mode` (`error` or `incompatible`) |

## Labels

Expand Down Expand Up @@ -221,7 +221,7 @@ module. Do not depend on the same distribution from both in one binary.
| Simple API for files without URLs, `netrc` | {attr}`pip.default.index_url`, {attr}`pip.default.netrc`, {attr}`pip.default.auth_patterns` | have |
| one file with two hashes | files are looked up by the lock file hash | have |
| glibc, musl and macOS version flags | {obj}`pip.default` platforms | partial |
| `unavailable_package_mode` | an error | partial |
| `unavailable_package_mode` | {attr}`pip.parse.unavailable_package_mode`; no `fail_at_execution` | partial |
| automatic dependency cycles | {attr}`pip.parse.experimental_requirement_cycles` | planned |
| uv `conflicts` | an error; use a separate hub for each conflicting group | gap |
| per-repo transitions | none; the {flag}`--venv` flag selects a hub for the unified `@pypi` hub | gap |
Expand Down
3 changes: 3 additions & 0 deletions news/pip-parse-unavailable-package-mode.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(pypi) The {attr}`pip.parse.unavailable_package_mode` attribute can make the
targets of the packages that are not available for the current configuration
incompatible instead of failing the analysis (bzlmod only).
23 changes: 23 additions & 0 deletions python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ You cannot use both the additive_build_content and additive_build_content_file a
# Where hub, whl, and pip are the repo names
hub_whl_map = {}
hub_group_map = {}
unavailable_package_modes = {}
exposed_packages = {}
extra_aliases = {}
whl_libraries = {}
Expand All @@ -345,6 +346,7 @@ You cannot use both the additive_build_content and additive_build_content_file a
exposed_packages[hub.name] = out.exposed_packages
extra_aliases[hub.name] = out.extra_aliases
hub_group_map[hub.name] = out.group_map
unavailable_package_modes[hub.name] = out.unavailable_package_mode
hub_whl_map[hub.name] = out.whl_map
hubs[hub.name] = struct(
module_name = hub.module_name,
Expand Down Expand Up @@ -385,6 +387,7 @@ You cannot use both the additive_build_content and additive_build_content_file a
facts = simpleapi_cache.get_facts(),
hub_group_map = hub_group_map,
hub_whl_map = hub_whl_map,
unavailable_package_modes = unavailable_package_modes,
unified_hub_requirements_bzl = config.unified_hub_requirements_bzl,
whl_libraries = whl_libraries,
whl_mods = whl_mods,
Expand Down Expand Up @@ -535,6 +538,7 @@ def _pip_impl(module_ctx):
groups = mods.hub_group_map.get(hub_name),
# The hubs configured with `pip.configure` hide the other packages.
hide_unexposed_packages = hub_name in mods.configured_hubs,
unavailable_package_mode = mods.unavailable_package_modes.get(hub_name, "error"),
)

_create_unified_hub_repo(mods)
Expand Down Expand Up @@ -1003,6 +1007,25 @@ any custom platforms configured with {obj}`pip.default`.
Added support for `"*"` to select all of the configured platforms.
:::
""",
),
"unavailable_package_mode": attr.string(
default = "error",
values = ["error", "incompatible"],
doc = """\
What to do when a package of the hub is used in a configuration that it is
not available for, e.g. a package that only has wheels for Linux is used on
macOS:
* `error` (the default): fail the analysis with an error that lists the
supported configurations.
* `incompatible`: make the targets of the package incompatible, so that the
targets that depend on it are skipped, e.g. by `bazel build //...`, or fail
with an error that lists the incompatible dependencies.

All of the `pip.parse` calls of a hub need to use the same value. A package
that is missing from a hub that the unified `@pypi` hub selects still fails.

:::{versionadded} VERSION_NEXT_FEATURE
:::""",
),
"uv_lock": attr.label(
doc = """
Expand Down
12 changes: 12 additions & 0 deletions python/private/pypi/hub_builder.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def hub_builder(
# The set of package names to expose. Dict acting as set
# dict[str package, None]
_exposed_packages = {}, # modified by _add_exposed_packages
_unavailable_package_mode = {}, # modified by _pip_parse
# Map of the per-package aliases.
# The nested dict is a dict acting as a set.
# dict[str whl_name, dict[str alias_name, bool]]
Expand Down Expand Up @@ -124,6 +125,7 @@ def _build(self):
extra_aliases = {},
exposed_packages = [],
whl_libraries = {},
unavailable_package_mode = "error",
)
if self._logger.failed():
return ret
Expand Down Expand Up @@ -153,10 +155,20 @@ def _build(self):
# Mapping of whl_library repo names and their kwargs.
# dict[str repo_name, dict[str, object] kwargs]
whl_libraries = self._whl_libraries,
# What to do when a package is not available.
unavailable_package_mode = self._unavailable_package_mode.get("mode", "error"),
)

def _pip_parse(self, module_ctx, pip_attr, default_envsubst = [], python_version = None):
python_version = python_version or pip_attr.python_version
mode = getattr(pip_attr, "unavailable_package_mode", "error")
if self._unavailable_package_mode.setdefault("mode", mode) != mode:
self._logger.fail(lambda: "All of the pip.parse calls of the hub '{}' need the same 'unavailable_package_mode', got '{}' and '{}'".format(
self.name,
self._unavailable_package_mode["mode"],
mode,
))
return
if python_version in self._platforms:
fail((
"Duplicate pip python version '{version}' for hub " +
Expand Down
7 changes: 7 additions & 0 deletions python/private/pypi/hub_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def _impl(rctx):
extra_hub_aliases = rctx.attr.extra_hub_aliases,
requirement_cycles = rctx.attr.groups,
platform_config_settings = rctx.attr.platform_config_settings,
# Only rendered when it is not the default
unavailable_package_mode = "" if rctx.attr.unavailable_package_mode == "error" else rctx.attr.unavailable_package_mode,
)
for path, contents in aliases.items():
rctx.file(path, contents)
Expand Down Expand Up @@ -104,6 +106,11 @@ unless `hide_unexposed_packages` is set.
mandatory = True,
doc = "The apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical name.",
),
"unavailable_package_mode": attr.string(
default = "error",
values = ["error", "incompatible"],
doc = "What to do when a package is not available for the current configuration.",
),
"whl_map": attr.string_dict(
mandatory = True,
doc = """\
Expand Down
17 changes: 16 additions & 1 deletion python/private/pypi/pkg_aliases.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ However, the command above will hide the `bazel config <config id>` message.
_LABEL_NONE = labels.NONE
_LABEL_CURRENT_CONFIG = Label("//python/config_settings:current_config")
_LABEL_CURRENT_CONFIG_NO_MATCH = Label("//python/config_settings:is_not_matching_current_config")
_LABEL_INCOMPATIBLE = Label("@platforms//:incompatible")
_INCOMPATIBLE = "_no_matching_repository"
_DEFAULT = "//conditions:default"

Expand All @@ -90,6 +91,7 @@ def pkg_aliases(
extras = [],
include = [],
group_deps = [],
unavailable_package_mode = "error",
**kwargs):
"""Create aliases for an actual package.

Expand All @@ -116,6 +118,9 @@ def pkg_aliases(
requested extras come from the {obj}`whl_config_setting` keys.
include: {type}`list[str]` The subset of packages to include.
Comes from `//:config.bzl#packages`
unavailable_package_mode: {type}`str` What to do when the package is
not available for the current configuration: `error` fails the
analysis and `incompatible` makes the targets incompatible.
**kwargs: extra kwargs to pass to {bzl:obj}`get_config_settings`.
"""
if not group_name:
Expand Down Expand Up @@ -180,7 +185,17 @@ def pkg_aliases(

extras_by_setting = _extras_by_setting(actual, extras)
actual = multiplatform_whl_aliases(aliases = actual, **kwargs)
if type(actual) == type({}) and "//conditions:default" not in actual:
if type(actual) == type({}) and "//conditions:default" not in actual and unavailable_package_mode == "incompatible":
# The targets that depend on the package are skipped.
_native.filegroup(
name = _INCOMPATIBLE,
target_compatible_with = [_LABEL_INCOMPATIBLE],
visibility = ["//visibility:private"],
tags = ["manual"],
)
actual["//conditions:default"] = _INCOMPATIBLE
elif type(actual) == type({}) and "//conditions:default" not in actual:
# The analysis fails with an error that lists the config settings.
_native.alias(
name = _INCOMPATIBLE,
actual = select(
Expand Down
30 changes: 30 additions & 0 deletions tests/pypi/hub_builder/hub_builder_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,36 @@ def _test_pylock(env):

_tests.append(_test_pylock)

def _test_unavailable_package_mode_mismatch(env):
logs = {}
builder = hub_builder(
env,
log_printer = lambda key, message: logs.setdefault(key.strip(), []).append(message),
)
for python_version, mode in [("3.15", "incompatible"), ("3.14", "error")]:
builder.pip_parse(
_mock_mctx(
os_name = "osx",
arch_name = "aarch64",
mock_files = {
"uv.lock": """{"package":[]}""",
},
),
_parse(
hub_name = "pypi",
python_version = python_version,
requirements_lock = None,
uv_lock = "uv.lock",
unavailable_package_mode = mode,
),
)

env.expect.that_collection(logs.get("rules_python:unit-test FAIL:", [])).contains_exactly([
"All of the pip.parse calls of the hub 'pypi' need the same 'unavailable_package_mode', got 'incompatible' and 'error'",
])

_tests.append(_test_unavailable_package_mode_mismatch)

def _test_uv_lock_conflicting_versions(env):
logs = {}
builder = hub_builder(
Expand Down
37 changes: 37 additions & 0 deletions tests/pypi/pkg_aliases/pkg_aliases_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,43 @@ configuration settings:

_tests.append(_test_config_setting_aliases)

def _test_config_setting_aliases_incompatible(env):
got = {}
filegroups = {}

pkg_aliases(
name = "bar_baz",
actual = {
"//:my_config_setting": "bar_baz_repo",
},
unavailable_package_mode = "incompatible",
native = struct(
alias = lambda *, name, actual, visibility = None, tags = None: got.update({name: actual}),
filegroup = lambda *, name, **kwargs: filegroups.update({name: kwargs}),
),
select = lambda value, no_match_error = None: value,
)

env.expect.that_dict(got).contains_at_least({
"[]": {
"//:my_config_setting": "@bar_baz_repo//:pkg",
"//conditions:default": "_no_matching_repository",
},
"whl": {
"//:my_config_setting": "@bar_baz_repo//:whl",
"//conditions:default": "_no_matching_repository",
},
})
env.expect.that_dict(filegroups).contains_exactly({
"_no_matching_repository": {
"tags": ["manual"],
"target_compatible_with": [Label("@platforms//:incompatible")],
"visibility": ["//visibility:private"],
},
})

_tests.append(_test_config_setting_aliases_incompatible)

def _test_config_setting_aliases_many(env):
# Use this function as it is used in pip_repository
got = {}
Expand Down
Loading