Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
7ea54f5
refactor(pypi): do not parse the versions to select the wheels
aignas Nov 21, 2026
4c177f8
refactor(coverage): do not evaluate the markers to select the wheels
aignas Nov 21, 2026
cb060eb
refactor: normalize the names with pypackaging.bzl
aignas Nov 21, 2026
d3c8627
build: add pypackaging.bzl 0.3.0 as a dependency
aignas Nov 21, 2026
2a57aeb
refactor(pypi): parse the requirements with pypackaging.bzl
aignas Nov 21, 2026
d0aec1f
refactor(pypi): evaluate the markers with pypackaging.bzl
aignas Nov 21, 2026
9020c92
tests: update the lockfile of the bzlmod_lockfile integration test
aignas Nov 21, 2026
9b94b81
Merge branch 'refactor.aignas.pypackaging-03-dep' into refactor.aigna…
aignas Nov 22, 2026
356cceb
revert: keep normalize_name free of pypackaging.bzl
aignas Nov 22, 2026
5ef4a74
Merge branch 'refactor.aignas.pypackaging-05-requirements' into refac…
aignas Nov 22, 2026
972580f
Merge remote-tracking branch 'upstream/main' into refactor.aignas.pyp…
aignas Nov 22, 2026
e36a56f
Merge branch 'refactor.aignas.pypackaging-02-coverage-deps' into refa…
aignas Nov 22, 2026
a7b02dd
Merge branch 'refactor.aignas.pypackaging-03-dep' into refactor.aigna…
aignas Nov 22, 2026
9c4e4ab
Merge branch 'refactor.aignas.pypackaging-05-requirements' into refac…
aignas Nov 22, 2026
924a90d
Merge branch 'refactor.aignas.pypackaging-01-select-whl' into refacto…
aignas Nov 22, 2026
69cac6c
build: use pypackaging.bzl 0.4.0
aignas Nov 22, 2026
18cc7d9
Merge branch 'refactor.aignas.pypackaging-03-dep' into refactor.aigna…
aignas Nov 22, 2026
9473995
Merge branch 'refactor.aignas.pypackaging-05-requirements' into refac…
aignas Nov 22, 2026
fe38326
Merge remote-tracking branch 'upstream/main' into refactor.aignas.pyp…
aignas Nov 22, 2026
180fe43
Merge branch 'refactor.aignas.pypackaging-02-coverage-deps' into refa…
aignas Nov 22, 2026
6a53a41
Merge branch 'refactor.aignas.pypackaging-03-dep' into refactor.aigna…
aignas Nov 22, 2026
c88ac64
Merge branch 'refactor.aignas.pypackaging-05-requirements' into refac…
aignas Nov 22, 2026
e263e7a
Merge remote-tracking branch 'upstream/main' into refactor.aignas.pyp…
aignas Nov 22, 2026
1ccf5c6
Merge branch 'refactor.aignas.pypackaging-02-coverage-deps' into refa…
aignas Nov 22, 2026
be5da73
Merge branch 'refactor.aignas.pypackaging-03-dep' into refactor.aigna…
aignas Nov 22, 2026
62fd871
Merge branch 'refactor.aignas.pypackaging-05-requirements' into refac…
aignas Nov 22, 2026
ee0ddc7
fix(pypi): accept tabs before the options and markers right after the…
aignas Nov 22, 2026
51b9eec
Merge branch 'refactor.aignas.pypackaging-05-requirements' into refac…
aignas Nov 22, 2026
6bff817
refactor(pypi): use the partial marker evaluation of pypackaging.bzl
aignas Nov 22, 2026
2390c98
build: add pypackaging.bzl 0.3.0 as a dependency
aignas Nov 21, 2026
918e6c6
tests: update the lockfile of the bzlmod_lockfile integration test
aignas Nov 21, 2026
910bc28
build: use pypackaging.bzl 0.4.0
aignas Nov 22, 2026
d0a2382
refactor: normalize the names with pypackaging.bzl
aignas Nov 21, 2026
3f1bdca
refactor(pypi): parse the requirements with pypackaging.bzl
aignas Nov 21, 2026
470aa71
revert: keep normalize_name free of pypackaging.bzl
aignas Nov 22, 2026
80f5be2
fix(pypi): accept tabs before the options and markers right after the…
aignas Nov 22, 2026
c07b4fa
refactor(pypi): cache parsed requirements across files and polish docs
rickeylev Nov 23, 2026
744f304
fix(pypi): use the platform_machine aliases for the value in the env
aignas Nov 23, 2026
3777668
refactor(pypi): use the types helpers and keep the platform_machine a…
aignas Nov 23, 2026
476e5d6
fix(pypi): only strip the options outside of the quoted strings of th…
aignas Nov 23, 2026
c55d291
Merge remote-tracking branch 'upstream/refactor.aignas.pypackaging-05…
aignas Nov 23, 2026
9528040
refactor(pypi): document and wrap pep508_evaluate helpers
rickeylev Nov 24, 2026
634d30e
docs(pypi): condense pypackaging-bzl news entry
rickeylev Nov 24, 2026
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: 2 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ load("@package_metadata//rules:package_metadata.bzl", "package_metadata")
# gazelle:resolve starlark @bazel_tools//tools/build_defs/repo:http.bzl //python/private:bazel_tools
# gazelle:resolve starlark @bazel_tools//tools/build_defs/repo:utils.bzl //python/private:bazel_tools

