unsloth/studio/backend/utils/uv_path_safety.py
Saicharan Ramineni 9d53656614
Make _uv_safe_path space-safe on macOS/Linux (#6503) (#6534)
* Copy uv `-c`/`-r` paths to a space-free temp dir on macOS/Linux

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/unsloth#6503

* Route UV_OVERRIDE through _uv_safe_path and fix temp-dir leak (#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.

---------

Co-authored-by: danielhanchen <danielhanchen@gmail.com>
2026-06-24 04:02:24 -07:00

66 lines
2.1 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Hand uv a space-free `-c`/`--override`/`-r` file path (issue #6503).
uv splits `-c`/`--override` (and UV_OVERRIDE) on whitespace, so a path with a
space truncates. Windows uses the 8.3 short form; POSIX copies the file into a
space-free temp dir (removed at exit). Falls back to the original path on error.
Shared by install_python_stack and utils.mlx_repair.
"""
from __future__ import annotations
import atexit
import os
import platform
import shutil
import tempfile
IS_WINDOWS = platform.system() == "Windows"
_UV_SAFE_PATH_TMPDIRS: list[str] = []
@atexit.register
def _cleanup_uv_safe_path_tmpdirs() -> None:
while _UV_SAFE_PATH_TMPDIRS:
shutil.rmtree(_UV_SAFE_PATH_TMPDIRS.pop(), ignore_errors = True)
def uv_safe_path(path: object) -> str:
s = str(path)
if " " not in s:
return s
if IS_WINDOWS:
try:
import ctypes
from ctypes import wintypes
get_short = ctypes.windll.kernel32.GetShortPathNameW
get_short.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
get_short.restype = wintypes.DWORD
buf = ctypes.create_unicode_buffer(32768)
rc = get_short(s, buf, 32768)
if 0 < rc < 32768 and " " not in buf.value:
return buf.value
except Exception:
pass
return s
tmp_dir = None
try:
if not os.path.isfile(s):
return s
tmp_dir = tempfile.mkdtemp(prefix = "unsloth_uv_")
if " " in tmp_dir: # e.g. TMPDIR itself has a space
shutil.rmtree(tmp_dir, ignore_errors = True)
return s
dst = os.path.join(tmp_dir, (os.path.basename(s) or "uv_args.txt").replace(" ", "_"))
shutil.copyfile(s, dst)
_UV_SAFE_PATH_TMPDIRS.append(tmp_dir)
tmp_dir = None
return dst
except Exception:
if tmp_dir is not None: # don't leak the temp dir if the copy failed
shutil.rmtree(tmp_dir, ignore_errors = True)
return s