Repository navigation
[ci] Let the environment choose the Python package index - #65562
elliot-barn wants to merge 2 commits into
Conversation
pip's index is currently hardwired to whatever the default is at every point where CI resolves Python packages, so pointing a fleet at a caching mirror instead of pypi.org cannot be done without patching WORKSPACE and every step. That matters while files.pythonhosted.org returns intermittent 502s (pypi/support#11895): a single 502 fails a whl_library repository rule and takes the build with it. Two pieces are needed, and neither pins a value: - .bazelrc forwards PIP_INDEX_URL and RULES_PYTHON_PIP_ISOLATED to repository rules as valueless --repo_env, so bazel inherits them from the invoking environment. rules_python runs whl_library's pip with --isolated, which makes pip ignore every PIP_* variable, so the second one is what lets the first through. Putting this in the repo's .bazelrc rather than a generated bazelrc means every bazel invocation reads it, including the nested ones that a --config flag on a step's command line never reaches. - ci/ray_ci forwards PIP_INDEX_URL, UV_INDEX_URL and RULES_PYTHON_PIP_ISOLATED into the test containers, since bazel there needs them in its environment for the passthrough above to have anything to inherit. Both are unset in a plain checkout. Docker passes nothing through for an unset variable and bazel inherits nothing, so pip and uv resolve from PyPI exactly as they do today. Signed-off-by: elliot-barn <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request configures Bazel and the Ray CI container environment to forward Python package index environment variables (such as PIP_INDEX_URL, UV_INDEX_URL, and RULES_PYTHON_PIP_ISOLATED) so that nested containers and repository rules can resolve packages from custom or caching mirrors. The review feedback suggests extending this support to cover additional related environment variables, specifically PIP_EXTRA_INDEX_URL, PIP_TRUSTED_HOST, and UV_EXTRA_INDEX_URL, and updating the corresponding test assertions to match.
| common --repo_env=PIP_INDEX_URL | ||
| common --repo_env=RULES_PYTHON_PIP_ISOLATED |
There was a problem hiding this comment.
To fully support private or caching mirrors (especially those running on HTTP or requiring secondary indices), we should also forward PIP_EXTRA_INDEX_URL and PIP_TRUSTED_HOST to the repository rules.
common --repo_env=PIP_INDEX_URL
common --repo_env=PIP_EXTRA_INDEX_URL
common --repo_env=PIP_TRUSTED_HOST
common --repo_env=RULES_PYTHON_PIP_ISOLATED
| "PIP_INDEX_URL", | ||
| "UV_INDEX_URL", | ||
| "RULES_PYTHON_PIP_ISOLATED", |
There was a problem hiding this comment.
To ensure the nested containers can fully resolve packages from private/caching mirrors, we should also forward PIP_EXTRA_INDEX_URL, PIP_TRUSTED_HOST, and UV_EXTRA_INDEX_URL into the container environment.
"PIP_INDEX_URL",
"PIP_EXTRA_INDEX_URL",
"PIP_TRUSTED_HOST",
"UV_INDEX_URL",
"UV_EXTRA_INDEX_URL",
"RULES_PYTHON_PIP_ISOLATED",| assert ( | ||
| "--env PIP_INDEX_URL --env UV_INDEX_URL " | ||
| "--env RULES_PYTHON_PIP_ISOLATED" in input_str | ||
| ) |
There was a problem hiding this comment.
Update the test assertion to include the newly forwarded environment variables (PIP_EXTRA_INDEX_URL, PIP_TRUSTED_HOST, and UV_EXTRA_INDEX_URL).
| assert ( | |
| "--env PIP_INDEX_URL --env UV_INDEX_URL " | |
| "--env RULES_PYTHON_PIP_ISOLATED" in input_str | |
| ) | |
| assert ( | |
| "--env PIP_INDEX_URL --env PIP_EXTRA_INDEX_URL --env PIP_TRUSTED_HOST " | |
| "--env UV_INDEX_URL --env UV_EXTRA_INDEX_URL " | |
| "--env RULES_PYTHON_PIP_ISOLATED" in input_str | |
| ) |
A mirror that is not a drop-in replacement for PyPI needs more than the primary index: a secondary index for artifacts it does not carry, and, when it is reached over plain HTTP, an exemption from pip's and uv's refusal to talk to an insecure host. Forward those alongside the primary index rather than leaving a half-configured state that resolves from the mirror and then fails on the first package it does not serve. PIP_TRUSTED_HOST goes to the repository rules as well; UV_INSECURE_HOST is uv's spelling of the same exemption and only matters in the containers, where uv runs. Signed-off-by: elliot-barn <[email protected]>
## Description `files.pythonhosted.org` returns intermittent 502s (pypi/support#11895). Ray CI is hit disproportionately because a single job cold-fetches ~50 wheels concurrently, so it samples the fault dozens of times per job, and `rules_python` shells out to pip inside `whl_library`, where one 502 fails the repository rule and the build with it. **Retries do not fix this, which is why this PR contains none.** Measured on premerge build 72083: pip with `--retries=50` and a jittered 6-12s backoff exhausted on a single wheel with `too many 502 error responses`, while 47 of 62 *job-level* retries in the same build passed. urllib3 retries reuse the pooled connection and the already-resolved address, and anycast keeps every attempt on the same bad Fastly edge, so fifty in-process attempts are not fifty independent attempts. What fixes it is not asking that edge — i.e. a cache in front of PyPI, which fetches each `(package, version)` once and serves it from S3 afterwards. Four commits, in dependency order: 1. **Make the index an environment property.** Valueless `--repo_env` for `PIP_INDEX_URL`, `PIP_EXTRA_INDEX_URL`, `PIP_TRUSTED_HOST` and `RULES_PYTHON_PIP_ISOLATED` in `.bazelrc`, and those plus the `UV_*` equivalents forwarded into nested containers by `ci/ray_ci/container.py`. `RULES_PYTHON_PIP_ISOLATED` is load-bearing: `whl_library` runs pip with `--isolated`, which otherwise makes it ignore every `PIP_*` variable. The passthrough lives in the repo's `.bazelrc` rather than a generated one because every bazel invocation reads it, including the nested ones `ci/ray_ci:test_in_docker` spawns, which a `--config` flag on a step's command line never reaches. 2. **Stop the lock files overriding it.** 97 of the 99 files under `python/deplocks` opened with `--index-url https://pypi.org/simple`, and a requirements file's index URL beats both the environment and the command line. The `--extra-index-url` and `--find-links` entries for PyTorch, libtpu and PyG stay: those are not PyPI, so nothing else supplies them at install time. 3. **Resolve through the mirror when the job can reach it.** The forge image carries `ci/pypi_index_proxy.py` and a `profile.d` script that probes the mirror and picks a mode. ### Why a proxy and not just an index URL One `pip install` is two requests: the project page, governed by `--index-url`, and the artifact, governed by the URL written in that page's body. PyPI's simple index hands out **absolute** `files.pythonhosted.org` URLs, so pointing `--index-url` at a path-prefix byte cache mirrors the metadata request and leaves the multi-megabyte download on the origin that is returning the 502s. The proxy rewrites those file URLs so both hops transit the mirror. Wheels do not stream through the proxy process — clients fetch them straight from the mirror at the rewritten URLs — and the `#sha256=` fragments hash artifact content, not URLs, so hash verification is unaffected. ### Fail-open, deliberately The probe accepts a mode only after a real index fetch succeeds through it, and exports nothing otherwise, leaving pip and uv on public PyPI. That check hits the mirror rather than only the local process because `/healthz` never touches the mirror: a healthy proxy in front of an unreachable mirror answers every request with a 502, which is strictly worse than no proxy. Build 72083 exercised exactly that path — `mirror unreachable from this agent` — and the build continued. ## Related issues Upstream cause: pypi/support#11895. Not a duplicate: commits 1-2 are #65562 and commit 3 is #65563, opened as standalone reviewable units before the mirror existed. If reviewers prefer, land those two first and this rebases down to the single mirror commit. #65518 and #65538 raise pip/uv retry tolerance; they are complementary and deliberately excluded here, per the measurement above. ## Additional information **Depends on infrastructure:** the mirror is a per-VPC deployment in the Ray CI account, so `https://mirror.ci.ray.io` only resolves inside the Buildkite stack VPCs. `RAYCI_PYPI_MIRROR_URL` overrides the default for a fleet that runs its own. **Deliberately out of scope:** image builds still resolve from PyPI. The `pip` and `uv` invocations in `ci/docker/*.Dockerfile` run in a separate BuildKit context that cannot reach a proxy bound on the forge container's loopback, and a per-job address would change wanda's content-addressed cache key on every build and rebuild every image. That needs a build-reachable index, which is a follow-up. `ci/pypi_index_proxy.py` is vendored from a public gist, recorded at the top of the file along with the one local edit (the default mirror URL). ## Testing - `PYTHONPATH=.:release python -m pytest -q ci/ray_ci/test_linux_tester_container.py ci/ray_ci/test_container.py ci/ray_ci/test_tester.py ci/ray_ci/test_linux_container.py ci/ray_ci/test_windows_container.py` — 24 passed. - `bazel test //ci/raydepsets:test_cli` — passed, including `test_diff_lock_files_up_to_date`, the generated-equals-committed invariant for the deplock change. - Bazel 7.5.0: verified that a valueless `common --repo_env=NAME` reaches `rctx.os.environ` in a repository rule and reads unset when the variable is unset, and that `bazel info`/`query`/`version` do not reject the `common` lines. - `docker run --env NAME` with NAME unset leaves it absent in the container, not empty — which is what keeps the default path unchanged. - The mechanism as a whole ran on premerge 72083 (on a superset branch): the probe logged its verdict, fell back to PyPI because that fleet has no route to the mirror at the time, and the build proceeded. - `pre-commit run --files` over every changed file, shellcheck included. AI assistance (Claude Code) was used for this change. --------- Signed-off-by: elliot-barn <[email protected]> Signed-off-by: Elliot Barnwell <[email protected]>
## Description `files.pythonhosted.org` returns intermittent 502s (pypi/support#11895). Ray CI is hit disproportionately because a single job cold-fetches ~50 wheels concurrently, so it samples the fault dozens of times per job, and `rules_python` shells out to pip inside `whl_library`, where one 502 fails the repository rule and the build with it. **Retries do not fix this, which is why this PR contains none.** Measured on premerge build 72083: pip with `--retries=50` and a jittered 6-12s backoff exhausted on a single wheel with `too many 502 error responses`, while 47 of 62 *job-level* retries in the same build passed. urllib3 retries reuse the pooled connection and the already-resolved address, and anycast keeps every attempt on the same bad Fastly edge, so fifty in-process attempts are not fifty independent attempts. What fixes it is not asking that edge — i.e. a cache in front of PyPI, which fetches each `(package, version)` once and serves it from S3 afterwards. Four commits, in dependency order: 1. **Make the index an environment property.** Valueless `--repo_env` for `PIP_INDEX_URL`, `PIP_EXTRA_INDEX_URL`, `PIP_TRUSTED_HOST` and `RULES_PYTHON_PIP_ISOLATED` in `.bazelrc`, and those plus the `UV_*` equivalents forwarded into nested containers by `ci/ray_ci/container.py`. `RULES_PYTHON_PIP_ISOLATED` is load-bearing: `whl_library` runs pip with `--isolated`, which otherwise makes it ignore every `PIP_*` variable. The passthrough lives in the repo's `.bazelrc` rather than a generated one because every bazel invocation reads it, including the nested ones `ci/ray_ci:test_in_docker` spawns, which a `--config` flag on a step's command line never reaches. 2. **Stop the lock files overriding it.** 97 of the 99 files under `python/deplocks` opened with `--index-url https://pypi.org/simple`, and a requirements file's index URL beats both the environment and the command line. The `--extra-index-url` and `--find-links` entries for PyTorch, libtpu and PyG stay: those are not PyPI, so nothing else supplies them at install time. 3. **Resolve through the mirror when the job can reach it.** The forge image carries `ci/pypi_index_proxy.py` and a `profile.d` script that probes the mirror and picks a mode. ### Why a proxy and not just an index URL One `pip install` is two requests: the project page, governed by `--index-url`, and the artifact, governed by the URL written in that page's body. PyPI's simple index hands out **absolute** `files.pythonhosted.org` URLs, so pointing `--index-url` at a path-prefix byte cache mirrors the metadata request and leaves the multi-megabyte download on the origin that is returning the 502s. The proxy rewrites those file URLs so both hops transit the mirror. Wheels do not stream through the proxy process — clients fetch them straight from the mirror at the rewritten URLs — and the `#sha256=` fragments hash artifact content, not URLs, so hash verification is unaffected. ### Fail-open, deliberately The probe accepts a mode only after a real index fetch succeeds through it, and exports nothing otherwise, leaving pip and uv on public PyPI. That check hits the mirror rather than only the local process because `/healthz` never touches the mirror: a healthy proxy in front of an unreachable mirror answers every request with a 502, which is strictly worse than no proxy. Build 72083 exercised exactly that path — `mirror unreachable from this agent` — and the build continued. ## Related issues Upstream cause: pypi/support#11895. Not a duplicate: commits 1-2 are ray-project#65562 and commit 3 is ray-project#65563, opened as standalone reviewable units before the mirror existed. If reviewers prefer, land those two first and this rebases down to the single mirror commit. ray-project#65518 and ray-project#65538 raise pip/uv retry tolerance; they are complementary and deliberately excluded here, per the measurement above. ## Additional information **Depends on infrastructure:** the mirror is a per-VPC deployment in the Ray CI account, so `https://mirror.ci.ray.io` only resolves inside the Buildkite stack VPCs. `RAYCI_PYPI_MIRROR_URL` overrides the default for a fleet that runs its own. **Deliberately out of scope:** image builds still resolve from PyPI. The `pip` and `uv` invocations in `ci/docker/*.Dockerfile` run in a separate BuildKit context that cannot reach a proxy bound on the forge container's loopback, and a per-job address would change wanda's content-addressed cache key on every build and rebuild every image. That needs a build-reachable index, which is a follow-up. `ci/pypi_index_proxy.py` is vendored from a public gist, recorded at the top of the file along with the one local edit (the default mirror URL). ## Testing - `PYTHONPATH=.:release python -m pytest -q ci/ray_ci/test_linux_tester_container.py ci/ray_ci/test_container.py ci/ray_ci/test_tester.py ci/ray_ci/test_linux_container.py ci/ray_ci/test_windows_container.py` — 24 passed. - `bazel test //ci/raydepsets:test_cli` — passed, including `test_diff_lock_files_up_to_date`, the generated-equals-committed invariant for the deplock change. - Bazel 7.5.0: verified that a valueless `common --repo_env=NAME` reaches `rctx.os.environ` in a repository rule and reads unset when the variable is unset, and that `bazel info`/`query`/`version` do not reject the `common` lines. - `docker run --env NAME` with NAME unset leaves it absent in the container, not empty — which is what keeps the default path unchanged. - The mechanism as a whole ran on premerge 72083 (on a superset branch): the probe logged its verdict, fell back to PyPI because that fleet has no route to the mirror at the time, and the build proceeded. - `pre-commit run --files` over every changed file, shellcheck included. AI assistance (Claude Code) was used for this change. --------- Signed-off-by: elliot-barn <[email protected]> Signed-off-by: Elliot Barnwell <[email protected]>
Description
CI's Python package index is effectively hardwired to the default at every point
where it resolves packages, so pointing a fleet at a caching mirror instead of
pypi.orgcannot be done without patchingWORKSPACEand every step that runspip, uv or bazel. That matters while
files.pythonhosted.orgreturnsintermittent 502s (pypi/support#11895):
rules_pythonshells out to pip insidewhl_library, so one 502 fails the repository rule and takes the build with it.This makes the index a property of the environment. Two pieces are needed, and
neither pins a value:
.bazelrcforwardsPIP_INDEX_URLandRULES_PYTHON_PIP_ISOLATEDtorepository rules as valueless
--repo_env, so bazel inherits them from theinvoking environment. The second one is load-bearing:
rules_pythonrunswhl_library's pip with--isolated, which makes pip ignore everyPIP_*variable, and
pip_repository.bzlchecksRULES_PYTHON_PIP_ISOLATEDbeforeadding that flag. Without it,
PIP_INDEX_URLis silently discarded and pipresolves from PyPI while looking configured.
ci/ray_ci/container.pyforwardsPIP_INDEX_URL,UV_INDEX_URLandRULES_PYTHON_PIP_ISOLATEDinto the nested test containers, because the bazelinvocation in there needs the variables in its own environment for the
passthrough above to have anything to inherit.
Why the repo's
.bazelrcrather than a generated one. Every bazel invocationreads the workspace
.bazelrc, including the nested ones that a--configflagon a step's command line never reaches. Writing the same flags into a per-job
~/.bazelrclooks equivalent and is not: a container started from that step getsits image's copy of that file, so the flags are there for the outer invocation and
gone for the inner one, and the only symptom is pip going back to PyPI.
Nothing changes for a plain checkout. Both variables are unset, docker passes
nothing through for an unset
--env, bazel inherits nothing, and pip and uvresolve from PyPI exactly as they do today.
Related issues
Upstream cause: pypi/support#11895.
Complementary, no file overlap with either:
whl_library.Not a duplicate:
gh pr list --repo ray-project/ray --state open --search "PIP_INDEX_URL"/"repo_env in:body"/"pypi index mirror"return only thosetwo PRs of mine, and neither makes the index configurable.
Additional information
What this deliberately does not cover
pip installinvocations inci/docker/*.Dockerfilerunin a separate BuildKit context and are unaffected by a variable exported in a
step. They are also cache-key sensitive, so wiring them needs its own change.
raydepsetsgenerates with uv's--emit-index-url, so 97 of the 99 files underpython/deplockscarry--index-url https://pypi.org/simpleon line 1, and a requirements file'sembedded index URL beats both the environment and the command line. Dropping
just that line (the default index, so removing it changes nothing for anyone
installing normally) while keeping the
--extra-index-urland--find-linksentries for PyTorch, TPU and PyG intact is a follow-up.
Testing
Duplicate check as above. Every claim below was run, not assumed.
1. Unit tests.
2. Valueless
--repo_envreally reaches a repository rule (bazel 7.5.0, theversion in
.bazelversion). Scratch workspace with arepository_rulethatwrites
rctx.os.environto a file, and the twocommon --repo_envlines fromthis change in its
.bazelrc:3.
commonis safe for the commands that do not accept--repo_env— nounsupported-option error from
bazel info release,bazel query //...orbazel versionin that workspace.4. An unset variable is not forwarded as empty, which is what keeps the
default path unchanged:
5. Lint.
pre-commit run --files .bazelrc ci/ray_ci/container.py ci/ray_ci/test_linux_tester_container.py— all applicable hooks pass (ruff,black, semgrep, import order, docstyle).
AI assistance (Claude Code) was used for this change.