Skip to content

Windows: keep uv cache under the Studio root - #9007

Merged
danielhanchen merged 1 commit into
unslothai:mainfrom
Max-Reisinger:fix/windows-installer-uv-cache
Oct 5, 2026
Merged

danielhanchen merged 1 commit into
unslothai:mainfrom
Max-Reisinger:fix/windows-installer-uv-cache

Conversation

@Max-Reisinger

Copy link
Copy Markdown
Contributor

Changes

  • Configure the Windows installer to use StudioHome/cache/uv when UV_CACHE_DIR is not explicitly set.
  • Move a conflicting regular file at the managed uv cache path aside before creating the directory.
  • Preserve an explicitly configured UV_CACHE_DIR.
  • Add Windows PowerShell regression coverage for the cache-file collision.

Why

The Windows installer currently lets uv use its user-wide default cache under LOCALAPPDATA. If that path is a regular file, uv venv fails with Windows error 183 before the managed Studio environment can be created.

Keeping the default uv cache inside the existing Studio cache tree avoids the broken user-wide path and matches the cache location used by the Studio runtime.

Testing

  • pytest tests/python/test_windows_python_venv_hardening.py -q: 1 passed, 14 skipped on macOS because PowerShell is unavailable
  • ruff check tests/python/test_windows_python_venv_hardening.py: passed
  • project Python formatter: passed
  • git diff --check: passed
  • source-order contract confirms cache setup and recovery precede uv venv
  • Windows PowerShell behavior test will run on Windows CI

Scope

This PR fixes the uv cache collision reported in the issue. It does not change the Tauri application install location, redirect all Studio data to that location, or change Python discovery.

Fixes #8991

Checklist

  • The PR contains only the Windows installer fix and its regression test.
  • No unrelated formatting or refactoring is included.
  • Explicit UV_CACHE_DIR configuration remains unchanged.
  • Available local checks pass.
  • Windows PowerShell CI passes.

@Max-Reisinger
Max-Reisinger marked this pull request as ready for review August 16, 2026 15:01

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7fead1577b

ℹ️ 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".

