diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index c72f0037..a36ee815 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -78,34 +78,60 @@ jobs: - name: 环境自检(Python / cppcheck / pre-commit) shell: powershell run: | + # NOTE: keep every run: block pure ASCII. The runner writes this block + # to a BOM-less .ps1 executed by Windows PowerShell 5.1, which decodes + # it as GBK -- non-ASCII bytes there break string quoting. $ErrorActionPreference = 'Stop' - $missing = @() - foreach ($item in @( - @{ name = 'Python'; cmd = 'python' }, - @{ name = 'cppcheck'; cmd = 'cppcheck' } - )) { - if (-not (Get-Command $item.cmd -ErrorAction SilentlyContinue)) { - $missing += $item.name - } + + # --- pick a Python that actually has pre-commit ------------------- + # The runner runs as SYSTEM, whose PATH is NOT the dev user's PATH. + # On this box SYSTEM's `python` is C:\msys64\...\python.exe (3.14.5, + # no pre-commit). Probe the same order as scripts/ci_local.py: + # current interpreter -> .venv -> anaconda3 + $repoRoot = (Get-Location).Path + $candidates = @( + (Get-Command python -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source), + (Join-Path $repoRoot '.venv\Scripts\python.exe'), + 'C:\ProgramData\anaconda3\python.exe' + ) + $py = $null + foreach ($c in $candidates) { + if (-not $c) { continue } + if (-not (Test-Path $c)) { continue } + & $c -m pre_commit --version *> $null + if ($LASTEXITCODE -eq 0) { $py = $c; break } } - if ($missing.Count -gt 0) { - Write-Host "::error::runner missing: $($missing -join ', ') -- install then re-run" + if (-not $py) { + Write-Host "::error::no Python with pre-commit found. Tried: $($candidates -join ' | ')" + exit 1 + } + Write-Host "using python: $py" + & $py --version + & $py -m pre_commit --version + "PYTHON=$py" | Out-File -FilePath $env:GITHUB_ENV -Append + + # --- cppcheck ----------------------------------------------------- + # cppcheck is often installed with a USER-scope PATH entry, which + # SYSTEM cannot see. Fall back to its default install dir. + $cpp = Get-Command cppcheck -ErrorAction SilentlyContinue + if ($cpp) { + Write-Host "found cppcheck: $($cpp.Source)" + } elseif (Test-Path 'C:\Program Files\Cppcheck\cppcheck.exe') { + $dir = 'C:\Program Files\Cppcheck' + $env:Path = "$env:Path;$dir" + "C:\Program Files\Cppcheck" | Out-File -FilePath $env:GITHUB_PATH -Append + Write-Host "cppcheck not on PATH; added $dir (User-scope PATH is invisible to SYSTEM)" + } else { + Write-Host "::error::runner missing cppcheck -- install it, then re-run" exit 1 } - python --version cppcheck --version - # pre-commit: use `python -m pre_commit` (does not rely on PATH console script) - python -m pre_commit --version - if ($LASTEXITCODE -ne 0) { - Write-Host "::error::runner missing pre-commit (pip install pre-commit)" - exit 1 - } Write-Host "runner environment check passed" - name: 安装 pre-commit 钩子到本仓库 shell: powershell - run: python -m pre_commit install --install-hooks + run: "& $env:PYTHON -m pre_commit install --install-hooks" - name: 决定跳过哪些钩子(Keil 依赖) id: skip @@ -128,7 +154,7 @@ jobs: SKIP: ${{ steps.skip.outputs.value }} run: | if ($env:SKIP) { Write-Host "SKIP=$env:SKIP" } - python -m pre_commit run --all-files --show-diff-on-failure + & $env:PYTHON -m pre_commit run --all-files --show-diff-on-failure if ($LASTEXITCODE -ne 0) { Write-Host "::error::L0 static gates failed (see output above)" exit $LASTEXITCODE @@ -173,6 +199,19 @@ jobs: if: steps.msvc.outputs.value == 'yes' shell: powershell run: | + # Resolve a working Python first (SYSTEM's PATH python may be unusable). + $repoRoot = (Get-Location).Path + $py = $null + foreach ($c in @( + (Get-Command python -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source), + (Join-Path $repoRoot '.venv\Scripts\python.exe'), + 'C:\ProgramData\anaconda3\python.exe' + )) { + if ($c -and (Test-Path $c)) { $py = $c; break } + } + if (-not $py) { Write-Host "::error::no python found"; exit 1 } + Write-Host "using python: $py" + # Iterate test/host/*/run_unit_test.py (each module ships its own runner via common harness) $runners = Get-ChildItem -Path 'test/host' -Recurse -Filter 'run_unit_test.py' | Sort-Object FullName @@ -185,7 +224,7 @@ jobs: Write-Host "================================" Write-Host "run: $($r.FullName)" Write-Host "================================" - python $r.FullName + & $py $r.FullName if ($LASTEXITCODE -ne 0) { Write-Host "::error::unit test failed: $($r.FullName) (exit $LASTEXITCODE)" $fail = 1