Skip to content

feat: add an env variable to toggle pipstar #2855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 5, 2025
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ END_UNRELEASED_TEMPLATE
(the default), the subprocess's stdout/stderr will be logged.
* (toolchains) Local toolchains can be activated with custom flags. See
[Conditionally using local toolchains] docs for how to configure.
* (pypi) `RULES_PYTHON_ENABLE_PIPSTAR` environment variable: when `1`, the Starlark
implementation of wheel METADATA parsing is used (which has improved multi-platform
build support).

{#v0-0-0-removed}
### Removed
Expand Down
9 changes: 9 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ The default became `1` if unspecified
:::
::::

::::{envvar} RULES_PYTHON_ENABLE_PIPSTAR

When `1`, the rules_python Starlark implementation of the pypi/pip integration is used
instead of the legacy Python scripts.

:::{versionadded} VERSION_NEXT_FEATURE
:::
::::

::::{envvar} RULES_PYTHON_EXTRACT_ROOT

Directory to use as the root for creating files necessary for bootstrapping so
Expand Down
4 changes: 4 additions & 0 deletions python/private/internal_config_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ settings for rules to later use.

load(":repo_utils.bzl", "repo_utils")

_ENABLE_PIPSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PIPSTAR"
_ENABLE_PIPSTAR_DEFAULT = "0"
_ENABLE_PYSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PYSTAR"
_ENABLE_PYSTAR_DEFAULT = "1"
_ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS"
Expand All @@ -28,6 +30,7 @@ _ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0"
_CONFIG_TEMPLATE = """\
config = struct(
enable_pystar = {enable_pystar},
enable_pipstar = {enable_pipstar},
enable_deprecation_warnings = {enable_deprecation_warnings},
BuiltinPyInfo = getattr(getattr(native, "legacy_globals", None), "PyInfo", {builtin_py_info_symbol}),
BuiltinPyRuntimeInfo = getattr(getattr(native, "legacy_globals", None), "PyRuntimeInfo", {builtin_py_runtime_info_symbol}),
Expand Down Expand Up @@ -84,6 +87,7 @@ def _internal_config_repo_impl(rctx):

rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format(
enable_pystar = enable_pystar,
enable_pipstar = _bool_from_environ(rctx, _ENABLE_PIPSTAR_ENVVAR_NAME, _ENABLE_PIPSTAR_DEFAULT),
enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT),
builtin_py_info_symbol = builtin_py_info_symbol,
builtin_py_runtime_info_symbol = builtin_py_runtime_info_symbol,
Expand Down
5 changes: 5 additions & 0 deletions python/private/pypi/whl_installer/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def parser(**kwargs: Any) -> argparse.ArgumentParser:
type=Platform.from_string,
help="Platforms to target dependencies. Can be used multiple times.",
)
parser.add_argument(
"--enable-pipstar",
action="store_true",
help="Disable certain code paths if we expect to process the whl in Starlark.",
)
parser.add_argument(
"--pip_data_exclude",
action="store",
Expand Down
44 changes: 26 additions & 18 deletions python/private/pypi/whl_installer/wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def _setup_namespace_pkg_compatibility(wheel_dir: str) -> None:
def _extract_wheel(
wheel_file: str,
extras: Dict[str, Set[str]],
enable_pipstar: bool,
enable_implicit_namespace_pkgs: bool,
platforms: List[wheel.Platform],
installation_dir: Path = Path("."),
Expand All @@ -114,6 +115,7 @@ def _extract_wheel(
wheel_file: the filepath of the .whl
installation_dir: the destination directory for installation of the wheel.
extras: a list of extras to add as dependencies for the installed wheel
enable_pipstar: if true, turns off certain operations.
enable_implicit_namespace_pkgs: if true, disables conversion of implicit namespace packages and will unzip as-is
"""

Expand All @@ -123,26 +125,31 @@ def _extract_wheel(
if not enable_implicit_namespace_pkgs:
_setup_namespace_pkg_compatibility(installation_dir)

extras_requested = extras[whl.name] if whl.name in extras else set()

dependencies = whl.dependencies(extras_requested, platforms)
metadata = {
"python_version": f"{sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}",
"entry_points": [
{
"name": name,
"module": module,
"attribute": attribute,
}
for name, (module, attribute) in sorted(whl.entry_points().items())
],
}
if not enable_pipstar:
extras_requested = extras[whl.name] if whl.name in extras else set()
dependencies = whl.dependencies(extras_requested, platforms)

metadata.update(
{
"name": whl.name,
"version": whl.version,
"deps": dependencies.deps,
"deps_by_platform": dependencies.deps_select,
}
)

with open(os.path.join(installation_dir, "metadata.json"), "w") as f:
metadata = {
"name": whl.name,
"version": whl.version,
"deps": dependencies.deps,
"python_version": f"{sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}",
"deps_by_platform": dependencies.deps_select,
"entry_points": [
{
"name": name,
"module": module,
"attribute": attribute,
}
for name, (module, attribute) in sorted(whl.entry_points().items())
],
}
json.dump(metadata, f)


Expand All @@ -161,6 +168,7 @@ def main() -> None:
_extract_wheel(
wheel_file=whl,
extras=extras,
enable_pipstar=args.enable_pipstar,
enable_implicit_namespace_pkgs=args.enable_implicit_namespace_pkgs,
platforms=arguments.get_platforms(args),
)
Expand Down
207 changes: 139 additions & 68 deletions python/private/pypi/whl_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@

""

load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth")
load("//python/private:envsubst.bzl", "envsubst")
load("//python/private:is_standalone_interpreter.bzl", "is_standalone_interpreter")
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
load(":attrs.bzl", "ATTRS", "use_isolated")
load(":deps.bzl", "all_repo_names", "record_files")
load(":generate_whl_library_build_bazel.bzl", "generate_whl_library_build_bazel")
load(":parse_requirements.bzl", "host_platform")
load(":parse_whl_name.bzl", "parse_whl_name")
load(":patch_whl.bzl", "patch_whl")
load(":pypi_repo_utils.bzl", "pypi_repo_utils")
load(":whl_metadata.bzl", "whl_metadata")
load(":whl_target_platforms.bzl", "whl_target_platforms")

_CPPFLAGS = "CPPFLAGS"
Expand Down Expand Up @@ -340,79 +343,147 @@ def _whl_library_impl(rctx):
timeout = rctx.attr.timeout,
)

target_platforms = rctx.attr.experimental_target_platforms or []
if target_platforms:
parsed_whl = parse_whl_name(whl_path.basename)

# NOTE @aignas 2023-12-04: if the wheel is a platform specific wheel, we
# only include deps for that target platform
if parsed_whl.platform_tag != "any":
target_platforms = [
p.target_platform
for p in whl_target_platforms(
platform_tag = parsed_whl.platform_tag,
abi_tag = parsed_whl.abi_tag.strip("tm"),
)
]

pypi_repo_utils.execute_checked(
rctx,
op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path),
python = python_interpreter,
arguments = args + [
"--whl-file",
whl_path,
] + ["--platform={}".format(p) for p in target_platforms],
srcs = rctx.attr._python_srcs,
environment = environment,
quiet = rctx.attr.quiet,
timeout = rctx.attr.timeout,
logger = logger,
)
if rp_config.enable_pipstar:
pypi_repo_utils.execute_checked(
rctx,
op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path),
python = python_interpreter,
arguments = args + [
"--whl-file",
whl_path,
"--enable-pipstar",
],
srcs = rctx.attr._python_srcs,
environment = environment,
quiet = rctx.attr.quiet,
timeout = rctx.attr.timeout,
logger = logger,
)

