Skip to content

[Runtime Env] Expand env vars in gen_requirements_txt for uv support - #62183

Closed
summaryzb wants to merge 1 commit into
ray-project:masterfrom
summaryzb:fix-runtime-env-expandvars-uv
Closed

summaryzb wants to merge 1 commit into
ray-project:masterfrom
summaryzb:fix-runtime-env-expandvars-uv

Conversation

@summaryzb

Copy link
Copy Markdown
Contributor

RAY_RUNTIME_ENV_CREATE_WORKING_DIR env var was not being expanded in the uv plugin because uv doesn't natively support ${VAR} syntax like pip does. Fix by calling os.path.expandvars() when writing requirements lines, so env vars are resolved before any package manager reads the file.

Fixes #59343

Thank you for contributing to Ray! 🚀
Please review the Ray Contribution Guide before opening a pull request.

⚠️ Remove these instructions before submitting your PR.

💡 Tip: Mark as draft if you want early feedback, or ready for review when it's complete.

Description

Briefly describe what this PR accomplishes and why it's needed.

Related issues

Link related issues: "Fixes #1234", "Closes #1234", or "Related to #1234".

Additional information

Optional: Add implementation details, API changes, usage examples, screenshots, etc.

`RAY_RUNTIME_ENV_CREATE_WORKING_DIR` env var was not being expanded in the
uv plugin because uv doesn't natively support `${VAR}` syntax like pip does.
Fix by calling `os.path.expandvars()` when writing requirements lines, so
env vars are resolved before any package manager reads the file.

Fixes ray-project#59343

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Signed-off-by: summaryzb <[email protected]>
@summaryzb
summaryzb requested a review from a team as a code owner March 30, 2026 08:58

@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 enables environment variable expansion in requirements files by applying os.path.expandvars to each line in gen_requirements_txt. It includes new tests to verify this behavior and its application within the uv runtime environment. Feedback suggests using the monkeypatch fixture in tests for more idiomatic environment variable management.

Comment on lines +248 to +262
def test_gen_requirements_txt_expands_env_vars(tmp_path):
"""Unit test: gen_requirements_txt expands ${VAR} in package lines."""
os.environ["_TEST_EXPAND_VAR"] = "/some/path"
try:
req_file = str(tmp_path / "req.txt")
gen_requirements_txt(
req_file,
["-r ${_TEST_EXPAND_VAR}/requirements.txt", "numpy"],
)
with open(req_file) as f:
lines = f.read().strip().split("\n")
assert lines[0] == "-r /some/path/requirements.txt"
assert lines[1] == "numpy"
finally:
del os.environ["_TEST_EXPAND_VAR"]

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

While using try...finally to manage environment variables works, it's more idiomatic and safer in pytest to use the monkeypatch fixture. It automatically handles setup and teardown of the environment variable, even if the test fails, making the test cleaner and more robust against state leakage.

def test_gen_requirements_txt_expands_env_vars(tmp_path, monkeypatch):
    """Unit test: gen_requirements_txt expands ${VAR} in package lines."""
    monkeypatch.setenv("_TEST_EXPAND_VAR", "/some/path")
    req_file = str(tmp_path / "req.txt")
    gen_requirements_txt(
        req_file,
        ["-r ${_TEST_EXPAND_VAR}/requirements.txt", "numpy"],
    )
    with open(req_file) as f:
        lines = f.read().strip().split("\n")
    assert lines[0] == "-r /some/path/requirements.txt"
    assert lines[1] == "numpy"

@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Mar 30, 2026
@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Mar 30, 2026
@edoakes
edoakes enabled auto-merge (squash) March 30, 2026 21:26
@edoakes
edoakes disabled auto-merge March 30, 2026 21:26
@edoakes
edoakes enabled auto-merge (squash) March 30, 2026 21:26
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Apr 14, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no more activity in the 14 days
since being marked stale.

Please feel free to reopen or open a new pull request if you'd still like this to be addressed.

Again, you can always ask for help on our discussion forum or Ray's public slack channel.

Thanks again for your contribution!

