修复 rust

This commit is contained in:
2026-05-29 09:03:13 +08:00
parent dd3eb24d0f
commit b094cb85cd
3 changed files with 68 additions and 13 deletions

View File

@@ -103,13 +103,18 @@ function Refresh-Path {
# ──────────────────────────────────────────────────────────────
function Install-Winget {
param([string]$Id, [string]$DisplayName)
$wg = Get-Command winget -ErrorAction SilentlyContinue
if (-not $wg) {
# pwsh (Core) 里 winget 不在 PATH尝试固定路径
$wgPath = (Get-Command winget -ErrorAction SilentlyContinue)?.Source
if (-not $wgPath) {
$wgPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\winget.exe"
if (-not (Test-Path $wgPath)) { $wgPath = $null }
}
if (-not $wgPath) {
Write-Warn "未找到 winget请手动安装 $DisplayName"
return $false
}
Write-Info "winget install $Id ..."
winget install --id $Id --silent --accept-source-agreements --accept-package-agreements 2>&1 | Out-Null
& $wgPath install --id $Id --silent --accept-source-agreements --accept-package-agreements 2>&1 | Out-Null
Refresh-Path
return $true
}
@@ -576,7 +581,12 @@ if ($cargoCmd) {
Write-OK "Rust 已安装: $($rustcVer.Trim())"
} else {
# 检查 winget 是否已记录安装(避免重复安装时阻塞)
$wgList = winget list --id Rustlang.Rustup --accept-source-agreements 2>&1 | Out-String
$wgPath = (Get-Command winget -ErrorAction SilentlyContinue)?.Source
if (-not $wgPath) {
$fallback = "$env:LOCALAPPDATA\Microsoft\WindowsApps\winget.exe"
if (Test-Path $fallback) { $wgPath = $fallback }
}
$wgList = if ($wgPath) { & $wgPath list --id Rustlang.Rustup --accept-source-agreements 2>&1 | Out-String } else { "" }
if ($wgList -match "Rustlang.Rustup") {
Write-Info "rustup 已安装,但当前会话 PATH 未更新。已将 $cargoBin 加入 PATH"
# rustup 首次运行会初始化 stable 工具链
@@ -586,14 +596,16 @@ if ($cargoCmd) {
} else {
Write-Info "通过 winget 安装 Rust (rustup)..."
$ok = Install-Winget "Rustlang.Rustup" "Rustup"
if (Test-Path $cargoBin) { $env:Path = "$cargoBin;$env:Path" }
Refresh-Path
$cargoCmd = Get-Command cargo -ErrorAction SilentlyContinue
if ($cargoCmd) {
Write-OK "Rust 安装完成: $(& rustc --version 2>&1)"
} else {
Write-Warn "Rust 安装后未找到 cargo请关闭并重开终端后继续"
Write-Warn " 或手动安装https://rustup.rs"
if ($ok) {
if (Test-Path $cargoBin) { $env:Path = "$cargoBin;$env:Path" }
Refresh-Path
$cargoCmd = Get-Command cargo -ErrorAction SilentlyContinue
if ($cargoCmd) {
Write-OK "Rust 安装完成: $(& rustc --version 2>&1)"
} else {
Write-Warn "Rust 安装后未找到 cargo请关闭并重开终端后继续"
Write-Warn " 或手动安装https://rustup.rs"
}
}
}
}