fix: detect missing WSL distro by exit code, not help output

This commit is contained in:
qananasikq
2026-04-18 01:06:39 +03:00
parent 86a5f9ce17
commit 9d01cd0a23

View File

@@ -30,14 +30,15 @@ if (-not (Test-Path $envSrc)) {
# --- 1. WSL2 + Ubuntu 22.04 ---------------------------------- # --- 1. WSL2 + Ubuntu 22.04 ----------------------------------
Step "WSL state" Step "WSL state"
$wslList = @() # Probe properly: 'wsl -l -q' on a system without any distro PRINTS HELP and exits non-zero.
try { # Use -v (verbose) and check exit code; only valid distro lines are accepted.
$raw = wsl --list --quiet 2>$null $ubuntu = $null
if ($raw) { $probe = & wsl.exe -l -q 2>&1
$wslList = ($raw -split "`n") | ForEach-Object { ($_ -replace "`0","").Trim() } | Where-Object { $_ } $probeExit = $LASTEXITCODE
} if ($probeExit -eq 0 -and $probe) {
} catch {} $candidates = ($probe -join "`n") -split "`n" | ForEach-Object { ($_ -replace "[`0`r]","").Trim() } | Where-Object { $_ -and ($_ -notmatch '^[-= ]+$') -and ($_ -notmatch '(?i)usage|help|wsl\.exe|--install|argument|distribution') }
$ubuntu = $wslList | Where-Object { $_ -match "Ubuntu" } | Select-Object -First 1 $ubuntu = $candidates | Where-Object { $_ -match '(?i)^Ubuntu' } | Select-Object -First 1
}
if (-not $ubuntu) { if (-not $ubuntu) {
Step "Installing WSL2 + Ubuntu-22.04 (REBOOT REQUIRED)" Step "Installing WSL2 + Ubuntu-22.04 (REBOOT REQUIRED)"