Skip to content

Commit 1ebc93f

Browse files
feat(pypi): enable pipstar by default (#3225)
Before this PR we were using our python helpers to parse the whl METADATA for a particular host (or experimentally - target) platform and the fixed dependency list would be written to the `BUILD.bazel` files materialized by the `whl_library` repository rule. This PR adds extra plumbing to leverage the Starlark implementation of the METADATA file parsing (a.k.a. pipstar) which moves the evaluation to analysis phase as we will get the target platform details via the `env_marker_config_setting` from the `toolchain` used for the dependencies. Since users are normally working with a subset of platforms that the METADATA would pull in, we are writing the list of packages from the requirements file to a `bzl` file so that `pipstar` knows which packages to consider when parsing the METADATA. This will ensure that `bazel query` continues to work. Since just passing the list of packages to the `whl_library` would cause refetches of all of the `whl_library` dependencies when we add or remove a package, we pass a path where to load the symbol called `packages` from, which means that only the analysis phase will be affected because the macros will be re-evaluated if one adds or removes a package. We still need to download the correct platform-specific wheels for the right platform in order to fully resolve the referenced tickets. With this PR we are deprecating the `experimental_target_platforms` attribute since it has no longer any effect. Fixes #2949 Work towards #260 Work towards #2241 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 79f6546 commit 1ebc93f

File tree

24 files changed

+267
-134
lines changed

24 files changed

+267
-134
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ END_UNRELEASED_TEMPLATE
7474
* (toolchains) `py_runtime` and `PyRuntimeInfo` reject Python 2 settings.
7575
Setting `py_runtime.python_version = "PY2"` or non-None
7676
`PyRuntimeInfo.py2_runtime` is an error.
77+
* (pypi) `pipstar` flag has been flipped to be enabled by default, to turn it
78+
off use `RULES_PYTHON_ENABLE_PIPSTAR=0` environment variable. If you do, please
79+
add a comment to
80+
[#2949](https://github.com/bazel-contrib/rules_python/issues/2949).
81+
With this release we are deprecating {obj}`pip.parse.experimental_target_platforms` and
82+
{obj}`pip_repository.experimental_target_platforms`. For users using `WORKSPACE` and
83+
vendoring the `requirements.bzl` file, please re-vendor so that downstream is unaffected
84+
when the APIs get removed.
7785

7886
{#v0-0-0-fixed}
7987
### Fixed

examples/pip_parse_vendored/requirements.bzl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
load("@rules_python//python:pip.bzl", "pip_utils")
7-
load("@rules_python//python/pip_install:pip_repository.bzl", "group_library", "whl_library")
7+
load("@rules_python//python/pip_install:pip_repository.bzl", "whl_config_repo", "whl_library")
88

99
all_requirements = [
1010
"@my_project_pip_deps_vendored_certifi//:pkg",
@@ -91,12 +91,17 @@ def install_deps(**whl_library_kwargs):
9191
for requirement in group_requirements
9292
}
9393

94-
group_repo = "my_project_pip_deps_vendored__groups"
95-
group_library(
96-
name = group_repo,
94+
config_repo = "my_project_pip_deps_vendored__config"
95+
whl_config_repo(
96+
name = "my_project_pip_deps_vendored__config",
9797
repo_prefix = "my_project_pip_deps_vendored_",
9898
groups = all_requirement_groups,
99+
whl_map = {
100+
p: ""
101+
for p in all_whl_requirements_by_package
102+
},
99103
)
104+
config_load = "@{}//:config.bzl".format(config_repo)
100105

101106
# Install wheels which may be participants in a group
102107
whl_config = dict(_config)
@@ -112,5 +117,6 @@ def install_deps(**whl_library_kwargs):
112117
group_name = group_name,
113118
group_deps = group_deps,
114119
annotation = _get_annotation(requirement),
120+
config_load = config_load,
115121
**whl_config
116122
)

python/pip_install/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ bzl_library(
2222
name = "pip_repository_bzl",
2323
srcs = ["pip_repository.bzl"],
2424
deps = [
25-
"//python/private/pypi:group_library_bzl",
2625
"//python/private/pypi:package_annotation_bzl",
2726
"//python/private/pypi:pip_repository_bzl",
27+
"//python/private/pypi:whl_config_repo_bzl",
2828
"//python/private/pypi:whl_library_bzl",
2929
],
3030
)

python/pip_install/pip_repository.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
""
1616

17-
load("//python/private/pypi:group_library.bzl", _group_library = "group_library")
1817
load("//python/private/pypi:package_annotation.bzl", _package_annotation = "package_annotation")
1918
load("//python/private/pypi:pip_repository.bzl", _pip_repository = "pip_repository")
19+
load("//python/private/pypi:whl_config_repo.bzl", _whl_config_repo = "whl_config_repo")
2020
load("//python/private/pypi:whl_library.bzl", _whl_library = "whl_library")
2121

2222
# Re-exports for backwards compatibility
23-
group_library = _group_library
23+
group_library = _whl_config_repo
2424
pip_repository = _pip_repository
2525
whl_library = _whl_library
26+
whl_config_repo = _whl_config_repo
2627
package_annotation = _package_annotation

python/private/internal_config_repo.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ load("//python/private:text_util.bzl", "render")
2222
load(":repo_utils.bzl", "repo_utils")
2323

2424
_ENABLE_PIPSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PIPSTAR"
25-
_ENABLE_PIPSTAR_DEFAULT = "0"
25+
_ENABLE_PIPSTAR_DEFAULT = "1"
2626
_ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS"
2727
_ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0"
2828

python/private/pypi/BUILD.bazel

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@ bzl_library(
157157
],
158158
)
159159

160-
bzl_library(
161-
name = "group_library_bzl",
162-
srcs = ["group_library.bzl"],
163-
deps = [
164-
":generate_group_library_build_bazel_bzl",
165-
],
166-
)
167-
168160
bzl_library(
169161
name = "hub_builder_bzl",
170162
srcs = ["hub_builder.bzl"],
@@ -408,6 +400,15 @@ bzl_library(
408400
],
409401
)
410402

403+
bzl_library(
404+
name = "whl_config_repo_bzl",
405+
srcs = ["whl_config_repo.bzl"],
406+
deps = [
407+
":generate_group_library_build_bazel_bzl",
408+
"//python/private:text_util_bzl",
409+
],
410+
)
411+
411412
bzl_library(
412413
name = "whl_config_setting_bzl",
413414
srcs = ["whl_config_setting.bzl"],

python/private/pypi/config.bzl.tmpl.bzlmod renamed to python/private/pypi/config.bzl.tmpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
NOTE: This is internal `rules_python` API and if you would like to depend on it, please raise an issue
44
with your usecase. This may change in between rules_python versions without any notice.
5-
6-
@generated by rules_python pip.parse bzlmod extension.
75
"""
86

9-
whl_map = %%WHL_MAP%%
7+
packages = %%PACKAGES%%

python/private/pypi/generate_whl_library_build_bazel.bzl

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def generate_whl_library_build_bazel(
7272
"requires",
7373
"metadata_name",
7474
"metadata_version",
75+
"packages",
7576
"include",
7677
]
7778
else:
@@ -82,17 +83,13 @@ def generate_whl_library_build_bazel(
8283
"target_platforms",
8384
"default_python_version",
8485
]
85-
dep_template = kwargs.get("dep_template")
86-
loads.append(
87-
"""load("{}", "{}")""".format(
88-
dep_template.format(
89-
name = "",
90-
target = "config.bzl",
91-
),
92-
"whl_map",
93-
),
94-
)
95-
kwargs["include"] = "whl_map"
86+
packages_load = kwargs.pop("config_load")
87+
if not kwargs.get("requires_dist"):
88+
# no deps, we can leave the extra loads out
89+
pass
90+
else:
91+
loads.append("""load("{}", "{}")""".format(packages_load, "packages"))
92+
kwargs["include"] = "packages"
9693

9794
for arg in unsupported_args:
9895
if kwargs.get(arg):

python/private/pypi/group_library.bzl

Lines changed: 0 additions & 40 deletions
This file was deleted.

python/private/pypi/hub_builder.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ def _common_args(self, module_ctx, *, pip_attr):
451451
# attrs.
452452
whl_library_args = dict(
453453
dep_template = "@{}//{{name}}:{{target}}".format(self.name),
454+
config_load = "@{}//:config.bzl".format(self.name),
454455
)
455456
maybe_args = dict(
456457
# The following values are safe to omit if they have false like values

0 commit comments

Comments
 (0)