# Prevent Gazelle from incorrectly stripping the .bzl suffix from the toml.bzl repo name
# Prevent Gazelle from incorrectly stripping the .bzl suffix from the repo names
# gazelle:resolve starlark @pypackaging.bzl//:pypackaging.bzl @pypackaging.bzl//:pypackaging
# gazelle:resolve starlark @toml.bzl//:toml.bzl @toml.bzl//:toml

# Override Gazelle's incorrect default resolution for platforms host constraints Starlark library
Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bazel_dep(name = "bazel_features", version = "1.55.0")
bazel_dep(name = "bazel_skylib", version = "1.8.2")
bazel_dep(name = "package_metadata", version = "0.0.13")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "pypackaging.bzl", version = "0.4.0")
bazel_dep(name = "rules_cc", version = "0.2.17")
bazel_dep(name = "toml.bzl", version = "0.4.1")

Expand Down
9 changes: 6 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ load("@stardoc_maven//:defs.bzl", stardoc_pinned_maven_install = "pinned_maven_i

stardoc_pinned_maven_install()

load("//python:repositories.bzl", "py_repositories", "python_register_multi_toolchains")

# This needs to be called before the internal setup, because the repository
# rules that it loads use the dependencies defined here.
py_repositories()

load("//:internal_dev_setup.bzl", "rules_python_internal_setup")

rules_python_internal_setup()

load("@pythons_hub//:versions.bzl", "PYTHON_VERSIONS")
load("//python:repositories.bzl", "py_repositories", "python_register_multi_toolchains")

py_repositories()

python_register_multi_toolchains(
name = "python",
Expand Down
2 changes: 2 additions & 0 deletions downloader_config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rewrite ^github\.com/bazelbuild/rules_swift/(.*) github.com/bazelbuild/rules_swi
rewrite ^github\.com/bazelbuild/rules_testing/(.*) github.com/bazelbuild/rules_testing/$1
rewrite ^github\.com/bazelbuild/stardoc/(.*) github.com/bazelbuild/stardoc/$1
rewrite ^github\.com/google/googletest/(.*) github.com/google/googletest/$1
rewrite ^github\.com/jvolkman/pypackaging\.bzl/(.*) github.com/jvolkman/pypackaging.bzl/$1
rewrite ^github\.com/jvolkman/toml\.bzl/(.*) github.com/jvolkman/toml.bzl/$1
rewrite ^github\.com/protocolbuffers/protobuf/(.*) github.com/protocolbuffers/protobuf/$1
# keep-sorted end
Expand Down Expand Up @@ -48,6 +49,7 @@ rewrite ^github\.com/bazelbuild/rules_swift/(.*) mirror.bazel.build/github.com/b
rewrite ^github\.com/bazelbuild/rules_testing/(.*) mirror.bazel.build/github.com/bazelbuild/rules_testing/$1
rewrite ^github\.com/bazelbuild/stardoc/(.*) mirror.bazel.build/github.com/bazelbuild/stardoc/$1
rewrite ^github\.com/google/googletest/(.*) mirror.bazel.build/github.com/google/googletest/$1
rewrite ^github\.com/jvolkman/pypackaging\.bzl/(.*) mirror.bazel.build/github.com/jvolkman/pypackaging.bzl/$1
rewrite ^github\.com/jvolkman/toml\.bzl/(.*) mirror.bazel.build/github.com/jvolkman/toml.bzl/$1
rewrite ^github\.com/protocolbuffers/protobuf/(.*) mirror.bazel.build/github.com/protocolbuffers/protobuf/$1
# keep-sorted end
2 changes: 2 additions & 0 deletions gazelle/downloader_config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rewrite ^github\.com/bazelbuild/rules_swift/(.*) github.com/bazelbuild/rules_swi
rewrite ^github\.com/bazelbuild/rules_testing/(.*) github.com/bazelbuild/rules_testing/$1
rewrite ^github\.com/bazelbuild/stardoc/(.*) github.com/bazelbuild/stardoc/$1
rewrite ^github\.com/google/googletest/(.*) github.com/google/googletest/$1
rewrite ^github\.com/jvolkman/pypackaging\.bzl/(.*) github.com/jvolkman/pypackaging.bzl/$1
rewrite ^github\.com/jvolkman/toml\.bzl/(.*) github.com/jvolkman/toml.bzl/$1
rewrite ^github\.com/protocolbuffers/protobuf/(.*) github.com/protocolbuffers/protobuf/$1
# keep-sorted end
Expand Down Expand Up @@ -48,6 +49,7 @@ rewrite ^github\.com/bazelbuild/rules_swift/(.*) mirror.bazel.build/github.com/b
rewrite ^github\.com/bazelbuild/rules_testing/(.*) mirror.bazel.build/github.com/bazelbuild/rules_testing/$1
rewrite ^github\.com/bazelbuild/stardoc/(.*) mirror.bazel.build/github.com/bazelbuild/stardoc/$1
rewrite ^github\.com/google/googletest/(.*) mirror.bazel.build/github.com/google/googletest/$1
rewrite ^github\.com/jvolkman/pypackaging\.bzl/(.*) mirror.bazel.build/github.com/jvolkman/pypackaging.bzl/$1
rewrite ^github\.com/jvolkman/toml\.bzl/(.*) mirror.bazel.build/github.com/jvolkman/toml.bzl/$1
rewrite ^github\.com/protocolbuffers/protobuf/(.*) mirror.bazel.build/github.com/protocolbuffers/protobuf/$1
# keep-sorted end
3 changes: 3 additions & 0 deletions news/pypackaging-bzl.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(pypi) PEP 508 requirements and environment markers now match
[`packaging`](https://github.com/pypa/packaging) and reject invalid syntax via
[`pypackaging.bzl`](https://github.com/jvolkman/pypackaging.bzl).
6 changes: 6 additions & 0 deletions python/private/py_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def py_repositories(transition_settings = []):
strip_prefix = "toml.bzl-0.4.0",
url = "https://github.com/jvolkman/toml.bzl/releases/download/v0.4.0/toml.bzl-v0.4.0.tar.gz",
)
http_archive(
name = "pypackaging.bzl",
sha256 = "d234430a5425e8b090c0071944036516a3516b41f9c9d005113fff0533f591b3",
strip_prefix = "pypackaging.bzl-0.4.0",
url = "https://github.com/jvolkman/pypackaging.bzl/releases/download/v0.4.0/pypackaging.bzl-v0.4.0.tar.gz",
)
http_archive(
name = "package_metadata",
sha256 = "755bd6c8a3751982e2555d96c913ec070954ca9d01f935563392255da4ebad72",
Expand Down
12 changes: 4 additions & 8 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ bzl_library(
name = "pep508_deps",
srcs = ["pep508_deps.bzl"],
deps = [
":pep508_env",
":pep508_evaluate",
":pep508_requirement",
"//python/private:normalize_name",
Expand All @@ -294,25 +293,22 @@ bzl_library(
bzl_library(
name = "pep508_env",
srcs = ["pep508_env.bzl"],
deps = [
"//python/private:normalize_name",
"//python/private:version",
],
deps = ["//python/private:version"],
)

bzl_library(
name = "pep508_evaluate",
srcs = ["pep508_evaluate.bzl"],
deps = [
"//python/private:enum",
"//python/private:version",
"@bazel_skylib//lib:types",
"@pypackaging.bzl//:pypackaging",
],
)

bzl_library(
name = "pep508_requirement",
srcs = ["pep508_requirement.bzl"],
deps = ["//python/private:normalize_name"],
deps = ["@pypackaging.bzl//:pypackaging"],
)

bzl_library(
Expand Down
7 changes: 2 additions & 5 deletions python/private/pypi/env_marker_setting.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("//python/private:common_labels.bzl", "labels")
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
load(":env_marker_info.bzl", "EnvMarkerInfo")
load(":pep508_env.bzl", "create_env", "set_missing_env_defaults")
load(":pep508_env.bzl", "set_missing_env_defaults")
load(":pep508_evaluate.bzl", "evaluate")

# Use capitals to hint its not an actual boolean type.
Expand Down Expand Up @@ -38,10 +38,7 @@ def env_marker_setting(*, name, expression, **kwargs):
)

def _env_marker_setting_impl(ctx):
env = create_env()
env.update(
ctx.attr._env_marker_config_flag[EnvMarkerInfo].env,
)
env = dict(ctx.attr._env_marker_config_flag[EnvMarkerInfo].env)

runtime = ctx.toolchains[TARGET_TOOLCHAIN_TYPE].py3_runtime

Expand Down
3 changes: 1 addition & 2 deletions python/private/pypi/flags.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ load("//python/private:common_labels.bzl", "labels")
load(":env_marker_info.bzl", "EnvMarkerInfo")
load(
":pep508_env.bzl",
"create_env",
"os_name_select_map",
"platform_machine_select_map",
"platform_system_select_map",
Expand Down Expand Up @@ -54,7 +53,7 @@ def _default_env_marker_config(**kwargs):
)

def _env_marker_config_impl(ctx):
env = create_env()
env = {}
env["os_name"] = ctx.attr.os_name
env["sys_platform"] = ctx.attr.sys_platform
env["platform_machine"] = ctx.attr.platform_machine
Expand Down
27 changes: 16 additions & 11 deletions python/private/pypi/parse_requirements.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ def _parse_requirements_from_req_files(
all_files_parsed = {}
index_url = None
extra_index_urls = []

# The same lines are used on many platforms, so parse them once.
parsed_reqs = {}
for file, plats in requirements_by_platform.items():
logger.trace(lambda: "Using {} for {}".format(file, plats))
contents = ctx.read(file)
Expand Down Expand Up @@ -489,17 +492,21 @@ def _parse_requirements_from_req_files(
# (3.0).
get_index_urls = None

# Pre-parse requirements once per file to avoid redundant parsing in loops
parsed_reqs = [(entry, requirement(entry[1])) for entry in parse_result.requirements]
for _, req_line in parse_result.requirements:
if req_line not in parsed_reqs:
parsed_reqs[req_line] = requirement(req_line)

for plat in plats:
plat_env = platforms.get(plat)

requirements[plat] = [
entry
for entry, req in parsed_reqs
if not req.marker or (plat_env and evaluate(req.marker, env = plat_env.env))
]
plat_reqs = []
for entry in parse_result.requirements:
marker = parsed_reqs[entry[1]].marker
if not marker or (
plat_env and evaluate(marker, env = plat_env.env)
):
plat_reqs.append(entry)
requirements[plat] = plat_reqs

options[plat] = pip_args

Expand All @@ -511,16 +518,14 @@ def _parse_requirements_from_req_files(

reqs_by_name = {}
requirements_by_platform = {}

for plat, parse_results in requirements.items():
requirements_dict = {}
for entry in sorted(
parse_results,
key = lambda x: (len(x[1].partition("==")[0]), x),
):
req_line = entry[1]
req = requirement(req_line)

requirements_dict[req.name] = entry
requirements_dict[parsed_reqs[entry[1]].name] = entry

extra_pip_args_for_plat = options[plat]

Expand Down
7 changes: 2 additions & 5 deletions python/private/pypi/pep508_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"""

load("//python/private:normalize_name.bzl", "normalize_name")
load(":pep508_env.bzl", "create_env")
load(":pep508_evaluate.bzl", "evaluate")
load(":pep508_requirement.bzl", "requirement")

Expand Down Expand Up @@ -168,9 +167,8 @@ def _resolve_extras(self_name, reqs, extras):
return sorted(extras)

def _evaluate_any(req, extras):
env = create_env()
for extra in extras:
if evaluate(req.marker, env = env | {"extra": extra}):
if evaluate(req.marker, env = {"extra": extra}):
return True

return False
Expand All @@ -181,12 +179,11 @@ def _add_reqs(deps, deps_select, dep, reqs, *, extras):
_add(deps, deps_select, dep)
return

env = create_env()
markers = {}
found_unconditional = False
for req in reqs:
for x in extras:
m = evaluate(req.marker, env = env | {"extra": x}, strict = False)
m = evaluate(req.marker, env = {"extra": x}, strict = False)
if m == False:
continue
elif m == True:
Expand Down
23 changes: 1 addition & 22 deletions python/private/pypi/pep508_env.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""This module is for implementing PEP508 environment definition.
"""

load("//python/private:normalize_name.bzl", "normalize_name")
load("//python/private:version.bzl", "version")

_DEFAULT = "//conditions:default"
Expand All @@ -31,15 +30,6 @@ _os_aliases = {
}

# See https://stackoverflow.com/a/45125525
platform_machine_aliases = {
# These pairs mean the same hardware, but different values may be used
# on different host platforms.
"amd64": "x86_64",
"arm64": "aarch64",
"i386": "x86_32",
"i686": "x86_32",
}

# NOTE: There are many cpus, and unfortunately, the value isn't directly
# accessible to Starlark. Using CcToolchain.cpu might work, though.
# Some targets are aliases and are omitted below as their value is implied
Expand Down Expand Up @@ -184,8 +174,7 @@ def env(*, env = None, os, arch, python_version = "", extra = None):
Returns:
A dict that can be used as `env` in the marker evaluation.
"""
env = env or {}
env = env | create_env()
env = dict(env or {})
if extra != None:
env["extra"] = extra

Expand Down Expand Up @@ -214,16 +203,6 @@ def env(*, env = None, os, arch, python_version = "", extra = None):

return env

def create_env():
return {
# Per-variable normalization functions. Each entry maps a marker
# variable name to a function (value) -> normalized_value.
"_aliases": {
"extra": normalize_name,
"platform_machine": lambda x: platform_machine_aliases.get(x, x),
},
}

def set_missing_env_defaults(env):
"""Sets defaults based on existing values.

Expand Down
Loading
Loading