CI:修正 python 探测被 NativeCommandError 中断
CI / L0 静态门禁 (pre-commit) (push) Failing after 17s
CI / L0.5 主机侧单测 (MSVC) (push) Successful in 16s

第四次运行(run#4)L0.5 已 PASS(status=1,MSVC 探测 + 39 单测全过),
L0 仍失败于自检步骤自身的一个 PS 5.1 行为:

  & $c -m pre_commit --version *> $null
  → NativeCommandError,脚本中止

根因:$ErrorActionPreference='Stop' 下,原生命令写 stderr 会被 PS5.1
当成终止性错误(NativeCommandError),即使 *> $null 重定向也无法阻止;
于是第一个候选(msys64 python,无 pre-commit)就把整个 for 循环打断,
根本轮不到 anaconda3。

处置:探测每个候选时临时把 $ErrorActionPreference 降为 'Continue',
用 2>&1 | Out-Null 吞掉输出并取 $LASTEXITCODE,循环后恢复 'Stop'。
已在 WinPS 5.1 本地实测该探测逻辑:
  msys64 rc=1 → .venv rc=1 → anaconda3 rc=0 选中,STEP OK。
This commit is contained in:
2026-10-02 01:24:50 +08:00
parent 8e7b22184e
commit 48b33921c0
+15 -6
View File
@@ -81,26 +81,35 @@ jobs:
# 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'
$repoRoot = (Get-Location).Path
# --- 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
# PATH python -> .venv -> anaconda3
#
# CAUTION: a failing native command writes to stderr; with
# $ErrorActionPreference='Stop' that would abort the whole script
# (NativeCommandError) before the next candidate is tried. So relax
# the preference around each probe, and restore it afterwards.
$candidates = @(
(Get-Command python -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source),
(Join-Path $repoRoot '.venv\Scripts\python.exe'),
'C:\ProgramData\anaconda3\python.exe'
)
$py = $null
$saved = $ErrorActionPreference
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 }
$ErrorActionPreference = 'Continue'
& $c -m pre_commit --version 2>&1 | Out-Null
$rc = $LASTEXITCODE
$ErrorActionPreference = $saved
if ($rc -eq 0) { $py = $c; break }
}
$ErrorActionPreference = 'Stop'
if (-not $py) {
Write-Host "::error::no Python with pre-commit found. Tried: $($candidates -join ' | ')"
exit 1
@@ -119,7 +128,7 @@ jobs:
} 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
$dir | 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"