Skip to content

Make _uv_safe_path space-safe on macOS/Linux (#6503) - #6534

Merged
danielhanchen merged 9 commits into
unslothai:mainfrom
GodlyDonuts:fix-uv-safe-path-spaces
Jun 24, 2026
Merged

danielhanchen merged 9 commits into
unslothai:mainfrom
GodlyDonuts:fix-uv-safe-path-spaces

Conversation

@GodlyDonuts

Copy link
Copy Markdown
Contributor

Summary

Addresses the macOS/Linux half of #6503: unsloth studio setup fails when the repo is cloned under a path containing a space (e.g. /Users/me/Documents/Open Source/unsloth):

error: File not found: `/Users/me/Documents/Open`

Root cause

uv 0.11.x truncates a constraints/requirements path passed via -c / -r at the first space. install_python_stack.py already has a helper for exactly this — _uv_safe_path() — but it only handles Windows (resolving to the 8.3 short path). On macOS/Linux it returned the space-containing path unchanged, so -c <CONSTRAINTS> / -r <reqs> still broke. (The issue reporter pinpointed this exact function and proposed this fix.)

Fix

Extend _uv_safe_path() to copy the (small, flat) constraints/requirements file into a space-free temp dir and hand uv the copy, since macOS/Linux have no 8.3-short-path equivalent. The temp dirs are removed at process exit via atexit. It falls back to the original path on any error (missing file, TMPDIR itself containing a space, copy failure), so it's never worse than the prior Windows-only behaviour. The Windows path is unchanged.

This is the single chokepoint for all three -c/-r call sites in install_python_stack.py, so the Studio-setup stage (incl. unsloth studio update) now works from a space-containing path on every OS.

Scope / what this does NOT cover

This fixes the install_python_stack.py (Studio setup) stage. The reporter also hit an earlier failure in install.sh's PyTorch step, which is a separate uv cwd-project-discovery issue (the command itself is space-clean; uv mishandles the space-containing working directory). That belongs in install.sh and is left for a follow-up so this change stays small and reviewable.

Tests

tests/python/test_install_python_stack.py::TestUvSafePath (runs on the existing GPU-free harness):

  • no-space path is returned unchanged (all platforms);
  • a space-containing POSIX path yields a space-free copy with identical contents;
  • a missing file falls back to the original path instead of raising.

All 7 tests in the file pass locally.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@GodlyDonuts
GodlyDonuts force-pushed the fix-uv-safe-path-spaces branch from e697b06 to e4c6cda Compare June 21, 2026 07:27
uv 0.11.x truncates a constraints/requirements path passed via `-c`/`-r`
at the first space, so `unsloth studio` setup from a repo cloned under a
path containing a space (e.g. `/Users/me/Open Source/unsloth`) fails with:

    error: File not found: `/Users/me/Open`

_uv_safe_path() already worked around this on Windows via the 8.3 short
path but returned the space-containing path unchanged on macOS/Linux,
which have no 8.3 equivalent. Extend it to copy the (small, flat)
constraints/requirements file into a space-free temp dir and hand uv the
copy; the temp dirs are removed at process exit. Falls back to the
original path on any error, so it is never worse than before.

Refs unslothai#6503
@GodlyDonuts
GodlyDonuts force-pushed the fix-uv-safe-path-spaces branch from e4c6cda to 9296bef Compare June 21, 2026 07:30
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 5a3caa0c24

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 5a3caa0c24

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…hai#6503)

The -c/-r fix did not cover UV_OVERRIDE, which uv also truncates at the first
space. On Apple Silicon the overrides file is handed to uv via UV_OVERRIDE at
install time (install_python_stack.py) and during the MLX self-heal
(utils.mlx_repair), so a repo under a path containing a space still broke every
uv call there. Move _uv_safe_path into backend.utils.uv_path_safety so both
sites share it, and route UV_OVERRIDE through it.

Also stop leaking the temp dir when shutil.copyfile fails after mkdtemp, and add
tests for the UV_OVERRIDE channel, the TMPDIR-with-space fallback, the atexit
cleanup, and the no-leak path.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member

Pushed a follow-up commit onto this branch to round out the fix.

The -c/-r change here is correct, but uv truncates UV_OVERRIDE at the first space in exactly the same way. On Apple Silicon the overrides file is handed to uv via UV_OVERRIDE both at install time (install_python_stack.py) and during the MLX self-heal (utils/mlx_repair.py), so a checkout under a path containing a space (the case in #6503) would still break every uv call on those paths.

What the follow-up commit does:

  • Moves _uv_safe_path into studio/backend/utils/uv_path_safety.py so the installer and mlx_repair share one implementation, and routes UV_OVERRIDE through it at both sites.
  • Fixes a temp-dir leak: when shutil.copyfile failed after mkdtemp, the empty temp dir was never registered for cleanup. It is now removed on the error path.
  • Adds tests for the UV_OVERRIDE channel, the TMPDIR-with-a-space fallback, the atexit cleanup, and the no-leak path.

Verified on Linux and on real macOS arm64 and Windows runners: the install_python_stack and mlx_repair suites pass, and a repo under a space-containing path now installs cleanly on every OS. uv 0.5 through 0.11 all show the same -c/UV_OVERRIDE truncation, and the fix holds across them. -r does not truncate in any tested version, so wrapping it is harmless but not strictly required.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 3accaad110

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 9d53656 into unslothai:main Jun 24, 2026
36 of 37 checks passed
@danielhanchen

Copy link
Copy Markdown
Member

Thanks!

danielhanchen added a commit that referenced this pull request Jun 25, 2026
* Installer: make UV_OVERRIDE space-safe on Apple Silicon (#6503)

On Apple Silicon, install.sh exports UV_OVERRIDE pointing at the bundled
overrides-darwin-arm64.txt. uv splits UV_OVERRIDE on whitespace, so a repo
cloned under a path containing a space (e.g. /Users/me/Open Source/unsloth)
truncates the value and every later uv call aborts with
'error: File not found: <truncated>' (the PyTorch install step in #6503).

Copy the overrides file into a space-free temp dir and point uv at the copy
when the path contains a space, mirroring the macOS/Linux handling already
merged for the Python installer in #6534. The temp dir is removed in the
exit trap, and the code falls back to the original path when no space-free
temp dir is available, so the no-space and non-macOS paths are unchanged.

Adds tests/sh/test_install_uv_override_space.sh, which extracts and runs the
install.sh hardening block and checks the spaced, no-space, and
spaced-TMPDIR fallback cases.

* Installer: match all whitespace (not just spaces) in UV_OVERRIDE handling

uv splits UV_OVERRIDE on any whitespace, so use the POSIX class
*[[:space:]]* rather than a literal space in install.sh (catches tabs and
newlines in the path too) and the matching test assertions. Use the portable
awk bracket expression [$] instead of \$ in the extraction so the test runs
the same under BSD awk (macOS) and GNU awk (Linux). Adds a tab-in-path case.

* Installer: clear _UV_OVERRIDE_TMPDIR before the exit trap

The exit trap rm -rf's _UV_OVERRIDE_TMPDIR. Initialize it to empty before
registering the trap so an inherited environment value can never be removed;
only a temp dir this script creates (Apple Silicon, spaced path) is cleaned.
Adds a structural test asserting the init precedes the trap.

* Run the install.sh UV_OVERRIDE space test in CI via a pytest wrapper

The Shell installer tests job uses a fixed script list (not tests/run_all.sh),
so the new shell test would not run on PRs. Add a pytest wrapper under
tests/python/ that invokes it; the auto-discovered repo CPU test job collects
tests/python/ and so executes the Apple Silicon spaced-path regression.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants