Studio: fix setup Python venv detection#6044
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the studio/setup.ps1 script to prioritize the Python executable from the Studio virtual environment before falling back to py.exe. It introduces helper functions for checking compatible Python versions and updating the process PATH. The review feedback highlights a critical bug in the tilde expansion logic that can cause a crash in PowerShell 5.1 when the home directory is exactly "~", suggesting centralizing this logic into a helper function. Additionally, it recommends tightening the Python version regex with a word boundary to avoid false matches on future Python minor versions like 3.110.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| $_prereqStudioHome = $null | ||
| if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) { | ||
| $_prereqStudioHome = $env:UNSLOTH_STUDIO_HOME.Trim() | ||
| } elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) { | ||
| $_prereqStudioHome = $env:STUDIO_HOME.Trim() | ||
| } else { | ||
| $_prereqStudioHome = Join-Path $env:USERPROFILE ".unsloth\studio" | ||
| } | ||
| if ($_prereqStudioHome -eq "~" -or $_prereqStudioHome -like "~/*" -or $_prereqStudioHome -like "~\*") { | ||
| $_prereqStudioHome = (Join-Path $env:USERPROFILE $_prereqStudioHome.Substring(1).TrimStart('/','\')) | ||
| } |
There was a problem hiding this comment.
Resolving the Studio Home directory is a recurring and complex logical check that is duplicated in multiple places in this script (here and at line 1813). Centralizing this logic into a single helper function improves maintainability and ensures consistency.
Additionally, there is a critical bug in the current tilde expansion logic: if $_prereqStudioHome is exactly "~", then $_prereqStudioHome.Substring(1) returns an empty string "". Passing an empty string as the ChildPath argument to Join-Path throws a terminating error in PowerShell 5.1: Join-Path : Cannot bind argument to parameter 'ChildPath' because it is an empty string. Since $ErrorActionPreference = "Stop" is set, this will crash the setup script.
Defining a helper function Get-StudioHome that handles the exact "~" case separately resolves both the duplication and the potential crash.
function Get-StudioHome {
$homeDir = $null
if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) {
$homeDir = $env:UNSLOTH_STUDIO_HOME.Trim()
} elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) {
$homeDir = $env:STUDIO_HOME.Trim()
} else {
$homeDir = Join-Path $env:USERPROFILE ".unsloth\studio"
}
if ($homeDir -eq "~") {
$homeDir = $env:USERPROFILE
} elseif ($homeDir -like "~/*" -or $homeDir -like "~\*") {
$homeDir = Join-Path $env:USERPROFILE $homeDir.Substring(1).TrimStart('/','\')
}
return $homeDir
}
$_prereqStudioHome = Get-StudioHome
References
- Centralize recurring or complex logical checks into a single helper function and reuse it across the codebase to ensure consistency and simplify maintenance.
| param([string]$PythonExe) | ||
| try { | ||
| $out = & $PythonExe --version 2>&1 | Out-String | ||
| if ($out -match 'Python (3\.(11|12|13)(\.\d+)?)') { |
There was a problem hiding this comment.
The regular expression 'Python (3\.(11|12|13)(\.\d+)?)' can match incorrectly if the minor version starts with 11, 12, or 13 but has trailing digits (e.g., 3.110 or 3.120 in future releases). Adding a word boundary \b after the minor version ensures that only exact matches for 3.11, 3.12, or 3.13 are matched.
if ($out -match 'Python (3\.(11|12|13)\b(\.\d+)?)') {
Summary
Fixes #6017
setup.ps1could fail Python detection when an incompatible Python, such as 3.14, appeared earlier onPATH. This makes setup use the known Studio venv Python first when available, avoiding false prerequisite failures.install.ps1before falling back topy.exe>=3.11,<3.14PATH