Skip to content

[ci] Let the environment choose the Python package index - #65562

Closed
elliot-barn wants to merge 2 commits into
masterfrom
elliot-barn/pip-index-env-passthrough
Closed

elliot-barn wants to merge 2 commits into
masterfrom
elliot-barn/pip-index-env-passthrough

Conversation

@elliot-barn

Copy link
Copy Markdown
Collaborator

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.org cannot be done without patching WORKSPACE and every step that runs
pip, uv or bazel. That matters while files.pythonhosted.org returns
intermittent 502s (pypi/support#11895): rules_python shells out to pip inside
whl_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:

  • .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. The second one is load-bearing: rules_python runs
    whl_library's pip with --isolated, which makes pip ignore every PIP_*
    variable, and pip_repository.bzl checks RULES_PYTHON_PIP_ISOLATED before
    adding that flag. Without it, PIP_INDEX_URL is silently discarded and pip
    resolves from PyPI while looking configured.
  • ci/ray_ci/container.py forwards PIP_INDEX_URL, UV_INDEX_URL and
    RULES_PYTHON_PIP_ISOLATED into the nested test containers, because the bazel
    invocation in there needs the variables in its own environment for the
    passthrough above to have anything to inherit.

Why the repo's .bazelrc rather than a generated one. Every bazel invocation
reads the workspace .bazelrc, including the nested ones that a --config flag
on a step's command line never reaches. Writing the same flags into a per-job
~/.bazelrc looks equivalent and is not: a container started from that step gets
its 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 uv
resolve from PyPI exactly as they do today.

Related issues

Upstream cause: pypi/support#11895.

Complementary, no file overlap with either:

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 those
two PRs of mine, and neither makes the index configurable.

Additional information

What this deliberately does not cover

  • Image builds. The pip install invocations in ci/docker/*.Dockerfile run
    in 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.
  • The committed lock files. raydepsets generates with uv's
    --emit-index-url, so 97 of the 99 files under python/deplocks carry
    --index-url https://pypi.org/simple on line 1, and a requirements file's
    embedded 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-url and --find-links
    entries 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.

$ PYTHONPATH=.:release python -m pytest -q ci/ray_ci/test_linux_tester_container.py \
    ci/ray_ci/test_container.py ci/ray_ci/test_windows_container.py \
    ci/ray_ci/test_linux_container.py
15 passed in 1.23s

2. Valueless --repo_env really reaches a repository rule (bazel 7.5.0, the
version in .bazelversion). Scratch workspace with a repository_rule that
writes rctx.os.environ to a file, and the two common --repo_env lines from
this change in its .bazelrc:

$ PIP_INDEX_URL=http://127.0.0.1:35999/simple RULES_PYTHON_PIP_ISOLATED=0 bazel build @probe//:out.txt
PIP_INDEX_URL=http://127.0.0.1:35999/simple RULES_PYTHON_PIP_ISOLATED=0

$ bazel clean --expunge && bazel build @probe//:out.txt      # both variables unset
PIP_INDEX_URL=<unset> RULES_PYTHON_PIP_ISOLATED=<unset>

3. common is safe for the commands that do not accept --repo_env — no
unsupported-option error from bazel info release, bazel query //... or
bazel version in that workspace.

4. An unset variable is not forwarded as empty, which is what keeps the
default path unchanged:

$ docker run --rm --env FOO_UNSET_PROBE --env BAR_SET_PROBE=x alpine:latest \
    sh -c 'echo "FOO present: ${FOO_UNSET_PROBE+yes}${FOO_UNSET_PROBE-no}"; echo "BAR=$BAR_SET_PROBE"'
FOO present: no
BAR=x

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.

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]>
@elliot-barn
elliot-barn requested a review from a team as a code owner August 18, 2026 16:40

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread .bazelrc
Comment on lines +21 to +22
common --repo_env=PIP_INDEX_URL
common --repo_env=RULES_PYTHON_PIP_ISOLATED

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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

Comment thread ci/ray_ci/container.py
Comment on lines +52 to +54
"PIP_INDEX_URL",
"UV_INDEX_URL",
"RULES_PYTHON_PIP_ISOLATED",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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",

Comment on lines +104 to +107
assert (
"--env PIP_INDEX_URL --env UV_INDEX_URL "
"--env RULES_PYTHON_PIP_ISOLATED" in input_str
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update the test assertion to include the newly forwarded environment variables (PIP_EXTRA_INDEX_URL, PIP_TRUSTED_HOST, and UV_EXTRA_INDEX_URL).

Suggested change
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
)

@elliot-barn elliot-barn added go add ONLY when ready to merge, run all tests actor scalability and removed actor scalability labels Aug 18, 2026
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]>
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core devprod labels Aug 18, 2026
elliot-barn added a commit that referenced this pull request Aug 19, 2026
## 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]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
## 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core devprod go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant