Skip to content

Commit 6d37bd8

Browse files
committed
fix(profile): 修复代理检测失败回退和工具名提取问题
修复代理自动检测中异常捕获逻辑,确保失败时能回退到直接探测 统一工具名提取逻辑,在 Windows 上移除 .exe 扩展名以保持一致性
1 parent 669625f commit 6d37bd8

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

profile/features/environment.ps1

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,20 @@ function Initialize-Environment {
149149

150150
# 自动检测代理(缓存 5 分钟避免每次 profile 加载都做 TCP 探测)
151151
if (-not $SkipProxy) {
152-
$proxyState = Invoke-WithCache -Key "proxy-auto-detect" -MaxAge ([TimeSpan]::FromMinutes(5)) -CacheType Text -ScriptBlock {
153-
Set-Proxy -Command auto
154-
$result = if ($env:http_proxy) { 'on' } else { 'off' }
155-
return $result
152+
try {
153+
$proxyState = Invoke-WithCache -Key "proxy-auto-detect" -MaxAge ([TimeSpan]::FromMinutes(5)) -CacheType Text -ScriptBlock {
154+
Set-Proxy -Command auto
155+
$result = if ($env:http_proxy) { 'on' } else { 'off' }
156+
return $result
157+
}
158+
if ($proxyState -eq 'on' -and -not $env:http_proxy) {
159+
Set-Proxy -Command on
160+
}
156161
}
157-
if ($proxyState -eq 'on' -and -not $env:http_proxy) {
158-
Set-Proxy -Command on
162+
catch {
163+
Write-Verbose "代理自动检测失败: $($_.Exception.Message)"
164+
# 回退到不缓存的直接探测
165+
Set-Proxy -Command auto
159166
}
160167
}
161168

@@ -184,7 +191,9 @@ function Initialize-Environment {
184191
$foundCommands = Get-Command -Name $toolNames -CommandType Application -ErrorAction SilentlyContinue
185192
if ($foundCommands) {
186193
foreach ($cmd in $foundCommands) {
187-
$availableTools.Add($cmd.Name) | Out-Null
194+
# Windows 上 .Name 带扩展名 (如 starship.exe),统一去掉扩展名
195+
$toolName = [System.IO.Path]::GetFileNameWithoutExtension($cmd.Name)
196+
$availableTools.Add($toolName) | Out-Null
188197
}
189198
}
190199

0 commit comments

Comments
 (0)