metadata = json.decode(rctx.read("metadata.json"))
rctx.delete("metadata.json")
metadata = json.decode(rctx.read("metadata.json"))
rctx.delete("metadata.json")
python_version = metadata["python_version"]

# NOTE @aignas 2024-06-22: this has to live on until we stop supporting
# passing `twine` as a `:pkg` library via the `WORKSPACE` builds.
#
# See ../../packaging.bzl line 190
entry_points = {}
for item in metadata["entry_points"]:
name = item["name"]
module = item["module"]
attribute = item["attribute"]

# There is an extreme edge-case with entry_points that end with `.py`
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name
entry_point_target_name = (
_WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py
# NOTE @aignas 2024-06-22: this has to live on until we stop supporting
# passing `twine` as a `:pkg` library via the `WORKSPACE` builds.
#
# See ../../packaging.bzl line 190
entry_points = {}
for item in metadata["entry_points"]:
name = item["name"]
module = item["module"]
attribute = item["attribute"]

# There is an extreme edge-case with entry_points that end with `.py`
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name
entry_point_target_name = (
_WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py
)
entry_point_script_name = entry_point_target_name + ".py"

rctx.file(
entry_point_script_name,
_generate_entry_point_contents(module, attribute),
)
entry_points[entry_point_without_py] = entry_point_script_name

metadata = whl_metadata(
install_dir = whl_path.dirname.get_child("site-packages"),
read_fn = rctx.read,
logger = logger,
)
entry_point_script_name = entry_point_target_name + ".py"

rctx.file(
entry_point_script_name,
_generate_entry_point_contents(module, attribute),
build_file_contents = generate_whl_library_build_bazel(
name = whl_path.basename,
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix),
entry_points = entry_points,
metadata_name = metadata.name,
metadata_version = metadata.version,
default_python_version = python_version,
requires_dist = metadata.requires_dist,
target_platforms = rctx.attr.experimental_target_platforms or [host_platform(rctx)],
# TODO @aignas 2025-04-14: load through the hub:
annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))),
data_exclude = rctx.attr.pip_data_exclude,
group_deps = rctx.attr.group_deps,
group_name = rctx.attr.group_name,
)
entry_points[entry_point_without_py] = entry_point_script_name

