From 9b17e28ca02067b044f3d83e75fbfff60fe73f86 Mon Sep 17 00:00:00 2001 From: mordom <838419775@qq.com> Date: Tue, 24 Nov 2026 20:53:34 +0800 Subject: [PATCH] fix(packaging): make onefile fork diagnostics opt-in Every onefile launch printed the fork target to stderr. Launchers such as MeteorMasterAI surface any child stderr as a warning, so users got a bogus warning on every run even though the patch resolved the logical path to itself (no MSIX redirection in a plain Win32 install). Gate the line behind METDETPY_ONEFILE_VERBOSE instead of printing it unconditionally: values starting with 1/y/t enable it, everything else (including unset) keeps production and Store builds silent. The marker string stays in the compiled bootstrap, so the post-build artifact check and the loud-by-choice troubleshooting path both keep working. Also verify in patch_bootstrap that the env var name still appears in CREATE_CODE, document the switch, and add a regression test. --- MetLib/packaging/nuitka_patches/README.md | 20 +++++++++++ .../nuitka_patches/patch_onefile_bootstrap.py | 33 +++++++++++++++---- tests/test_nuitka_patch.py | 9 +++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/MetLib/packaging/nuitka_patches/README.md b/MetLib/packaging/nuitka_patches/README.md index 6fbd4e8..a5dbf6b 100644 --- a/MetLib/packaging/nuitka_patches/README.md +++ b/MetLib/packaging/nuitka_patches/README.md @@ -28,6 +28,26 @@ python make_package.py --backend nuitka --onefile --apply-zip --build-report 补丁模块是打包内部实现,不再提供修改已安装 Nuitka 的独立命令。 +## 运行时诊断 + +补丁默认静默,正式环境和商店版都不会向 stderr 写任何内容:启动器(如 +MeteorMasterAI)会把子进程的任何 stderr 当作警告展示,无条件打印会导致每次 +启动都出现一条误报。排查时临时打开开关即可: + +```powershell +$env:METDETPY_ONEFILE_VERBOSE = "1" +``` + +打开后引导层会在启动子进程前向 stderr 打印一行 + +``` +[MetDetPy onefile] METDETPY_MSIX_ONEFILE_PATCH_ACTIVE fork target: logical="..." final="..." +``` + +非商店环境下 `logical` 与 `final` 相同(`%LOCALAPPDATA%\MetDetPy\`); +商店环境下 `final` 指向 `%LOCALAPPDATA%\Packages\\LocalCache\Local\MetDetPy\`, +可据此确认路径虚拟化确实发生。诊断只写 stderr,stdout 始终只保留 JSON 行协议。 + ## 兼容性检查 - `SUPPORTED_NUITKA_VERSIONS` 是版本白名单,初始保留历史构建版本 `4.0.5`。 diff --git a/MetLib/packaging/nuitka_patches/patch_onefile_bootstrap.py b/MetLib/packaging/nuitka_patches/patch_onefile_bootstrap.py index 9271feb..a5252bc 100644 --- a/MetLib/packaging/nuitka_patches/patch_onefile_bootstrap.py +++ b/MetLib/packaging/nuitka_patches/patch_onefile_bootstrap.py @@ -18,6 +18,11 @@ # The packaging backend imports this constant for artifact verification. BINARY_MARKER = SOURCE_MARKER + "_ACTIVE" +# 运行时诊断开关:设置该环境变量(值为 1/yes/true 等)才打印 fork 目标路径。 +# Fork diagnostics are opt-in. They used to print on every launch, and launchers +# that surface any child stderr as a warning showed a bogus warning every run. +VERBOSE_ENV_VAR = "METDETPY_ONEFILE_VERBOSE" + # --- 锚点:命中不上就失败,绝不猜测 ------------------------------------------------- # 锚点 1:Windows 专用区段内的子进程命令行构造函数。 @@ -128,12 +133,26 @@ // METDETPY_MSIX_ONEFILE_PATCH end""" CREATE_CODE = r""" // METDETPY_MSIX_ONEFILE_PATCH begin - // Diagnostics go to stderr and are flushed right away: stdout carries the - // JSON line protocol with the detection results and must stay clean. - fprintf(stderr, - "[MetDetPy onefile] METDETPY_MSIX_ONEFILE_PATCH_ACTIVE fork target: logical=\"%ls\" final=\"%ls\"\n", - (wchar_t const *)fork_binary, (wchar_t const *)fork_binary_final); - fflush(stderr); + // Fork diagnostics are opt-in. They used to be printed on every launch, + // which made launchers that treat any child stderr as a warning show a + // bogus warning for every run. Set METDETPY_ONEFILE_VERBOSE=1 (or any + // value starting with 1/y/t) to get the line back when troubleshooting. + // stdout carries the JSON line protocol with the detection results and + // stays clean either way. + { + wchar_t verbose_value[8] = {0}; + DWORD const verbose_length = + GetEnvironmentVariableW(L"METDETPY_ONEFILE_VERBOSE", verbose_value, 8); + if (verbose_length > 0 && verbose_length < 8) { + wchar_t const first = verbose_value[0]; + if (first == L'1' || first == L'y' || first == L'Y' || first == L't' || first == L'T') { + fprintf(stderr, + "[MetDetPy onefile] METDETPY_MSIX_ONEFILE_PATCH_ACTIVE fork target: logical=\"%ls\" final=\"%ls\"\n", + (wchar_t const *)fork_binary, (wchar_t const *)fork_binary_final); + fflush(stderr); + } + } + } bool_res = CreateProcessW(fork_binary_final, getCommandLineForChildProcess(), // command line @@ -223,6 +242,8 @@ def patch_bootstrap(path: Path, version: str) -> None: if BINARY_MARKER not in CREATE_CODE: fail("CREATE_CODE 里缺少 BINARY_MARKER,构建后的二进制校验会失效。") + if VERBOSE_ENV_VAR not in CREATE_CODE: + fail("CREATE_CODE 里缺少 VERBOSE_ENV_VAR,运行时诊断开关会失效。") with open(path, "r", encoding="utf-8", newline="") as handle: text = handle.read() diff --git a/tests/test_nuitka_patch.py b/tests/test_nuitka_patch.py index fb3954b..2268fc9 100644 --- a/tests/test_nuitka_patch.py +++ b/tests/test_nuitka_patch.py @@ -24,6 +24,15 @@ def test_versions_keep_anchor_checks(tmp_path, capsys, version, newline): assert b'\n' not in source.read_bytes().replace(b'\r\n', b'') +def test_fork_diagnostics_are_opt_in(): + """Production builds stay silent; the fork log needs METDETPY_ONEFILE_VERBOSE.""" + assert patch.VERBOSE_ENV_VAR in patch.CREATE_CODE + # The marker must stay in the artifact for the post-build verification. + assert patch.BINARY_MARKER in patch.CREATE_CODE + # The line is behind a runtime environment check, never printed unconditionally. + assert patch.CREATE_CODE.index('GetEnvironmentVariableW') < patch.CREATE_CODE.index('fprintf') + + @pytest.mark.parametrize('anchor', [patch.ANCHOR_HELPER, patch.ANCHOR_FORK, patch.ANCHOR_CREATE]) @pytest.mark.parametrize('count', [0, 2]) def test_bad_anchor_never_changes_source(tmp_path, anchor, count):