Skip to content

studio setup: let Windows PowerShell load its own Security module - #7692

Merged
danielhanchen merged 6 commits into
mainfrom
windows-update-psmodulepath
Aug 1, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
windows-update-psmodulepath

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

What happened

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.
##[error]update exited 1

and stopped there, with 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:

PSModulePath: C:\Users\runneradmin\Documents\PowerShell\Modules;
              C:\Program Files\PowerShell\Modules;
              c:\program files\powershell\7\Modules;...

Those ship their own Microsoft.PowerShell.Security. 5.1 finds that copy first and cannot load it.

pwsh is what Windows Terminal's default profile and the pwsh command give a modern user, so this is the common case rather than an edge one. There was no Windows coverage of unsloth studio update in CI to catch it: studio-update-smoke.yml is ubuntu and studio-mac-update-smoke.yml is 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-latest runner first and both still failed:

variant result
drop PSModulePath so 5.1 rebuilds its default still failed: the machine-level value on that image also leads with PS7
append the system directory still failed: the PS7 copy is still found first
prepend the system directory works

Evidence

A patched/unpatched pair on the same runner, same PyPI install, differing only in this file:

leg update exit hit the Security module error
patched 0 False
unpatched 1 True

The install is deliberately from PyPI rather than --local, so the only variable is setup.ps1. Installing from a checkout makes pip reinstall the unsloth package, which has to replace the running unsloth.exe and fails with WinError 32 on that runner, masking the thing under test. That is a separate problem, reported below.

Scope

  • Guarded on $PSVersionTable.PSEdition -ne 'Core', so the block is inert when setup.ps1 runs under PowerShell 7 itself.
  • Guarded on Test-Path, so a non-standard SystemRoot is a no-op.
  • The existing entries are preserved in order behind the prepended one; nothing is dropped.

Two Windows problems this does not fix

Found by the same matrix, filed separately so this stays reviewable:

  1. unsloth studio update --local cannot replace the running unsloth.exe. _release_self_exe_lock_windows renames it out of the way first, and that rename itself fails with WinError 32, after which pip's reinstall fails the same way. This blocks every repeat local update on Windows.
  2. A PyPI-installed CLI invoked with --local derives site-packages as the repo root. That is studio update --local: refuse site-packages as the repo root #7693.

`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.
@danielhanchen
danielhanchen force-pushed the windows-update-psmodulepath branch from f09b845 to 4df64f6 Compare July 31, 2026 16:17
@danielhanchen danielhanchen changed the title studio update: do not hand PowerShell 7's PSModulePath to Windows PowerShell studio setup: let Windows PowerShell load its own Security module Jul 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 1, 2026
…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

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

Comment thread install.ps1
$env:PSModulePath -split ';' |
Where-Object { $_ -and ($_ -ne $_UnslothSystemModules) }
)
$env:PSModulePath = (@($_UnslothSystemModules) + $_UnslothKept) -join ';'

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 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

@codex review

2 similar comments
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

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

Comment thread studio/setup.ps1
$env:PSModulePath -split ';' |
Where-Object { $_ -and ($_ -ne $_UnslothSystemModules) }
)
$env:PSModulePath = (@($_UnslothSystemModules) + $_UnslothKept) -join ';'

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 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 7e6a08473f

ℹ️ 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 479b46e into main Aug 1, 2026
73 of 74 checks passed
@danielhanchen
danielhanchen deleted the windows-update-psmodulepath branch August 1, 2026 10:05
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.

1 participant