From 48b33921c026d75589e8df98d36614ffde917648 Mon Sep 17 00:00:00 2001 From: Wanderingss <1624155937@qq.com> Date: Fri, 2 Oct 2026 01:24:50 +0800 Subject: [PATCH] =?UTF-8?q?CI=EF=BC=9A=E4=BF=AE=E6=AD=A3=20python=20?= =?UTF-8?q?=E6=8E=A2=E6=B5=8B=E8=A2=AB=20NativeCommandError=20=E4=B8=AD?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第四次运行(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。 --- .gitea/workflows/ci.yml | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index a36ee815..e19926a0 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -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"