@github-actions github-actions Bot closed this Apr 28, 2026
auto-merge was automatically disabled April 28, 2026 01:08

Pull request was closed

edoakes pushed a commit that referenced this pull request Sep 4, 2026
## Why are these changes needed?

`RAY_RUNTIME_ENV_CREATE_WORKING_DIR` is available while a runtime
environment is
being created, but uv package entries and `uv_pip_install_options` were
written
and passed through literally. This made working-directory-relative
requirements
fail for uv runtime environments.

Expand shell-style `$VAR` and `${VAR}` references using the per-install
environment passed to uv. Using that snapshot keeps requirements-file
contents,
CLI options, and the uv subprocess environment consistent without
consulting
mutable process-global environment state after async setup.

The existing unit test also now uses a pytest-scoped target directory
instead of
the global `/tmp` directory.

Fixes #59343.

## Duplicate-work check

I searched open PRs by issue number and semantic keywords and found no
open
implementation. #62183 was approved but closed unmerged as stale, and
the bug is
still present on current `master` at
`7bbef5a7de29c97a44e682e21fa0d4f759f92937`. Other historical attempts
are also
closed.

## Checks

- Exact-master regression harness: failed before the patch, passed after
it.
- `bazel test //python/ray/tests/unit:test_runtime_env_uv
--test_output=errors`
  - passed
- `python -m pytest -q
python/ray/tests/test_runtime_env_uv.py::test_package_install_from_working_dir_requirements
-s`
  - `1 passed in 182.56s`
- Ray pre-commit ruff hooks, black, `py_compile`, and `git diff --check`
  - passed

The focused integration test used a Ray 2.58.0 wheel with the exact
patched
`uv.py` overlaid; the current-master source itself was covered by the
exact-master
harness and Bazel unit target.

## AI assistance disclosure

AI assistance was used to research, implement, and test this change. I
reviewed
every changed line, understand the implementation and its limitations,
and
confirmed the test evidence above before submission.

---------

Signed-off-by: Wang Zupeng <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
## Why are these changes needed?

`RAY_RUNTIME_ENV_CREATE_WORKING_DIR` is available while a runtime
environment is
being created, but uv package entries and `uv_pip_install_options` were
written
and passed through literally. This made working-directory-relative
requirements
fail for uv runtime environments.

Expand shell-style `$VAR` and `${VAR}` references using the per-install
environment passed to uv. Using that snapshot keeps requirements-file
contents,
CLI options, and the uv subprocess environment consistent without
consulting
mutable process-global environment state after async setup.

The existing unit test also now uses a pytest-scoped target directory
instead of
the global `/tmp` directory.

Fixes ray-project#59343.

## Duplicate-work check

I searched open PRs by issue number and semantic keywords and found no
open
implementation. ray-project#62183 was approved but closed unmerged as stale, and
the bug is
still present on current `master` at
`7bbef5a7de29c97a44e682e21fa0d4f759f92937`. Other historical attempts
are also
closed.

## Checks

- Exact-master regression harness: failed before the patch, passed after
it.
- `bazel test //python/ray/tests/unit:test_runtime_env_uv
--test_output=errors`
  - passed
- `python -m pytest -q
python/ray/tests/test_runtime_env_uv.py::test_package_install_from_working_dir_requirements
-s`
  - `1 passed in 182.56s`
- Ray pre-commit ruff hooks, black, `py_compile`, and `git diff --check`
  - passed

The focused integration test used a Ray 2.58.0 wheel with the exact
patched
`uv.py` overlaid; the current-master source itself was covered by the
exact-master
harness and Bazel unit target.

## AI assistance disclosure

AI assistance was used to research, implement, and test this change. I
reviewed
every changed line, understand the implementation and its limitations,
and
confirmed the test evidence above before submission.

---------

Signed-off-by: Wang Zupeng <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests stale The issue is stale. It will be closed within 7 days unless there are further conversation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RuntimeEnv] RAY_RUNTIME_ENV_CREATE_WORKING_DIR isn't propagated when using 'ray job submit' Ray fails to serialize self-reference objects

2 participants