diff --git a/python/private/pypi/pep508_deps.bzl b/python/private/pypi/pep508_deps.bzl index 8aa74194fa..90002b1dc1 100644 --- a/python/private/pypi/pep508_deps.bzl +++ b/python/private/pypi/pep508_deps.bzl @@ -28,7 +28,8 @@ def deps( extras = [], excludes = [], include = [], - keep_extras = False): + keep_extras = False, + envs = []): """Parse the RequiresDist from wheel METADATA Args: @@ -42,12 +43,19 @@ def deps( extras: {type}`list[str]` the requested extras to generate targets for. keep_extras: {type}`bool` keep the extras of the dependencies, if `True`, the dependencies with extras are returned as `foo[bar]`. + envs: {type}`list[dict[str, str]]` the marker environments of the + target platforms. If specified, the dependencies that are left + out because they are not in `include` are returned as `missing` + if they are needed on any of the target platforms. Returns: A struct with attributes: * deps: {type}`list[str]` dependencies to include unconditionally. * deps_select: {type}`dict[str, list[str]]` dependencies to include on particular subset of target platforms. + * missing: {type}`dict[str, list[str]]` the names of the + dependencies that are not in `include`, but are needed on at + least one of the `envs`, and the requirements that need them. The dependencies are normalized names, e.g. `foo`, or normalized names with a single extra, e.g. `foo[bar]`, if the dependency is requested @@ -58,6 +66,7 @@ def deps( return struct( deps = [], deps_select = {}, + missing = {}, ) reqs = sorted( @@ -72,6 +81,7 @@ def deps( excludes = {group_member(x): None for x in excludes} reqs_by_dep = {} + missing = {} for req in reqs: # drop self edges @@ -79,6 +89,8 @@ def deps( continue if include and req.name_ not in include: + if envs and _is_needed(req, extras = want_extras, envs = envs): + missing.setdefault(req.name, []).append(_format(req)) continue for dep in ["{}[{}]".format(req.name_, e) for e in req.extras if keep_extras] or [req.name_]: @@ -100,8 +112,37 @@ def deps( d: markers for d, markers in sorted(deps_select.items()) }, + missing = missing, ) +def _format(req): + return "{}{}{}".format( + req.name, + "[{}]".format(",".join(req.extras)) if req.extras else "", + "; {}".format(req.marker) if req.marker else "", + ) + +def _is_needed(req, *, extras, envs): + """Tell if the requirement is needed on any of the target platforms.""" + if not req.marker: + return True + + for extra in extras: + # Most of the requirements are for the other extras, so do not go + # through the target platforms for them. + env = create_env() | {"extra": extra} + m = evaluate(req.marker, env = env, strict = False) + if m == False: + continue + elif m == True: + return True + + for target_env in envs: + if evaluate(req.marker, env = env | target_env, strict = False) != False: + return True + + return False + def _add(deps, deps_select, dep, markers = None): if not markers: deps[dep] = True diff --git a/tests/pypi/pep508/deps_tests.bzl b/tests/pypi/pep508/deps_tests.bzl index be288fe308..adb2d02e49 100644 --- a/tests/pypi/pep508/deps_tests.bzl +++ b/tests/pypi/pep508/deps_tests.bzl @@ -376,6 +376,67 @@ def test_deps_with_extras_excludes_and_includes(env): _tests.append(test_deps_with_extras_excludes_and_includes) +def test_missing_deps(env): + """The deps that are not included are returned if the target platforms need them. + + Args: + env: the test env. + """ + requires_dist = [ + "bar", + "not-locked", + "tomli>=2; python_version < \"3.11\"", + "tomli-w[all]; python_version < \"3.11\" and extra == \"write\"", + "pywin32; sys_platform == \"win32\"", + "colorama; sys_platform == \"win32\" and extra == \"color\"", + "foo[write]; extra == \"all\"", + ] + linux = lambda v: {"python_version": v, "sys_platform": "linux"} + + got = deps("foo", requires_dist = requires_dist, include = ["bar"]) + env.expect.that_dict(got.missing).contains_exactly({}) + + got = deps("foo", requires_dist = requires_dist, envs = [linux("3.10")]) + env.expect.that_collection(got.deps).contains_at_least(["bar", "not_locked"]) + env.expect.that_dict(got.missing).contains_exactly({}) + + got = deps("foo", requires_dist = requires_dist, include = ["bar"], envs = [linux("3.12")]) + env.expect.that_dict(got.missing).contains_exactly({"not-locked": ["not-locked"]}) + + got = deps("foo", requires_dist = requires_dist, include = ["bar"], envs = [linux("3.12"), linux("3.10")]) + env.expect.that_dict(got.missing).contains_exactly({ + "not-locked": ["not-locked"], + "tomli": ["tomli; python_version < \"3.11\""], + }) + + # The extras that are included via the self references are checked. + got = deps( + "foo", + requires_dist = requires_dist, + include = ["bar"], + extras = ["all", "color"], + envs = [linux("3.12"), linux("3.10")], + ) + env.expect.that_dict(got.missing).contains_exactly({ + "not-locked": ["not-locked"], + "tomli": ["tomli; python_version < \"3.11\""], + "tomli-w": ["tomli-w[all]; python_version < \"3.11\" and extra == \"write\""], + }) + + got = deps( + "foo", + requires_dist = requires_dist, + include = ["bar", "not_locked"], + extras = ["color"], + envs = [{"python_version": "3.12", "sys_platform": "win32"}], + ) + env.expect.that_dict(got.missing).contains_exactly({ + "colorama": ["colorama; sys_platform == \"win32\" and extra == \"color\""], + "pywin32": ["pywin32; sys_platform == \"win32\""], + }) + +_tests.append(test_missing_deps) + def deps_test_suite(name): # buildifier: disable=function-docstring test_suite( name = name,