fix(build): bootstrap-windows 讓 MSYS2 bash 找得到 Go / wails / node / pnpm

Windows build 卡在 `go: command not found`,但 PowerShell 端 go 明明可用。

原因是腳本用 `bash.exe -l` 呼叫 make,login shell 會 source /etc/profile
重建 PATH,而非繼承 Windows 的。`MSYS2_PATH_TYPE=inherit` 只對 MSYS2
自己的啟動器有效,直接呼叫 bash.exe 時不生效。

腳本原本就為 Inno Setup 與 Python 手動 export 路徑,只是漏了這四個工具。
現在比照辦理:

- 新增 Find-ToolDir,先用 Get-Command(可找到自訂磁碟的安裝),
  找不到再退回已知安裝位置,都失敗則明確 Fail 並附安裝指引
- wails 路徑改問 `go env GOPATH` 而非假設 %USERPROFILE%\go,
  且用 go.exe 絕對路徑呼叫(否則在本 bug 情境下自己也會找不到 go)
- pnpm 獨立偵測,npm i -g 的安裝位置與 node 不同
- 各目錄個別加雙引號後以 : 串接,避免 Program Files 的空白被斷詞

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
jim800121chen 2026-07-22 21:30:31 +08:00
parent a7bc67baba
commit 61593f004d

View File

@ -104,6 +104,108 @@ function Convert-ToMsysPath($winPath) {
}
$msysPython = Convert-ToMsysPath $realPython
# ── 偵測 Go / wails / node / pnpm 的實際安裝目錄 ────────────────────
# 為什麼需要這段:
# $env:MSYS2_PATH_TYPE = 'inherit' 只影響 MSYS2 自己的 shell 啟動器
# msys2.exe / mingw64.exe直接呼叫 bash.exe -l 時 /etc/profile 會
# 重建 PATHWindows 的 PATH 不會被完整繼承 → bash 找不到 go / wails
# 出現 "/bin/bash: 列 1: go: command not found"。
# 所以必須跟 Inno Setup 一樣,明確把工具目錄轉成 MSYS2 路徑後 export。
#
# 偵測策略(都不寫死路徑,允許裝在任意磁碟 / 自訂目錄):
# 1. 先用 Get-Command 問 PowerShell 目前 PATH上面已重載 Machine+User PATH
# 2. 找不到再退回常見安裝位置winget / 官方 installer 的預設路徑)
function Find-ToolDir {
param(
[string] $Name, # 執行檔名(不含 .exe
[string[]] $Candidates # fallback 的完整 exe 路徑清單
)
$cmd = Get-Command $Name -ErrorAction SilentlyContinue
if ($cmd -and $cmd.Source) {
return (Split-Path $cmd.Source -Parent)
}
foreach ($p in $Candidates) {
if ($p -and (Test-Path $p)) { return (Split-Path $p -Parent) }
}
return $null
}
# Gowinget 預設 C:\Program Files\Go\bin但可能被裝到其他磁碟
$goDir = Find-ToolDir -Name 'go' -Candidates @(
"$env:ProgramFiles\Go\bin\go.exe",
"${env:ProgramFiles(x86)}\Go\bin\go.exe",
"$env:LOCALAPPDATA\Programs\Go\bin\go.exe",
"C:\Go\bin\go.exe"
)
if (-not $goDir) {
Fail @'
找不到 go.exe請確認 Go 已安裝winget install -e --id GoLang.Go
然後重開一個新的系統管理員 PowerShell再跑一次此腳本PATH 需要重新載入
'@
}
Log "偵測到 Go: $goDir"
# wailsgo install 會放到 $(go env GOPATH)\bin優先問 go 自己而不是猜 $HOME\go
# 用上面偵測到的 $goDir\go.exe 絕對路徑呼叫,不依賴 go 是否在 PowerShell PATH 上
$goPath = $null
$goExe = Join-Path $goDir 'go.exe'
if (Test-Path $goExe) {
try {
$goPath = (& $goExe env GOPATH 2>$null | Select-Object -First 1)
} catch {
$goPath = $null
}
}
if ([string]::IsNullOrWhiteSpace($goPath)) { $goPath = "$env:USERPROFILE\go" }
$wailsDir = Find-ToolDir -Name 'wails' -Candidates @(
(Join-Path $goPath 'bin\wails.exe'),
"$env:USERPROFILE\go\bin\wails.exe"
)
if (-not $wailsDir) {
Fail @"
找不到 wails.exe預期在 $goPath\bin
請先執行go install github.com/wailsapp/wails/v2/cmd/wails@latest
"@
}
Log "偵測到 wails: $wailsDir"
# nodeMakefile 的 pnpm build 需要 node 在 PATH 上
$nodeDir = Find-ToolDir -Name 'node' -Candidates @(
"$env:ProgramFiles\nodejs\node.exe",
"${env:ProgramFiles(x86)}\nodejs\node.exe",
"$env:LOCALAPPDATA\Programs\nodejs\node.exe"
)
if (-not $nodeDir) {
Fail @'
找不到 node.exe請確認 Node.js 已安裝winget install -e --id OpenJS.NodeJS.LTS
然後重開 PowerShell 再試一次
'@
}
Log "偵測到 node: $nodeDir"
# pnpmnpm i -g 會裝到 %APPDATA%\npm與 node 目錄不同,必須另外加)
$pnpmDir = Find-ToolDir -Name 'pnpm' -Candidates @(
"$env:APPDATA\npm\pnpm.cmd",
"$env:APPDATA\npm\pnpm",
"$env:ProgramFiles\nodejs\pnpm.cmd"
)
if (-not $pnpmDir) {
Fail @'
找不到 pnpm請執行npm i -g pnpm
然後重開 PowerShell 再試一次
'@
}
Log "偵測到 pnpm: $pnpmDir"
# 轉成 MSYS2 路徑去重後合併成「單一」export避免多行 export 互相覆蓋,
# 也避免同一目錄重複出現在 PATH 上,例如 pnpm 與 node 同目錄的情況)
$toolDirs = @($goDir, $wailsDir, $nodeDir, $pnpmDir) |
Where-Object { $_ } |
Select-Object -Unique
# 用 @() 強制成陣列:只有一個目錄時 pipeline 會退化成單一 string
# 後面的 .Count / -join 行為才不會有歧義
$msysToolDirs = @($toolDirs | ForEach-Object { Convert-ToMsysPath $_ })
# 找 Inno Setup Compiler (ISCC.exe)
# 重要:檔名是 ISCC.exe大寫winget 裝在 Program Files (x86) 不在 PATH 裡
function Find-Iscc {
@ -178,6 +280,16 @@ $bashParts = @(
"cd '$msysPath'",
"export VISIONA_PYTHON='$msysPython'"
)
# 把 go / wails / node / pnpm 的目錄加進 bash 的 PATH。
# 用一行 export 塞完所有目錄,前面補在 $PATH 之前(優先於 MSYS2 內建工具)。
# 每個目錄各自用雙引號包住,路徑含空白(如 "Program Files")與括號
# (如 "Program Files (x86)")才不會被 bash 拆字或當成子 shell 語法。
# 註export 是 assignment context等號右邊不做 word splitting
# 所以結尾的 :$PATH 不加引號也安全(與既有 Inno Setup 那行同慣例)。
if ($msysToolDirs -and $msysToolDirs.Count -gt 0) {
$quotedDirs = ($msysToolDirs | ForEach-Object { "`"$_`"" }) -join ':'
$bashParts += "export PATH=$quotedDirs`:`$PATH"
}
if ($msysIsccDir) {
$bashParts += "export PATH=`"$msysIsccDir`":`$PATH"
}