build_file_contents = generate_whl_library_build_bazel(
name = whl_path.basename,
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix),
entry_points = entry_points,
# TODO @aignas 2025-04-14: load through the hub:
dependencies = metadata["deps"],
dependencies_by_platform = metadata["deps_by_platform"],
annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))),
data_exclude = rctx.attr.pip_data_exclude,
group_deps = rctx.attr.group_deps,
group_name = rctx.attr.group_name,
tags = [
"pypi_name={}".format(metadata["name"]),
"pypi_version={}".format(metadata["version"]),
],
)
else:
target_platforms = rctx.attr.experimental_target_platforms or []
if target_platforms:
parsed_whl = parse_whl_name(whl_path.basename)

# NOTE @aignas 2023-12-04: if the wheel is a platform specific wheel, we
# only include deps for that target platform
if parsed_whl.platform_tag != "any":
target_platforms = [
p.target_platform
for p in whl_target_platforms(
platform_tag = parsed_whl.platform_tag,
abi_tag = parsed_whl.abi_tag.strip("tm"),
)
]

pypi_repo_utils.execute_checked(
rctx,
op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path),
python = python_interpreter,
arguments = args + [
"--whl-file",
whl_path,
] + ["--platform={}".format(p) for p in target_platforms],
srcs = rctx.attr._python_srcs,
environment = environment,
quiet = rctx.attr.quiet,
timeout = rctx.attr.timeout,
logger = logger,
)

metadata = json.decode(rctx.read("metadata.json"))
rctx.delete("metadata.json")

# NOTE @aignas 2024-06-22: this has to live on until we stop supporting
# passing `twine` as a `:pkg` library via the `WORKSPACE` builds.
#
# See ../../packaging.bzl line 190
entry_points = {}
for item in metadata["entry_points"]:
name = item["name"]
module = item["module"]
attribute = item["attribute"]

# There is an extreme edge-case with entry_points that end with `.py`
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name
entry_point_target_name = (
_WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py
)
entry_point_script_name = entry_point_target_name + ".py"

rctx.file(
entry_point_script_name,
_generate_entry_point_contents(module, attribute),
)
entry_points[entry_point_without_py] = entry_point_script_name

build_file_contents = generate_whl_library_build_bazel(
name = whl_path.basename,
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix),
entry_points = entry_points,
# TODO @aignas 2025-04-14: load through the hub:
dependencies = metadata["deps"],
dependencies_by_platform = metadata["deps_by_platform"],
annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))),
data_exclude = rctx.attr.pip_data_exclude,
group_deps = rctx.attr.group_deps,
group_name = rctx.attr.group_name,
tags = [
"pypi_name={}".format(metadata["name"]),
"pypi_version={}".format(metadata["version"]),
],
)

rctx.file("BUILD.bazel", build_file_contents)

return
Expand Down
1 change: 1 addition & 0 deletions tests/pypi/whl_installer/wheel_installer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_wheel_exists(self) -> None:
extras={},
enable_implicit_namespace_pkgs=False,
platforms=[],
enable_pipstar = False,
)

want_files = [
Expand Down