Repository navigation
studio setup: let Windows PowerShell load its own Security module - #7692
Conversation
988f5f2 to
f09b845
Compare
`unsloth studio update` failed on Windows when launched from a PowerShell 7
prompt:
installing uv package manager...
The 'Get-ExecutionPolicy' command was found in the module
'Microsoft.PowerShell.Security', but the module could not be loaded.
and stopped there with exit 1 and no further output. setup.ps1 installs uv by
running astral's install.ps1, which calls Get-ExecutionPolicy, and
Invoke-SetupCommand makes a failure there fatal.
_run_setup_script spawns powershell.exe, which is Windows PowerShell 5.1, and
the child inherits the caller's PSModulePath. From a pwsh 7 prompt that path
leads with PowerShell 7's module directories, which ship their own
Microsoft.PowerShell.Security. 5.1 finds that copy first and cannot load it.
The problem is precedence, not absence, so the system directory is prepended.
Two weaker variants were tested on a windows-latest runner and both still
failed: dropping PSModulePath so 5.1 rebuilds its default (the machine-level
value on that image also leads with PS7), and appending the system directory
(the PS7 copy is still found first).
Confirmed by a patched/unpatched pair on the same runner and the same PyPI
install, differing only in this file:
patched update exit 0, Security module error: False
unpatched update exit 1, Security module error: True
Guarded on PSEdition so the block is inert under PowerShell 7 itself, and on
Test-Path so a non-standard SystemRoot is a no-op.
pwsh is what Windows Terminal's default profile and the `pwsh` command give a
modern user, and there was no Windows coverage of `unsloth studio update` in
CI, so this went unnoticed.
f09b845 to
4df64f6
Compare
…posure install.ps1:1493 runs astral's uv installer in-process the same way setup.ps1 does, and that installer calls Get-ExecutionPolicy out of Microsoft.PowerShell.Security. The desktop app reaches it as Tauri -> Rust -> powershell.exe (studio/src-tauri/src/install.rs:326), and PowerShell rewrites PSModulePath only for a direct pwsh -> powershell.exe hop, so the Rust process in between leaves Windows PowerShell 5.1 leading with PowerShell 7's module directories. PowerShell/PowerShell#18681 is this exact chain through an intermediate process. The --shortcuts-only path returns at install.ps1:1104 before reaching the uv install, so the `unsloth studio update` shortcut refresh was never exposed. scripts/uninstall.ps1 loads no Security cmdlet and spawns no shell, so it needs nothing. Also guards on $env:SystemRoot, since Join-Path throws on an empty or null Path under ErrorActionPreference Stop and this runs before anything else, and corrects the comment about why the failure is fatal: the uv call is wrapped in try/catch, so what ends the run is that Invoke-Expression executes the installer in this process.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 314fead6e9
ℹ️ 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".
| $env:PSModulePath -split ';' | | ||
| Where-Object { $_ -and ($_ -ne $_UnslothSystemModules) } | ||
| ) | ||
| $env:PSModulePath = (@($_UnslothSystemModules) + $_UnslothKept) -join ';' |
There was a problem hiding this comment.
Restore the caller's module search path
When users follow the documented irm ... | iex or ./install.ps1 invocation, the installer runs inside their interactive PowerShell process, so this assignment permanently leaves the system module directory ahead of their user and Program Files module directories for the rest of the session. Subsequent module autoloads can therefore select Windows' bundled version instead of a user-installed version with the same name; the equivalent block in studio/setup.ps1 has the same effect when run directly. Save the original PSModulePath and restore it in a finally block after installation while allowing child setup processes to inherit the corrected value.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The mechanism is right: `$env:` is the process environment, not a scope, so neither the enclosing function in install.ps1 nor script exit contains it. Not taking the try/finally though, for two reasons.
It does not fix the case you name. The interactive path ends at install.ps1:2999 with a foreground `& $UnslothExe studio -p 8888`, so a finally at the end of Install-UnslothStudio would not run until the user stops the Studio server. The window stays open for essentially the whole session.
And the guard limits the blast radius more than it looks. `PSEdition -ne 'Core'` makes the block a no-op in a pwsh 7 host, which is where the pwsh -> powershell.exe conflict lives; it only fires in Windows PowerShell 5.1, where the directory is already present and only gets moved forward within the process. I considered narrowing the trigger to detect the broken chain, but that trades a session-lived precedence change for the risk of skipping the fix on a chain the check does not anticipate, and that failure mode is the install breaking outright. Given the fix already needed three attempts and patched/unpatched control runs to establish, I would rather keep it unconditional.
Recorded the reasoning in both files in 28f9f62 so it reads as intent rather than oversight.
The reordering is process scope, so it outlives the script in an interactive console. That is a deliberate trade, not an oversight, and the reasoning was only in the PR discussion. Comment only.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 28f9f62728
ℹ️ 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".
| $env:PSModulePath -split ';' | | ||
| Where-Object { $_ -and ($_ -ne $_UnslothSystemModules) } | ||
| ) | ||
| $env:PSModulePath = (@($_UnslothSystemModules) + $_UnslothKept) -join ';' |
There was a problem hiding this comment.
Preserve the module-path repair across environment refreshes
When an earlier dependency installation calls Refresh-Environment (for example after installing Git, Python, or the VC++ runtime) and uv is still unavailable, that function reloads every registry environment variable at studio/setup.ps1:141-147, including the broken machine-level PSModulePath, before Astral's installer runs at line 3080. This discards the prefix added here and lets Get-ExecutionPolicy fail in exactly the Windows PowerShell 5.1 scenario this change targets; skip PSModulePath during refresh or reapply this normalization before invoking the uv installer.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, this was a real hole in the fix. Refresh-Environment reloads every Machine and User variable except Path, and eight of its call sites (860, 949, 981, 1784, 2007, 2018, 2367, 2503) run before the uv installer at 3080, so the normalization was being undone exactly where it has to hold.
Fixed in 7e6a084 by making PSModulePath an exception alongside Path, rather than reapplying at the call site: reapplying would have to be repeated at every future caller, and skipping is correct for the same reason Path is skipped.
Also added tests/studio/test_psmodulepath_normalization.ps1, which pins both halves of the invariant, since the prepend-vs-append half was previously only guaranteed by a CI control run. It fails against the previous commit.
Refresh-Environment reloads every Machine and User variable except Path, which put the broken machine-level PSModulePath back over the normalization at the top of the file. Eight of its call sites run before the uv installer at setup.ps1:3080, and that installer is what loads Microsoft.PowerShell.Security, so the fix was undone exactly where it has to hold. PSModulePath now joins Path as an exception. Adds tests/studio/test_psmodulepath_normalization.ps1, which pins both halves: the system directory is prepended rather than appended in both entry points, and Refresh-Environment leaves PSModulePath alone. It fails against the previous commit.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
What happened
unsloth studio updatefailed on Windows when launched from a PowerShell 7 prompt:and stopped there, with no further output.
setup.ps1installs uv by running astral'sinstall.ps1, which callsGet-ExecutionPolicy, andInvoke-SetupCommandmakes a failure there fatal._run_setup_scriptspawnspowershell.exe, which is Windows PowerShell 5.1, and the child inherits the caller'sPSModulePath. From a pwsh 7 prompt that path leads with PowerShell 7's module directories:Those ship their own
Microsoft.PowerShell.Security. 5.1 finds that copy first and cannot load it.pwshis what Windows Terminal's default profile and thepwshcommand give a modern user, so this is the common case rather than an edge one. There was no Windows coverage ofunsloth studio updatein CI to catch it:studio-update-smoke.ymlis ubuntu andstudio-mac-update-smoke.ymlis macOS.The fix, and two that did not work
The problem is precedence, not absence, so the system module directory is prepended. Two weaker variants were tried on a
windows-latestrunner first and both still failed:PSModulePathso 5.1 rebuilds its defaultEvidence
A patched/unpatched pair on the same runner, same PyPI install, differing only in this file:
updateexitThe install is deliberately from PyPI rather than
--local, so the only variable issetup.ps1. Installing from a checkout makes pip reinstall theunslothpackage, which has to replace the runningunsloth.exeand fails withWinError 32on that runner, masking the thing under test. That is a separate problem, reported below.Scope
$PSVersionTable.PSEdition -ne 'Core', so the block is inert when setup.ps1 runs under PowerShell 7 itself.Test-Path, so a non-standardSystemRootis a no-op.Two Windows problems this does not fix
Found by the same matrix, filed separately so this stays reviewable:
unsloth studio update --localcannot replace the runningunsloth.exe._release_self_exe_lock_windowsrenames it out of the way first, and that rename itself fails withWinError 32, after which pip's reinstall fails the same way. This blocks every repeat local update on Windows.--localderivessite-packagesas the repo root. That is studio update --local: refuse site-packages as the repo root #7693.