Skip to content

[core][runtime_env] Accept a py_executable with arguments on Windows - #68150

Open
amirebrahimi wants to merge 3 commits into
ray-project:masterfrom
amirebrahimi:fix-windows-py-executable-with-args
Open

amirebrahimi wants to merge 3 commits into
ray-project:masterfrom
amirebrahimi:fix-windows-py-executable-with-args

Conversation

@amirebrahimi

@amirebrahimi amirebrahimi commented Nov 13, 2026 •

Copy link
Copy Markdown

Description (🧑‍🦱-written)

I noticed that uv support 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.

  • Cause: POSIX puts py_executable unquoted into bash -c, but Windows' list form makes list2cmdline quote the whole command, so cmd.exe looks for a program named "uv run --locked python".
  • Fix: a value that contains whitespace and isn't an existing file goes into the command line as written. A real path with spaces stays quoted, so [RuntimeEnv] Fixes spaces in paths causing failures on Windows #25659's behaviour is kept.
  • Also affected: Ray's own uv run hook (uv_runtime_env_hook.py), which sets a py_executable with arguments.
  • Testing: new unit tests in tests/unit/test_runtime_env_context.py, and a manual run on a local Windows cluster. Workers failed with stock Ray and started through uv run --locked python with the fix. A job without py_executable still 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.

amirebrahimi and others added 2 commits November 13, 2026 15:47
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]>
@amirebrahimi
amirebrahimi requested a review from a team as a code owner November 13, 2026 23:56

@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 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.

Comment on lines +78 to +82
if (
len(executable) == 1
and any(c.isspace() for c in executable[0])
and not os.path.isfile(executable[0])
):

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.

high

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"))
    ):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

🤖 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.

Comment on lines +24 to +30
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'

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

Add a test case to verify that a non-existing executable path with spaces (but without arguments) is still correctly quoted on Windows.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If reviewers agree with the above response, then this test wouldn't be necessary.

@amirebrahimi amirebrahimi changed the title Fix windows py executable with args [core][runtime_env] Accept a py_executable with arguments on Windows Nov 14, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

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]>
@amirebrahimi

Copy link
Copy Markdown
Author

@edoakes or @martinlhw, would either of you be willing to review?

@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 Nov 28, 2026
@amirebrahimi

Copy link
Copy Markdown
Author

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.

(posting to keep this PR from going stale)
I've already joined the Slack channel, but haven't received a response back yet about PRs in general. I'll be coming to an office hours to see if I can get any traction on this.

This branch has not been deployed

No deployments
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 stale The issue is stale. It will be closed within 7 days unless there are further conversation windows

Development

Successfully merging this pull request may close these issues.

1 participant