Repository navigation
[core][runtime_env] Accept a py_executable with arguments on Windows - #68150
amirebrahimi wants to merge 3 commits into
Conversation
On POSIX, exec_worker puts py_executable unquoted into `bash -c`, so a value with arguments such as `uv run --locked python` works. On Windows it passes a list to `subprocess.Popen(..., shell=True)`, and list2cmdline quotes any item that contains whitespace, so cmd.exe looks for a program literally named `uv run --locked python`. build_windows_worker_command inserts such an executable into the command line as written, matching POSIX. A path that contains spaces and exists, such as `C:\Program Files\Python\python.exe`, stays one quoted token. The command prefix and the worker's arguments are still quoted. This commit adds the helper and its tests; the next one uses it in exec_worker. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Amir Ebrahimi <[email protected]>
exec_worker now builds the Windows worker command with
build_windows_worker_command, so a py_executable such as
`uv run --locked python` starts workers on Windows as it already does on
POSIX. Before this change, cmd.exe failed with:
'"uv run --locked python"' is not recognized as an internal or
external command, operable program or batch file.
The workers never registered. This also affects Ray's `uv run` runtime
env hook, which sets py_executable to a `uv run ...` command.
A py_executable without whitespace, or one that is an existing file,
still goes to Popen as before.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Amir Ebrahimi <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces the build_windows_worker_command helper to properly construct worker execution commands on Windows, ensuring that executable commands with arguments (like uv run) are not incorrectly quoted while actual executable paths with spaces are preserved. It also adds comprehensive unit tests for this behavior. The review feedback highlights a potential issue where non-existent executable paths with spaces (e.g., during dry runs) might be incorrectly treated as commands with arguments and suggests checking for common Windows executable extensions to prevent execution failures. Additionally, it recommends adding a test case to verify the quoting behavior of non-existent executable paths with spaces.
| if ( | ||
| len(executable) == 1 | ||
| and any(c.isspace() for c in executable[0]) | ||
| and not os.path.isfile(executable[0]) | ||
| ): |
There was a problem hiding this comment.
If the executable path contains spaces but does not exist on the host (e.g., during a dry run, testing, or before the environment is fully set up), os.path.isfile(executable[0]) will return False. This causes the path to be treated as a command with arguments, meaning it won't be quoted. When executed via cmd.exe, this will fail with an error like 'C:\Program' is not recognized as an internal or external command....
To prevent this, we should check if the executable string ends with a common Windows executable extension (like .exe, .bat, .cmd, etc.). If it does, we should treat it as a path and keep it quoted, even if the file does not currently exist.
if (
len(executable) == 1
and any(c.isspace() for c in executable[0])
and not os.path.isfile(executable[0])
and not executable[0].lower().endswith((".exe", ".bat", ".cmd", ".com", ".ps1"))
):There was a problem hiding this comment.
🤖 The suggested fix would also break valid commands. A py_executable that doesn't exist fails either way; only the error text changes. Keeping anything ending in .exe as one token would break commands like uv run ... python.exe, which is the case this PR fixes. The same value on POSIX goes unquoted too.
| def test_existing_path_with_spaces_is_quoted(tmp_path): | ||
| exe = tmp_path / "dir with space" / "python.exe" | ||
| exe.parent.mkdir() | ||
| exe.touch() | ||
| cmd = build_windows_worker_command([], [str(exe)], ["worker.py"]) | ||
| assert cmd == [str(exe), "worker.py"] | ||
| assert subprocess.list2cmdline(cmd) == f'"{exe}" worker.py' |
There was a problem hiding this comment.
Add a test case to verify that a non-existing executable path with spaces (but without arguments) is still correctly quoted on Windows.
| def test_existing_path_with_spaces_is_quoted(tmp_path): | |
| exe = tmp_path / "dir with space" / "python.exe" | |
| exe.parent.mkdir() | |
| exe.touch() | |
| cmd = build_windows_worker_command([], [str(exe)], ["worker.py"]) | |
| assert cmd == [str(exe), "worker.py"] | |
| assert subprocess.list2cmdline(cmd) == f'"{exe}" worker.py' | |
| def test_existing_path_with_spaces_is_quoted(tmp_path): | |
| exe = tmp_path / "dir with space" / "python.exe" | |
| exe.parent.mkdir() | |
| exe.touch() | |
| cmd = build_windows_worker_command([], [str(exe)], ["worker.py"]) | |
| assert cmd == [str(exe), "worker.py"] | |
| assert subprocess.list2cmdline(cmd) == f'" {exe}" worker.py' | |
| def test_non_existing_path_with_spaces_is_quoted(): | |
| cmd = build_windows_worker_command([], ["C:\\missing dir\\python.exe"], ["worker.py"]) | |
| assert cmd == ["C:\\missing dir\\python.exe", "worker.py"] | |
| assert subprocess.list2cmdline(cmd) == '"C:\\missing dir\\python.exe" worker.py' |
There was a problem hiding this comment.
If reviewers agree with the above response, then this test wouldn't be necessary.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 757c9c1. Configure here.
…n Windows A py_executable that contains whitespace and isn't an existing file goes into the Windows command line as written, as it does on POSIX. So a path with spaces followed by arguments works when the user quotes the path, e.g. `"C:\Program Files\Python\python.exe" -X utf8`. Before this change, that value was quoted again as a whole and failed. One test checks the command line that is built. A Windows-only test runs it through cmd.exe with a script in a directory with spaces, and checks the arguments the script receives. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Amir Ebrahimi <[email protected]>
|
@edoakes or @martinlhw, would either of you be willing to review? |
|
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. |
(posting to keep this PR from going stale) |

Description (🧑🦱-written)
I noticed that
uvsupport on Windows doesn't work the same as Linux. I wanted to be able to use the uv + ray integration mentioned in this blogpost. I had started to devise a workaround, but then decided to issue this PR instead.py_executableunquoted intobash -c, but Windows' list form makeslist2cmdlinequote the whole command, so cmd.exe looks for a program named"uv run --locked python".uv runhook (uv_runtime_env_hook.py), which sets apy_executablewith arguments.tests/unit/test_runtime_env_context.py, and a manual run on a local Windows cluster. Workers failed with stock Ray and started throughuv run --locked pythonwith the fix. A job withoutpy_executablestill used the default interpreter.Related issues
Related to #50961. See this comment and this comment.
Additional information
Work was completed by Claude Opus 5.5 🤖 under supervised planning.