Repository navigation
Conversation
`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]>
There was a problem hiding this comment.
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.
| 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"] |
There was a problem hiding this comment.
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"|
This pull request has been automatically marked as stale because it has not had 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. |
|
This pull request has been automatically closed because there has been no more activity in the 14 days 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! |
Pull request was closed
## 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]>
## 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]>
RAY_RUNTIME_ENV_CREATE_WORKING_DIRenv var was not being expanded in the uv plugin because uv doesn't natively support${VAR}syntax like pip does. Fix by callingos.path.expandvars()when writing requirements lines, so env vars are resolved before any package manager reads the file.Fixes #59343
Description
Related issues
Additional information