Comment thread install.ps1 Outdated
Comment on lines +3229 to +3230
if ([string]::IsNullOrWhiteSpace($env:UV_CACHE_DIR)) {
$env:UV_CACHE_DIR = Join-Path (Join-Path $StudioHome "cache") "uv"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restore the installer-owned cache value after each run

When the installer is invoked twice in the same PowerShell session with UV_CACHE_DIR initially unset, this assignment remains in the process environment after the first invocation. If the user then selects a different UNSLOTH_STUDIO_HOME, the second invocation mistakes the first run's generated path for an explicit override and continues using the old root; this can cross-contaminate installations or make recovery fail when the old root was removed or became inaccessible. Preserve and restore the caller's original variable, or otherwise distinguish installer-generated values from user overrides.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Real leak, and it is fixed in 3000939. install.ps1 now captures UV_CACHE_DIR at line 2431, before the outer try, and the matching finally at 5769 either restores the caller's value or removes the variable, so a second install in the same session re-derives the path from its own UNSLOTH_STUDIO_HOME instead of inheriting the first root. Both Exit-InstallFailure paths (throw, and exit under --tauri) unwind through that finally. Covered by test_uv_cache_two_runs_in_one_session_use_their_own_studio_root.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3000939d69

ℹ️ 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".

Comment thread install.ps1 Outdated
$env:UV_CACHE_DIR = Join-Path (Join-Path $StudioHome "cache") "uv"
if (Test-Path -LiteralPath $env:UV_CACHE_DIR -PathType Leaf) {
$invalidUvCache = "$($env:UV_CACHE_DIR).invalid.$(Get-Date -Format 'yyyyMMddHHmmss').$PID"
Move-Item -LiteralPath $env:UV_CACHE_DIR -Destination $invalidUvCache

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Defer cache-file moves until workspace ownership is validated

When UNSLOTH_STUDIO_HOME points to a user workspace containing an unrelated unsloth_studio\Scripts\python.exe, the ownership guard later rejects the installation, but this earlier operation has already renamed any existing cache\uv file. This mutates an unowned workspace even though the installer refuses to proceed, so perform the cache collision recovery only after the existing ownership check has accepted the root.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Correct, and fixed in 3529e3a. The cache block now sits at install.ps1:3585, after the whole existing-venv branch, so the ownership rejection runs first. I traced the reject path: the throw at 3530 is inside the try opened at 3511, whose only handler is a finally at 5633 with no catch, so it propagates out of Install-UnslothStudio and line 3589 is never reached for a workspace the installer refuses. Covered by test_uv_cache_recovery_follows_existing_venv_ownership_guard.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 2d7de976c9

ℹ️ 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".

@Max-Reisinger

Copy link
Copy Markdown
Contributor Author

Hi @danielhanchen, this PR is ready for another review. Both previous Codex findings have been addressed, and the latest Codex review found no major issues on commit 2d7de97.

The GitHub Actions runs are currently waiting for approval (action_required). Could you please approve the workflows and review the latest revision when convenient? The main remaining validation is the Windows PowerShell regression coverage.

@oobabooga

Copy link
Copy Markdown
Member

Approved the runs that gate this change: Cross-platform parity, Clean machine install, Interrupted install recovery, Windows Application Control CI, Lint CI, and Workflow trigger lint. The remaining Windows smoke suites, Backend CI, Core and Startup profile were queued by broad path filters and do not exercise this diff, so I left those unapproved.

I checked both Codex findings against head rather than reading the follow-up commit titles. The restore in 3000939 sits in the finally that pairs with the try at install.ps1:2433, so it covers both Exit-InstallFailure paths out of Install-UnslothStudio. For 3529e3a, the ownership rejection at 3530 is inside the try opened at 3511 whose only handler is a finally with no catch, so a workspace the installer refuses never reaches the cache move at 3589. I also confirmed the deferral does not reopen #8991: between the uv install and the new block the only uv calls are the --version probes, and the first cache-consuming call is uv venv at 3602.

Worth recording since the PR description asserts it: $StudioHome\cache\uv is the same path storage_roots.cache_root() hands the runtime, and it puts the cache on the venv's filesystem, which is what uv's docs recommend for hardlinking. Locally pytest tests/python/test_windows_python_venv_hardening.py -q gives 3 passed and 16 skipped for lack of PowerShell, so the approved runs are what will actually exercise the new coverage.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: f71f6daf2b

ℹ️ 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".

@oobabooga

oobabooga commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

While this PR addresses the particular error message in #8991 by renaming the file that existed with the same name as the uv cache folder for whatever reason, it does more than necessary by moving the uv cache location. As-is, the cache still ends up on the same drive as before, just a different folder (C:\Users\...\AppData\Local\uv\cache to C:\Users\...\.unsloth\studio\cache\uv).

@Max-Reisinger

Copy link
Copy Markdown
Contributor Author

@oobabooga Thanks for the review! The current head addresses both earlier review findings and includes regression coverage.

Before making further changes, could you confirm whether keeping uv’s default cache under StudioHome/cache/uv is acceptable, or whether you prefer the narrower collision-only fix? The relevant Actions on the latest head also require approval.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed install.ps1 and studio/setup.ps1 currently leave uv on the user-wide LOCALAPPDATA cache, so the venv step dies on a stray file there, and pinning it under StudioHome/cache/uv with the restore in the outer finally looks right. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member

main now handles this in install.ps1 via Set-StudioUvCacheEnvironment, which defaults uv to StudioHome/cache/uv, skips an unusable default cache and restores the caller's UV_CACHE_DIR in the outer finally, so I do not think this is needed any more. Sorry for the slow review, and thanks for putting it together.

@danielhanchen

Copy link
Copy Markdown
Member

Main now picks the uv cache in install.ps1 and falls back when the default path is unusable, so I do not think this is needed any more. Sorry for the slow review, and thanks for putting it together.

@danielhanchen danielhanchen self-assigned this Oct 5, 2026
Superseded by the adaptive uv cache selection already on main (unslothai#10204, unslothai#10626, unslothai#10899, unslothai#10597).

Co-authored-by: Max-Reisinger <[email protected]>
@danielhanchen
danielhanchen force-pushed the fix/windows-installer-uv-cache branch from 796c5ca to 4e0bd89 Compare October 5, 2026 08:39
@danielhanchen
danielhanchen merged commit a0bd1f1 into unslothai:main Oct 5, 2026

@danielhanchen danielhanchen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the PR and for tracking down the os error 183 failure from #8991!

We ended up fixing this on main before this PR could land. The installer now picks the uv cache adaptively (#10204, #10626, #10899, #10597): it only uses the shared uv cache if that path is a real directory with packages in it. Otherwise it falls back to a cache inside the Studio folder, and that choice is made before the venv step. A regular file at %LOCALAPPDATA%\uv\cache is now skipped automatically, so the reported crash cannot happen. unsloth studio update uses the same selection (#10386).

Because of that, we merged this PR as an empty commit so you are still credited as a contributor. We did not apply the code changes. The setup.ps1 part would also have always forced the Studio cache during updates and repairs, overriding the adaptive selection, so a warm shared Torch/CUDA cache would have been downloaded again.

Thanks again for the contribution, and we would be glad to see more PRs from you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Install failed to create directory $HOME\AppData\Local\uv\cache: Cannot create a file when that file already exists. (os error 183)

3 participants